Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
import requests
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Load environment variables (for FLOWISE_API token)
|
| 7 |
load_dotenv()
|
|
@@ -15,8 +20,16 @@ def query(payload):
|
|
| 15 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 16 |
return response.json()
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Streamlit UI
|
| 19 |
-
st.title("
|
| 20 |
|
| 21 |
# Get input from the user
|
| 22 |
user_input = st.text_area("Describe your architecture:",
|
|
@@ -27,11 +40,31 @@ if st.button("Generate Diagram"):
|
|
| 27 |
# Send the user's input to the API
|
| 28 |
payload = {"question": user_input}
|
| 29 |
output = query(payload)
|
| 30 |
-
print(output)
|
| 31 |
|
| 32 |
-
# Check and display the API response
|
| 33 |
-
if "
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
else:
|
| 36 |
st.write("Response:", output)
|
| 37 |
else:
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
import requests
|
| 4 |
+
import re
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
import tempfile
|
| 10 |
|
| 11 |
# Load environment variables (for FLOWISE_API token)
|
| 12 |
load_dotenv()
|
|
|
|
| 20 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 21 |
return response.json()
|
| 22 |
|
| 23 |
+
# Extract only Python code from text
|
| 24 |
+
def extract_python_code(text):
|
| 25 |
+
code_pattern = r"```python(.*?)```"
|
| 26 |
+
match = re.search(code_pattern, text, re.DOTALL)
|
| 27 |
+
if match:
|
| 28 |
+
return match.group(1).strip()
|
| 29 |
+
return "No Python code found in response."
|
| 30 |
+
|
| 31 |
# Streamlit UI
|
| 32 |
+
st.title("Architecture Diagram Generator")
|
| 33 |
|
| 34 |
# Get input from the user
|
| 35 |
user_input = st.text_area("Describe your architecture:",
|
|
|
|
| 40 |
# Send the user's input to the API
|
| 41 |
payload = {"question": user_input}
|
| 42 |
output = query(payload)
|
|
|
|
| 43 |
|
| 44 |
+
# Check and display the Python code from the API response
|
| 45 |
+
if "text" in output:
|
| 46 |
+
python_code = extract_python_code(output["text"])
|
| 47 |
+
st.code(python_code, language="python")
|
| 48 |
+
|
| 49 |
+
# Save the generated code to a temporary file and execute it
|
| 50 |
+
with tempfile.TemporaryDirectory() as tmpdirname:
|
| 51 |
+
temp_file = os.path.join(tmpdirname, "architecture_diagram.py")
|
| 52 |
+
with open(temp_file, "w") as f:
|
| 53 |
+
f.write(python_code)
|
| 54 |
+
|
| 55 |
+
# Execute the code
|
| 56 |
+
try:
|
| 57 |
+
exec(open(temp_file).read())
|
| 58 |
+
# Assuming the diagram was saved as "WordPress_Architecture.png" in the script
|
| 59 |
+
diagram_path = os.path.join(tmpdirname, "WordPress_Architecture.png")
|
| 60 |
+
|
| 61 |
+
# Display the generated image
|
| 62 |
+
if os.path.exists(diagram_path):
|
| 63 |
+
st.image(diagram_path, caption="Generated Architecture Diagram")
|
| 64 |
+
else:
|
| 65 |
+
st.error("No diagram image was generated.")
|
| 66 |
+
except Exception as e:
|
| 67 |
+
st.error(f"Error executing the Python code: {e}")
|
| 68 |
else:
|
| 69 |
st.write("Response:", output)
|
| 70 |
else:
|