Update app.py
Browse files
app.py
CHANGED
|
@@ -2,68 +2,55 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
# ✅ Set up Hugging Face client
|
| 6 |
client = InferenceClient(
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
# 🧠 Function to generate software architecture
|
| 12 |
def generate_software_spec(name, description, architecture, components, deployment, platform, extra):
|
| 13 |
-
print("generate_software_spec")
|
| 14 |
prompt = f"""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
---
|
| 21 |
-
|
| 22 |
-
App Name: {name}
|
| 23 |
-
Description: {description}
|
| 24 |
-
Architecture: {architecture}
|
| 25 |
-
Deployment: {deployment}
|
| 26 |
-
Platform: {platform}
|
| 27 |
-
Components & Tech Stack:
|
| 28 |
-
{components}
|
| 29 |
-
Extra requirements: {extra or "None"}
|
| 30 |
-
|
| 31 |
-
Return the result in this format:
|
| 32 |
-
|
| 33 |
-
### Sequence Diagram (Mermaid)
|
| 34 |
-
```mermaid
|
| 35 |
-
<sequence_diagram_here>
|
| 36 |
-
```
|
| 37 |
-
|
| 38 |
-
### Business Process Flow (Mermaid)
|
| 39 |
-
```mermaid
|
| 40 |
-
<flowchart_here>
|
| 41 |
-
```
|
| 42 |
-
|
| 43 |
-
### Code Snippets
|
| 44 |
-
#### Component: <Component 1>
|
| 45 |
-
```<language>
|
| 46 |
-
<code_here>
|
| 47 |
-
```
|
| 48 |
-
|
| 49 |
-
#### Component: <Component 2>
|
| 50 |
-
```<language>
|
| 51 |
-
<code_here>
|
| 52 |
-
```
|
| 53 |
-
"""
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# 🎨 Gradio UI
|
| 69 |
with gr.Blocks() as demo:
|
|
@@ -72,11 +59,11 @@ with gr.Blocks() as demo:
|
|
| 72 |
with gr.Row():
|
| 73 |
with gr.Column():
|
| 74 |
name = gr.Textbox(label="App Name", placeholder="MyApp")
|
| 75 |
-
description = gr.Textbox(label="Short Description", lines=2, placeholder="A system
|
| 76 |
-
architecture = gr.Radio(["Monolithic", "Backend-Frontend"], label="Architecture Style")
|
| 77 |
-
deployment = gr.Radio(["Serverless", "VM"], label="Deployment Style")
|
| 78 |
-
platform = gr.Radio(["Web App", "Mobile App"], label="Target Platform")
|
| 79 |
-
components = gr.Textbox(label="Components & Tech Stack
|
| 80 |
extra = gr.Textbox(label="Extra Requirements (optional)", lines=2)
|
| 81 |
submit = gr.Button("Generate Design")
|
| 82 |
status = gr.Markdown(visible=False)
|
|
@@ -86,15 +73,23 @@ with gr.Blocks() as demo:
|
|
| 86 |
|
| 87 |
def wrapper(name, description, architecture, components, deployment, platform, extra):
|
| 88 |
result = generate_software_spec(name, description, architecture, components, deployment, platform, extra)
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
import
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
def show_spinner():
|
| 100 |
return gr.update(visible=True, value="<center>⏳ Generating... Please wait.</center>")
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# ✅ Set up Hugging Face client
|
| 6 |
client = InferenceClient(
|
| 7 |
+
model="deepseek-ai/DeepSeek-R1-0528",
|
| 8 |
+
token=os.environ["HF_TOKEN"]
|
| 9 |
+
)
|
| 10 |
|
| 11 |
# 🧠 Function to generate software architecture
|
| 12 |
def generate_software_spec(name, description, architecture, components, deployment, platform, extra):
|
|
|
|
| 13 |
prompt = f"""
|
| 14 |
+
You are a software architect assistant. Based on the following input, generate:
|
| 15 |
+
1. A **Sequence Diagram** in Mermaid syntax
|
| 16 |
+
2. A **Business Process Flow** in Mermaid syntax
|
| 17 |
+
3. Code Snippets for each component based on the selected tech stack.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
App Name: {name}
|
| 20 |
+
Description: {description}
|
| 21 |
+
Architecture: {architecture}
|
| 22 |
+
Deployment: {deployment}
|
| 23 |
+
Platform: {platform}
|
| 24 |
+
Components & Tech Stack:
|
| 25 |
+
{components}
|
| 26 |
+
Extra requirements: {extra or "None"}
|
| 27 |
+
|
| 28 |
+
Return the result in this format:
|
| 29 |
+
|
| 30 |
+
### Sequence Diagram (Mermaid)
|
| 31 |
+
```mermaid
|
| 32 |
+
<sequence_diagram_here>
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
### Business Process Flow (Mermaid)
|
| 36 |
+
```mermaid
|
| 37 |
+
<flowchart_here>
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
### Code Snippets
|
| 41 |
+
#### Component: <Component 1>
|
| 42 |
+
```<language>
|
| 43 |
+
<code_here>
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
#### Component: <Component 2>
|
| 47 |
+
```<language>
|
| 48 |
+
<code_here>
|
| 49 |
+
```
|
| 50 |
+
"""
|
| 51 |
+
# 🔄 Get text response
|
| 52 |
+
response = client.text_generation(prompt=prompt, max_tokens=2048)
|
| 53 |
+
return response if response else "❌ Error: No response received."
|
| 54 |
|
| 55 |
# 🎨 Gradio UI
|
| 56 |
with gr.Blocks() as demo:
|
|
|
|
| 59 |
with gr.Row():
|
| 60 |
with gr.Column():
|
| 61 |
name = gr.Textbox(label="App Name", placeholder="MyApp")
|
| 62 |
+
description = gr.Textbox(label="Short Description", lines=2, placeholder="A system for ...")
|
| 63 |
+
architecture = gr.Radio(["Monolithic", "Backend-Frontend"], label="Architecture Style", value="Backend-Frontend")
|
| 64 |
+
deployment = gr.Radio(["Serverless", "VM"], label="Deployment Style", value="Serverless")
|
| 65 |
+
platform = gr.Radio(["Web App", "Mobile App"], label="Target Platform", value="Web App")
|
| 66 |
+
components = gr.Textbox(label="Components & Tech Stack", lines=4, placeholder="Backend: Python + Flask, Frontend: React")
|
| 67 |
extra = gr.Textbox(label="Extra Requirements (optional)", lines=2)
|
| 68 |
submit = gr.Button("Generate Design")
|
| 69 |
status = gr.Markdown(visible=False)
|
|
|
|
| 73 |
|
| 74 |
def wrapper(name, description, architecture, components, deployment, platform, extra):
|
| 75 |
result = generate_software_spec(name, description, architecture, components, deployment, platform, extra)
|
| 76 |
+
|
| 77 |
+
# Extract Mermaid blocks and wrap them properly
|
| 78 |
+
def render_mermaid_blocks(text):
|
| 79 |
+
import re
|
| 80 |
+
blocks = re.findall(r"```mermaid\n(.*?)```", text, re.DOTALL)
|
| 81 |
+
html_blocks = "".join([f"<div class='mermaid'>{block}</div>" for block in blocks])
|
| 82 |
+
return html_blocks or text
|
| 83 |
+
|
| 84 |
+
mermaid_html = render_mermaid_blocks(result)
|
| 85 |
+
final_html = f"""
|
| 86 |
+
<link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.css\">
|
| 87 |
+
<script src=\"https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js\"></script>
|
| 88 |
+
<script>mermaid.initialize({{startOnLoad:true}});</script>
|
| 89 |
+
{mermaid_html}
|
| 90 |
+
<pre style='white-space: pre-wrap;'>{result}</pre>
|
| 91 |
+
"""
|
| 92 |
+
return final_html, gr.update(visible=False)
|
| 93 |
|
| 94 |
def show_spinner():
|
| 95 |
return gr.update(visible=True, value="<center>⏳ Generating... Please wait.</center>")
|