Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
from groq import Groq
|
| 5 |
+
|
| 6 |
+
groq_api_key = os.getenv("MiruL")
|
| 7 |
+
|
| 8 |
+
def generate_svg(description):
|
| 9 |
+
client = Groq(api_key=groq_api_key)
|
| 10 |
+
completion = client.chat.completions.create(
|
| 11 |
+
model="llama-3.3-70b-versatile",
|
| 12 |
+
messages=[
|
| 13 |
+
{"role": "system", "content": "Generate an SVG code based on the user description. Make sure it's just the code only"},
|
| 14 |
+
{"role": "user", "content": description}
|
| 15 |
+
],
|
| 16 |
+
temperature=0.7,
|
| 17 |
+
max_completion_tokens=1024,
|
| 18 |
+
top_p=1,
|
| 19 |
+
stream=False,
|
| 20 |
+
stop=None,
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
svg_code = completion.choices[0].message.content.strip()
|
| 24 |
+
# Wrap the generated code with the complete SVG tag and apply the dynamic viewBox
|
| 25 |
+
full_svg_code = f"<svg xmlns='http://www.w3.org/2000/svg' width='100%' height='100%' viewBox='0 0 1000 400'>{svg_code}</svg>"
|
| 26 |
+
|
| 27 |
+
# Save the SVG code to a file for download
|
| 28 |
+
with open("diagram.svg", "w") as f:
|
| 29 |
+
f.write(full_svg_code)
|
| 30 |
+
|
| 31 |
+
return full_svg_code, full_svg_code, "diagram.svg"
|
| 32 |
+
|
| 33 |
+
def update_svg(modified_svg_code):
|
| 34 |
+
# Save the modified SVG code to the file
|
| 35 |
+
with open("diagram.svg", "w") as f:
|
| 36 |
+
f.write(modified_svg_code)
|
| 37 |
+
return modified_svg_code, modified_svg_code, "diagram.svg"
|
| 38 |
+
|
| 39 |
+
with gr.Blocks() as demo:
|
| 40 |
+
gr.Markdown("# Miru - SVG Viewer")
|
| 41 |
+
gr.Markdown("Generate an SVG diagram by describing it, then edit the SVG code if needed and click **Update Diagram** to refresh the display and download.")
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
description_input = gr.Textbox(label="Describe your diagram or animation", placeholder="Enter a description...")
|
| 45 |
+
generate_button = gr.Button("Generate Diagram")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
svg_output = gr.HTML(label="Generated SVG", elem_id="svg-container")
|
| 49 |
+
download_svg_output = gr.File(label="Download SVG")
|
| 50 |
+
|
| 51 |
+
svg_code_output = gr.Textbox(label="SVG Code (editable)", lines=10, interactive=True)
|
| 52 |
+
|
| 53 |
+
update_button = gr.Button("Update Diagram")
|
| 54 |
+
|
| 55 |
+
generate_button.click(
|
| 56 |
+
fn=generate_svg,
|
| 57 |
+
inputs=description_input,
|
| 58 |
+
outputs=[svg_output, svg_code_output, download_svg_output]
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
update_button.click(
|
| 62 |
+
fn=update_svg,
|
| 63 |
+
inputs=svg_code_output,
|
| 64 |
+
outputs=[svg_output, svg_code_output, download_svg_output]
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
demo.launch()
|