Afeezee commited on
Commit
61d7218
·
verified ·
1 Parent(s): a764cd0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -34
app.py CHANGED
@@ -11,25 +11,21 @@ def generate_svg(description):
11
  model="llama-3.3-70b-versatile",
12
  messages=[
13
  {"role": "system", "content": """
14
- Create an SVG diagram for {description} with the following specifications:
15
  1. Canvas Setup:
16
  - Use viewBox="0 0 800 600"
17
-
18
  3. Visual Style Requirements (apply when needed):
19
  - Text: centered, with title (14px, bold) and details (12px)
20
  - Color scheme: professional color choices
21
  - Consistent stroke width: 2px
22
-
23
  - Avoid line crossings in connections
24
-
25
  6. Additional Requirements:
26
  Put the name of the Diagram in bold on top
27
  - Multiple Color coding
28
-
29
-
30
  Please generate a complete SVG code that can be directly used. Ensure the code follows SVG best practices and includes proper spacing and formatting for readability.
31
  Make sure it's just the code only
32
- Generate the correct code, ensure it is free from construct error and this kind of error: Unescaped '<' not allowed in attributes values"""},
 
33
  {"role": "user", "content": description}
34
  ],
35
  temperature=0.7,
@@ -40,47 +36,56 @@ Generate the correct code, ensure it is free from construct error and this kind
40
  )
41
 
42
  svg_code = completion.choices[0].message.content.strip()
43
- # Wrap the generated code with the complete SVG tag and apply the dynamic viewBox
44
  full_svg_code = f"<svg xmlns='http://www.w3.org/2000/svg' width='100%' height='100%' viewBox='0 0 1000 400'>{svg_code}</svg>"
45
 
46
  # Save the SVG code to a file for download
47
  with open("diagram.svg", "w") as f:
48
  f.write(full_svg_code)
49
 
50
- return full_svg_code, full_svg_code, "diagram.svg"
 
51
 
52
  def update_svg(modified_svg_code):
53
  # Save the modified SVG code to the file
54
  with open("diagram.svg", "w") as f:
55
  f.write(modified_svg_code)
56
- return modified_svg_code, modified_svg_code, "diagram.svg"
57
 
58
  with gr.Blocks() as demo:
59
  gr.Markdown("# Miru - Diagram Viewer")
60
- gr.Markdown("Generate a diagram by describing it, then edit the SVG code if needed and click **Update Diagram** to refresh the display and download.")
61
 
62
- with gr.Row():
63
- description_input = gr.Textbox(label="Describe your diagram or animation", placeholder="Enter a description...")
64
- generate_button = gr.Button("Generate Diagram")
65
-
66
- with gr.Row():
67
- svg_output = gr.HTML(label="Generated SVG", elem_id="svg-container")
68
- download_svg_output = gr.File(label="Download SVG")
69
-
70
- svg_code_output = gr.Textbox(label="SVG Code (editable)", lines=10, interactive=True)
71
-
72
- update_button = gr.Button("Update Diagram")
73
 
74
- generate_button.click(
75
- fn=generate_svg,
76
- inputs=description_input,
77
- outputs=[svg_output, svg_code_output, download_svg_output]
78
- )
79
-
80
- update_button.click(
81
- fn=update_svg,
82
- inputs=svg_code_output,
83
- outputs=[svg_output, svg_code_output, download_svg_output]
84
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
- demo.launch()
 
11
  model="llama-3.3-70b-versatile",
12
  messages=[
13
  {"role": "system", "content": """
14
+ Create an SVG diagram for {description} with the following specifications:
15
  1. Canvas Setup:
16
  - Use viewBox="0 0 800 600"
 
17
  3. Visual Style Requirements (apply when needed):
18
  - Text: centered, with title (14px, bold) and details (12px)
19
  - Color scheme: professional color choices
20
  - Consistent stroke width: 2px
 
21
  - Avoid line crossings in connections
 
22
  6. Additional Requirements:
23
  Put the name of the Diagram in bold on top
24
  - Multiple Color coding
 
 
25
  Please generate a complete SVG code that can be directly used. Ensure the code follows SVG best practices and includes proper spacing and formatting for readability.
26
  Make sure it's just the code only
27
+ Generate the correct code, ensure it is free from construct error and this kind of error: Unescaped '<' not allowed in attributes values
28
+ """},
29
  {"role": "user", "content": description}
30
  ],
31
  temperature=0.7,
 
36
  )
37
 
38
  svg_code = completion.choices[0].message.content.strip()
 
39
  full_svg_code = f"<svg xmlns='http://www.w3.org/2000/svg' width='100%' height='100%' viewBox='0 0 1000 400'>{svg_code}</svg>"
40
 
41
  # Save the SVG code to a file for download
42
  with open("diagram.svg", "w") as f:
43
  f.write(full_svg_code)
44
 
45
+ # Return the HTML display, download file, and store the full code in state for editing in Tab 2.
46
+ return full_svg_code, "diagram.svg", full_svg_code
47
 
48
  def update_svg(modified_svg_code):
49
  # Save the modified SVG code to the file
50
  with open("diagram.svg", "w") as f:
51
  f.write(modified_svg_code)
52
+ return modified_svg_code, "diagram.svg"
53
 
54
  with gr.Blocks() as demo:
55
  gr.Markdown("# Miru - Diagram Viewer")
56
+ gr.Markdown("Use the **Generate Diagram** tab to create a diagram by describing it. In the **Update Diagram** tab, you can edit the generated SVG code and update the diagram.")
57
 
58
+ # State to store the generated SVG code for editing in Tab 2
59
+ svg_state = gr.State()
 
 
 
 
 
 
 
 
 
60
 
61
+ with gr.Tabs():
62
+ with gr.TabItem("Generate Diagram"):
63
+ description_input = gr.Textbox(label="Describe your diagram or animation",
64
+ placeholder="Enter a description...",
65
+ interactive=True)
66
+ # Generated SVG displayed in a larger HTML area
67
+ svg_output = gr.HTML(label="Generated SVG", elem_id="svg-container", value="")
68
+ svg_output.style(height="600px")
69
+ download_svg_output = gr.File(label="Download SVG")
70
+
71
+ # When the user presses Enter, generate_svg is called and updates the outputs and state.
72
+ description_input.submit(fn=generate_svg,
73
+ inputs=description_input,
74
+ outputs=[svg_output, download_svg_output, svg_state])
75
+
76
+ with gr.TabItem("Update Diagram"):
77
+ gr.Markdown("Edit the SVG code below and click **Update Diagram** to refresh the display and download file.")
78
+ modified_svg_input = gr.Textbox(label="Edit SVG Code", lines=10, interactive=True)
79
+ load_button = gr.Button("Load Generated Code")
80
+ update_button = gr.Button("Update Diagram")
81
+ updated_svg_output = gr.HTML(label="Updated SVG", elem_id="svg-container", value="")
82
+ updated_download_svg_output = gr.File(label="Download SVG")
83
+
84
+ # Load the generated SVG code from state into the editable textbox
85
+ load_button.click(lambda code: code, inputs=svg_state, outputs=modified_svg_input)
86
+ # Update the diagram when the update button is clicked
87
+ update_button.click(fn=update_svg,
88
+ inputs=modified_svg_input,
89
+ outputs=[updated_svg_output, updated_download_svg_output])
90
 
91
+ demo.launch()