|
import os |
|
import streamlit as st |
|
import requests |
|
import re |
|
from dotenv import load_dotenv |
|
import tempfile |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
API_URL = os.getenv('API_URL') |
|
headers = {"Authorization": f"Bearer {os.getenv('FLOWISE_API')}"} |
|
|
|
|
|
def query(payload): |
|
response = requests.post(API_URL, headers=headers, json=payload) |
|
return response.json() |
|
|
|
|
|
def extract_python_code(text): |
|
code_pattern = r"```(?:python)?(.*?)```" |
|
match = re.search(code_pattern, text, re.DOTALL) |
|
if match: |
|
return match.group(1).strip() |
|
return "No Python code found in response." |
|
|
|
|
|
st.title("Architecture Diagram Generator") |
|
|
|
|
|
user_input = st.text_area("Describe your architecture:", |
|
placeholder="Enter a description of your architecture here...") |
|
|
|
if st.button("Generate Diagram"): |
|
if user_input: |
|
|
|
payload = {"question":"make sure to import from diagrams import Diagram" + user_input + " architecture"} |
|
output = query(payload) |
|
print(output) |
|
|
|
|
|
if "text" in output: |
|
python_code = extract_python_code(output["text"]) |
|
st.code(python_code, language="python") |
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname: |
|
temp_file = os.path.join(tmpdirname, "architecture_diagram.py") |
|
with open(temp_file, "w") as f: |
|
f.write(python_code) |
|
|
|
|
|
original_dir = os.getcwd() |
|
os.chdir(tmpdirname) |
|
try: |
|
exec(open(temp_file).read()) |
|
diagram_path = os.path.join(tmpdirname, "diagram.jpg") |
|
|
|
|
|
if os.path.exists(diagram_path): |
|
st.image(diagram_path, caption="Generated Architecture Diagram") |
|
else: |
|
st.error("No diagram image was generated.") |
|
except Exception as e: |
|
st.error(f"Error executing the Python code: {e}") |
|
finally: |
|
os.chdir(original_dir) |
|
else: |
|
st.write("Response:", output) |
|
else: |
|
st.warning("Please enter an architecture description.") |
|
|