Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,101 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
-
import torch
|
4 |
-
import os
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
print("Warning: HF_TOKEN not found. If the model is gated, it will fail to load.")
|
13 |
-
|
14 |
-
# Model repository ID (replace with your specific DeepscaleR model if different)
|
15 |
-
MODEL_ID = "agentica-org/DeepScaleR-1.5B-Preview"
|
16 |
-
|
17 |
-
# Load model and tokenizer
|
18 |
-
def load_model():
|
19 |
-
try:
|
20 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
21 |
-
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
22 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
23 |
-
model.to(device)
|
24 |
-
return tokenizer, model, device
|
25 |
-
except Exception as e:
|
26 |
-
return None, None, str(e)
|
27 |
-
|
28 |
-
# Load model at startup
|
29 |
-
tokenizer, model, load_error = load_model()
|
30 |
-
|
31 |
-
# Inference function with reasoning
|
32 |
-
def generate_text(input_text):
|
33 |
-
if load_error or tokenizer is None or model is None:
|
34 |
-
return f"Model failed to load: {load_error}", "Unable to proceed due to model loading error."
|
35 |
|
36 |
-
try:
|
37 |
-
reasoning = "Step 1: Tokenizing the input text...\n"
|
38 |
-
inputs = tokenizer(input_text, return_tensors="pt").to(device)
|
39 |
-
reasoning += f"Input tokenized into {inputs['input_ids'].shape[1]} tokens.\n"
|
40 |
-
|
41 |
-
reasoning += "Step 2: Running the DeepscaleR model for generation...\n"
|
42 |
-
outputs = model.generate(
|
43 |
-
inputs["input_ids"],
|
44 |
-
max_length=100,
|
45 |
-
num_return_sequences=1,
|
46 |
-
temperature=0.7,
|
47 |
-
do_sample=True
|
48 |
-
)
|
49 |
-
reasoning += f"Generated {outputs.shape[1]} tokens.\n"
|
50 |
-
|
51 |
-
reasoning += "Step 3: Decoding the output tokens into readable text...\n"
|
52 |
-
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
53 |
-
reasoning += "Decoding complete.\n"
|
54 |
-
|
55 |
-
reasoning += "Step 4: Finalizing the response.\n"
|
56 |
-
return generated_text, reasoning
|
57 |
-
except Exception as e:
|
58 |
-
error_msg = f"Error: {str(e)}"
|
59 |
-
return error_msg, f"Failed due to: {error_msg}"
|
60 |
-
|
61 |
-
# Custom CSS for black theme
|
62 |
-
custom_css = """
|
63 |
-
body { background-color: #1a1a1a; color: #ffffff; }
|
64 |
-
.gr-box { background-color: #2b2b2b; border: 1px solid #444444; border-radius: 5px; }
|
65 |
-
.gr-button { background-color: #4a4a4a; color: #ffffff; border: none; }
|
66 |
-
.gr-button:hover { background-color: #5a5a5a; }
|
67 |
-
.gr-textbox, .gr-textarea { background-color: #333333; color: #ffffff; border: 1px solid #555555; }
|
68 |
-
h1, h2, h3 { color: #ffffff; }
|
69 |
-
"""
|
70 |
-
|
71 |
-
# Gradio interface
|
72 |
-
with gr.Blocks(css=custom_css, theme="default") as demo:
|
73 |
-
gr.Markdown(
|
74 |
-
"""
|
75 |
-
# DeepscaleR Model Demo
|
76 |
-
A sleek, professional interface powered by xAI's Grok-inspired design.
|
77 |
-
Enter text below to see the DeepscaleR model's output and reasoning process.
|
78 |
-
"""
|
79 |
-
)
|
80 |
-
|
81 |
-
with gr.Row():
|
82 |
-
with gr.Column(scale=1):
|
83 |
-
input_text = gr.Textbox(
|
84 |
-
label="Input Text",
|
85 |
-
placeholder="Type your input here...",
|
86 |
-
lines=3
|
87 |
-
)
|
88 |
-
submit_btn = gr.Button("Generate")
|
89 |
-
|
90 |
-
with gr.Column(scale=2):
|
91 |
-
output_text = gr.Textbox(label="Generated Output", lines=5)
|
92 |
-
reasoning_text = gr.Textbox(label="Reasoning Process", lines=10)
|
93 |
-
|
94 |
-
submit_btn.click(
|
95 |
-
fn=generate_text,
|
96 |
-
inputs=input_text,
|
97 |
-
outputs=[output_text, reasoning_text]
|
98 |
-
)
|
99 |
-
|
100 |
-
# Launch the app
|
101 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
+
with gr.Blocks(fill_height=True) as demo:
|
4 |
+
with gr.Sidebar():
|
5 |
+
gr.Markdown("# Inference Provider")
|
6 |
+
gr.Markdown("This Space showcases the agentica-org/DeepScaleR-1.5B-Preview model, served by the hf-inference API. Sign in with your Hugging Face account to use this API.")
|
7 |
+
button = gr.LoginButton("Sign in")
|
8 |
+
gr.load("models/agentica-org/DeepScaleR-1.5B-Preview", accept_token=button, provider="hf-inference")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
demo.launch()
|