File size: 2,719 Bytes
773cbcf
 
 
b711724
773cbcf
b711724
773cbcf
 
 
 
 
 
 
 
 
 
 
 
 
b711724
 
f7ee2da
b711724
 
 
 
 
773cbcf
b711724
773cbcf
 
 
 
 
 
 
 
819503b
773cbcf
98f45cb
773cbcf
b711724
 
 
 
 
 
 
 
 
 
 
e779fad
 
 
b711724
 
17c4426
b711724
 
 
 
 
 
 
 
e779fad
 
773cbcf
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import streamlit as st
import requests
import re
from dotenv import load_dotenv
import tempfile

# Load environment variables (for FLOWISE_API token)
load_dotenv()

# Define API settings
API_URL = "https://nakheeltech.com:8030/api/v1/prediction/c1681ef1-8f47-4004-b4ab-594fbbd3eb3f"
headers = {"Authorization": f"Bearer {os.getenv('FLOWISE_API')}"}

# Function to send a query to the API
def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()

# Extract only Python code from text
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."

# Streamlit UI
st.title("Architecture Diagram Generator")

# Get input from the user
user_input = st.text_area("Describe your architecture:", 
                          placeholder="Enter a description of your architecture here...")

if st.button("Generate Diagram"):
    if user_input:
        # Send the user's input to the API
        payload = {"question":"make sure to import from diagrams import Diagram" + user_input + " architecture"}
        output = query(payload)
        print(output)
        
        # Check and display the Python code from the API response
        if "text" in output:
            python_code = extract_python_code(output["text"])
            st.code(python_code, language="python")

            # Save the generated code to a temporary file and execute it
            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)

                # Change directory to temp and execute the code
                original_dir = os.getcwd()
                os.chdir(tmpdirname)
                try:
                    exec(open(temp_file).read())
                    diagram_path = os.path.join(tmpdirname, "diagram.jpg")

                    # Display the generated image
                    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)  # Restore the original directory
        else:
            st.write("Response:", output)
    else:
        st.warning("Please enter an architecture description.")