Spaces:
Sleeping
Sleeping
added provider
Browse files
app.py
CHANGED
|
@@ -1,48 +1,84 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
#
|
| 4 |
def generate_code(prompt):
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
# Gradio
|
| 28 |
-
with gr.Blocks(
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Function to generate code with proper error handling
|
| 5 |
def generate_code(prompt):
|
| 6 |
+
try:
|
| 7 |
+
# Get the token from Gradio's secrets
|
| 8 |
+
token = gr.secrets["HF_TOKEN"]
|
| 9 |
+
|
| 10 |
+
client = InferenceClient(
|
| 11 |
+
model="deepseek-ai/DeepSeek-V3",
|
| 12 |
+
token=token
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
system_prompt = """You are an expert Next.js, TypeScript and TailwindCSS developer.
|
| 16 |
+
Generate clean, efficient code following these rules:
|
| 17 |
+
1. Use Next.js App Router
|
| 18 |
+
2. Strict TypeScript typing
|
| 19 |
+
3. Modern TailwindCSS classes
|
| 20 |
+
4. Include proper error boundaries
|
| 21 |
+
5. Add JSDoc comments"""
|
| 22 |
+
|
| 23 |
+
response = client.chat_completion(
|
| 24 |
+
messages=[
|
| 25 |
+
{"role": "system", "content": system_prompt},
|
| 26 |
+
{"role": "user", "content": prompt}
|
| 27 |
+
],
|
| 28 |
+
max_tokens=1500,
|
| 29 |
+
temperature=0.3
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
return response.choices[0].message.content
|
| 33 |
+
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"Error generating code: {str(e)}"
|
| 36 |
|
| 37 |
+
# Gradio interface with improved layout
|
| 38 |
+
with gr.Blocks(
|
| 39 |
+
title="Next.js Code Generator",
|
| 40 |
+
theme=gr.themes.Soft(),
|
| 41 |
+
css=".gradio-container {max-width: 900px !important}"
|
| 42 |
+
) as demo:
|
| 43 |
|
| 44 |
+
gr.Markdown("## 🚀 Next.js + TypeScript + TailwindCSS Code Generator")
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
with gr.Column(scale=3):
|
| 48 |
+
input_prompt = gr.Textbox(
|
| 49 |
+
label="Describe your component",
|
| 50 |
+
placeholder="Create a responsive navbar with dark mode toggle...",
|
| 51 |
+
lines=5,
|
| 52 |
+
max_lines=10
|
| 53 |
+
)
|
| 54 |
+
with gr.Row():
|
| 55 |
+
submit_btn = gr.Button("Generate Code", variant="primary")
|
| 56 |
+
clear_btn = gr.Button("Clear")
|
| 57 |
|
| 58 |
+
with gr.Column(scale=7):
|
| 59 |
+
output_code = gr.Code(
|
| 60 |
+
label="Generated Component",
|
| 61 |
+
language="typescript",
|
| 62 |
+
interactive=False,
|
| 63 |
+
lines=25
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Form submission handling
|
| 67 |
+
submit_btn.click(
|
| 68 |
+
fn=generate_code,
|
| 69 |
+
inputs=input_prompt,
|
| 70 |
+
outputs=output_code
|
| 71 |
+
)
|
| 72 |
|
| 73 |
+
clear_btn.click(
|
| 74 |
+
fn=lambda: ("", ""),
|
| 75 |
+
inputs=None,
|
| 76 |
+
outputs=[input_prompt, output_code]
|
| 77 |
+
)
|
| 78 |
|
| 79 |
+
# Launch with sharing enabled
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
demo.launch(
|
| 82 |
+
share=True,
|
| 83 |
+
auth=lambda u,p: True # Remove this for production!
|
| 84 |
+
)
|