File size: 2,995 Bytes
8ac80b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = [
#     "transformers>=4.40.0",
#     "torch>=2.0.0",
#     "rich>=13.0.0",
# ]
# ///
"""
Example uv script with inline dependencies.

This script demonstrates the PEP 723 inline metadata format for uv scripts.
Dependencies are declared in the script header and installed automatically.

Usage:
    # Run locally
    uv run example_uv_script.py

    # Run on remote HF Space via SSH
    scp example_uv_script.py user@SPACE.hf.space:/app/
    ssh user@SPACE.hf.space "cd /app && uv run example_uv_script.py"

    # Make executable and run directly
    chmod +x example_uv_script.py
    ./example_uv_script.py
"""

from rich.console import Console
from rich.table import Table
import torch


def check_cuda() -> dict:
    """Check CUDA availability and device info."""
    info = {
        "cuda_available": torch.cuda.is_available(),
        "device_count": torch.cuda.device_count() if torch.cuda.is_available() else 0,
        "device_name": None,
        "cuda_version": None,
    }

    if info["cuda_available"]:
        info["device_name"] = torch.cuda.get_device_name(0)
        info["cuda_version"] = torch.version.cuda

    return info


def run_inference():
    """Run a simple inference example."""
    from transformers import pipeline

    # Use CPU if CUDA not available
    device = 0 if torch.cuda.is_available() else -1

    console = Console()
    console.print("\n[bold blue]Running sentiment analysis...[/bold blue]\n")

    classifier = pipeline("sentiment-analysis", device=device)
    texts = [
        "I love using Hugging Face!",
        "This is a challenging problem.",
        "The weather is nice today.",
    ]

    results = classifier(texts)

    # Display results in a table
    table = Table(title="Sentiment Analysis Results")
    table.add_column("Text", style="cyan")
    table.add_column("Label", style="green")
    table.add_column("Score", style="magenta")

    for text, result in zip(texts, results):
        table.add_row(text, result["label"], f"{result['score']:.4f}")

    console.print(table)
    return results


def main():
    console = Console()

    # Header
    console.print("\n[bold green]HF Space Sandbox - Example Script[/bold green]")
    console.print("=" * 50)

    # Check CUDA
    cuda_info = check_cuda()

    info_table = Table(title="System Information")
    info_table.add_column("Property", style="cyan")
    info_table.add_column("Value", style="yellow")

    info_table.add_row("CUDA Available", str(cuda_info["cuda_available"]))
    info_table.add_row("Device Count", str(cuda_info["device_count"]))
    if cuda_info["device_name"]:
        info_table.add_row("GPU", cuda_info["device_name"])
        info_table.add_row("CUDA Version", cuda_info["cuda_version"])

    console.print(info_table)

    # Run inference
    run_inference()

    console.print("\n[bold green]Done![/bold green]\n")


if __name__ == "__main__":
    main()