Spaces:
Runtime error
Runtime error
Commit ·
d7ac493
1
Parent(s): b55f1be
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,42 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
-
import
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
def generate_plantuml_code(api_key,
|
| 7 |
openai.api_key = api_key
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
{
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
completions = openai.ChatCompletion.create(
|
| 14 |
-
model="gpt-3.5-turbo",
|
| 15 |
-
messages=messages,
|
| 16 |
-
max_tokens=512,
|
| 17 |
-
n=1,
|
| 18 |
-
stop=None,
|
| 19 |
-
temperature=0.7,
|
| 20 |
)
|
| 21 |
-
|
| 22 |
-
return
|
| 23 |
-
|
| 24 |
-
def
|
| 25 |
-
with
|
| 26 |
-
f
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
plantuml_code
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
)
|
| 50 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
+
from plantuml import PlantUML
|
| 4 |
+
import tempfile
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
def generate_plantuml_code(api_key, input_text):
|
| 8 |
openai.api_key = api_key
|
| 9 |
+
response = openai.ChatCompletion.create(
|
| 10 |
+
model='gpt-3.5-turbo',
|
| 11 |
+
messages=[{"role": "user", "content": f"Generate PlantUML code for the following use case: {input_text}"}],
|
| 12 |
+
max_tokens=150,
|
| 13 |
+
temperature=0,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
+
plantuml_code = response["choices"][0]["message"]["content"]
|
| 16 |
+
return plantuml_code
|
| 17 |
+
|
| 18 |
+
def render_plantuml_diagram(plantuml_code):
|
| 19 |
+
with tempfile.TemporaryDirectory() as tempdir:
|
| 20 |
+
puml = PlantUML(f"file://{tempdir}")
|
| 21 |
+
puml.processes(plantuml_code.encode())
|
| 22 |
+
with open(os.path.join(tempdir, "output.svg"), "r") as f:
|
| 23 |
+
svg_diagram = f.read()
|
| 24 |
+
return svg_diagram
|
| 25 |
+
|
| 26 |
+
def app(api_key, input_text="", plantuml_code=""):
|
| 27 |
+
if input_text:
|
| 28 |
+
plantuml_code = generate_plantuml_code(api_key, input_text)
|
| 29 |
+
svg_diagram = render_plantuml_diagram(plantuml_code)
|
| 30 |
+
return plantuml_code, svg_diagram
|
| 31 |
+
|
| 32 |
+
inputs = [
|
| 33 |
+
gr.inputs.Textbox(label="Enter OpenAI API Key"),
|
| 34 |
+
gr.inputs.Textbox(lines=5, label="Enter use case in natural language (optional)"),
|
| 35 |
+
gr.inputs.Textbox(lines=5, label="Paste PlantUML code (optional)")
|
| 36 |
+
]
|
| 37 |
+
outputs = [
|
| 38 |
+
gr.outputs.Textbox(label="Generated PlantUML Code"),
|
| 39 |
+
gr.outputs.HTML(label="Rendered Diagram")
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
gr.Interface(fn=app, inputs=inputs, outputs=outputs).launch()
|
|
|
|
|
|