Spaces:
Runtime error
Runtime error
Commit
·
eae9a22
1
Parent(s):
9914455
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import plantuml
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Function to call GPT-3.5-turbo model
|
| 7 |
+
def generate_plantuml_code(api_key, prompt):
|
| 8 |
+
openai.api_key = api_key
|
| 9 |
+
response = openai.Completion.create(
|
| 10 |
+
engine="gpt-3.5-turbo",
|
| 11 |
+
prompt=prompt,
|
| 12 |
+
max_tokens=150,
|
| 13 |
+
n=1,
|
| 14 |
+
stop=None,
|
| 15 |
+
temperature=0.7,
|
| 16 |
+
)
|
| 17 |
+
plantuml_code = response.choices[0].text.strip()
|
| 18 |
+
return plantuml_code
|
| 19 |
+
|
| 20 |
+
# Function to convert PlantUML code to SVG image
|
| 21 |
+
def plantuml_code_to_svg(plantuml_code):
|
| 22 |
+
with open("diagram.txt", "w") as f:
|
| 23 |
+
f.write(plantuml_code)
|
| 24 |
+
|
| 25 |
+
plantuml.exec_cmd(["-tsvg", "diagram.txt"])
|
| 26 |
+
|
| 27 |
+
with open("diagram.svg", "r") as f:
|
| 28 |
+
svg = f.read()
|
| 29 |
+
|
| 30 |
+
return svg
|
| 31 |
+
|
| 32 |
+
# Function to process user input and generate diagram
|
| 33 |
+
def create_diagram(api_key, description):
|
| 34 |
+
plantuml_code = generate_plantuml_code(api_key, description)
|
| 35 |
+
svg_image = plantuml_code_to_svg(plantuml_code)
|
| 36 |
+
return plantuml_code, svg_image
|
| 37 |
+
|
| 38 |
+
# Gradio interface
|
| 39 |
+
api_key_input = gr.Textbox(label="Enter your OpenAI API key")
|
| 40 |
+
description_input = gr.Textbox(lines=5, label="Describe the diagram in natural language")
|
| 41 |
+
plantuml_code_output = gr.Textbox(lines=10, label="Generated PlantUML code")
|
| 42 |
+
diagram_output = gr.Image(plot=True, label="Diagram")
|
| 43 |
+
|
| 44 |
+
iface = gr.Interface(
|
| 45 |
+
fn=create_diagram,
|
| 46 |
+
inputs=[api_key_input, description_input],
|
| 47 |
+
outputs=[plantuml_code_output, diagram_output],
|
| 48 |
+
)
|
| 49 |
+
iface.launch()
|