shukdevdatta123 commited on
Commit
5105400
·
verified ·
1 Parent(s): d2616c0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import openai
3
+ import tempfile
4
+ import os
5
+ from pymermaid import mermaid
6
+
7
+ # Function to call OpenAI GPT model for prompt processing
8
+ def get_diagram_code(prompt, diagram_type, api_key):
9
+ try:
10
+ # Set the OpenAI API key dynamically based on user input
11
+ openai.api_key = api_key
12
+
13
+ response = openai.Completion.create(
14
+ engine="gpt-4",
15
+ prompt=f"Generate a {diagram_type} diagram in Mermaid.js syntax based on the following prompt: {prompt}",
16
+ max_tokens=500
17
+ )
18
+ return response.choices[0].text.strip()
19
+ except Exception as e:
20
+ st.error(f"Error: {e}")
21
+ return None
22
+
23
+ # Function to render Mermaid.js diagram using pymermaid
24
+ def render_mermaid(diagram_code):
25
+ # Create a temporary file to save the mermaid code
26
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mmd") as mmd_file:
27
+ mmd_file.write(diagram_code.encode('utf-8'))
28
+ mmd_file.close()
29
+
30
+ # Generate diagram image using pymermaid
31
+ output_image_path = mmd_file.name.replace(".mmd", ".png")
32
+ mermaid.render(mmd_file.name, output_image_path)
33
+
34
+ return output_image_path
35
+
36
+ # Streamlit App UI
37
+ def main():
38
+ st.title("Generate Diagrams using GPT-4 and Mermaid.js")
39
+
40
+ # User input for OpenAI API key
41
+ api_key = st.text_input("Enter your OpenAI API Key:", type="password")
42
+
43
+ # Check if API key is provided
44
+ if not api_key:
45
+ st.warning("Please enter your OpenAI API Key to continue.")
46
+ return
47
+
48
+ # User prompt for diagram type
49
+ prompt = st.text_area("Enter your prompt for the diagram:", "")
50
+
51
+ diagram_types = ["UML Diagram", "ER Diagram", "State Diagram", "Class Diagram", "Sequence Diagram"]
52
+ diagram_choice = st.selectbox("Select the type of diagram to generate:", diagram_types)
53
+
54
+ if st.button("Generate Diagram"):
55
+ if prompt:
56
+ diagram_code = get_diagram_code(prompt, diagram_choice, api_key)
57
+
58
+ if diagram_code:
59
+ # Render Mermaid code to Streamlit
60
+ st.code(diagram_code, language='mermaid')
61
+
62
+ # Save diagram as image
63
+ try:
64
+ image_path = render_mermaid(diagram_code)
65
+ st.image(image_path, caption="Generated Diagram", use_column_width=True)
66
+
67
+ # Provide download option
68
+ with open(image_path, "rb") as img_file:
69
+ st.download_button(
70
+ label="Download Diagram Image",
71
+ data=img_file,
72
+ file_name="diagram.png",
73
+ mime="image/png"
74
+ )
75
+
76
+ # Clean up the temporary files
77
+ os.remove(image_path)
78
+ except Exception as e:
79
+ st.error(f"Error generating diagram image: {e}")
80
+ else:
81
+ st.error("Failed to generate diagram code.")
82
+ else:
83
+ st.error("Please enter a prompt.")
84
+
85
+ if __name__ == "__main__":
86
+ main()