Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,39 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
# Use a small code generation model that works on free tier
|
5 |
-
try:
|
6 |
-
generator = pipeline("text-generation", model="microsoft/DialoGPT-small")
|
7 |
-
except:
|
8 |
-
# Even smaller fallback
|
9 |
-
generator = pipeline("text-generation", model="distilgpt2")
|
10 |
|
11 |
def generate_code(prompt):
|
12 |
-
#
|
13 |
-
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
demo = gr.Interface(
|
23 |
fn=generate_code,
|
24 |
-
inputs=gr.Textbox(label="Code prompt"
|
25 |
outputs=gr.Textbox(label="Generated Code"),
|
26 |
-
title="
|
27 |
-
description="
|
28 |
)
|
29 |
|
30 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def generate_code(prompt):
|
6 |
+
# Use Hugging Face's free inference API
|
7 |
+
API_URL = "https://api-inference.huggingface.co/models/microsoft/CodeGPT-small-py"
|
8 |
+
|
9 |
+
headers = {
|
10 |
+
"Authorization": "Bearer hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # You'll need to get this
|
11 |
+
}
|
12 |
|
13 |
+
payload = {
|
14 |
+
"inputs": prompt,
|
15 |
+
"parameters": {
|
16 |
+
"max_length": 100,
|
17 |
+
"temperature": 0.7
|
18 |
+
}
|
19 |
+
}
|
20 |
|
21 |
+
try:
|
22 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
23 |
+
if response.status_code == 200:
|
24 |
+
result = response.json()
|
25 |
+
return result[0]['generated_text']
|
26 |
+
else:
|
27 |
+
return f"# Error: {response.status_code}\n# Try again in a moment"
|
28 |
+
except:
|
29 |
+
return f"# Generated template for: {prompt}\ndef your_function():\n # Add implementation here\n pass"
|
30 |
|
31 |
demo = gr.Interface(
|
32 |
fn=generate_code,
|
33 |
+
inputs=gr.Textbox(label="Code prompt"),
|
34 |
outputs=gr.Textbox(label="Generated Code"),
|
35 |
+
title="Code Generator",
|
36 |
+
description="Generate code using Hugging Face API"
|
37 |
)
|
38 |
|
39 |
demo.launch()
|