Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,32 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
# Load
|
4 |
generator = gr.Interface.load("huggingface/HuggingFaceH4/starchat-alpha")
|
5 |
|
6 |
def generate_code(prompt, max_tokens):
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
interface = gr.Interface(
|
10 |
fn=generate_code,
|
11 |
inputs=[
|
12 |
-
gr.Textbox(label="Code Prompt",
|
13 |
-
gr.Slider(20, 300, step=10, label="Max Tokens", value=100),
|
14 |
],
|
15 |
-
outputs=gr.Textbox(label="Generated Code"),
|
16 |
-
title="🧠 Code Generator (No Token
|
17 |
-
description="
|
18 |
)
|
19 |
|
20 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
# Load Hugging Face Space that provides code generation (no token needed)
|
4 |
generator = gr.Interface.load("huggingface/HuggingFaceH4/starchat-alpha")
|
5 |
|
6 |
def generate_code(prompt, max_tokens):
|
7 |
+
try:
|
8 |
+
# Call the generator with just the prompt
|
9 |
+
result = generator(prompt)
|
10 |
+
|
11 |
+
# Handle various return formats (tuple, list, dict, etc.)
|
12 |
+
if isinstance(result, (list, tuple)):
|
13 |
+
return result[0] if result else "No output received."
|
14 |
+
elif isinstance(result, dict):
|
15 |
+
return result.get("text", "No 'text' field in result.")
|
16 |
+
else:
|
17 |
+
return str(result)
|
18 |
+
except Exception as e:
|
19 |
+
return f"⚠️ Error generating code: {str(e)}"
|
20 |
|
21 |
interface = gr.Interface(
|
22 |
fn=generate_code,
|
23 |
inputs=[
|
24 |
+
gr.Textbox(lines=5, label="Code Prompt", placeholder="e.g., def factorial(n):"),
|
25 |
+
gr.Slider(minimum=20, maximum=300, step=10, label="Max Tokens (not used in this model)", value=100),
|
26 |
],
|
27 |
+
outputs=gr.Textbox(lines=10, label="Generated Code"),
|
28 |
+
title="🧠 Code Generator (No Token Needed)",
|
29 |
+
description="This app uses a free Hugging Face-hosted model. No token or GPU needed!"
|
30 |
)
|
31 |
|
32 |
if __name__ == "__main__":
|