Spaces:
Running
Running
Update ext.txt
Browse files
ext.txt
CHANGED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
import urllib.parse
|
4 |
+
import io
|
5 |
+
from PIL import Image
|
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 |
+
# For GPT-4 and other chat models, use the v1/chat/completions endpoint
|
14 |
+
response = openai.ChatCompletion.create(
|
15 |
+
model="gpt-4o", # Make sure you're using the right model
|
16 |
+
messages=[
|
17 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
18 |
+
{"role": "user", "content": f"Generate a {diagram_type} diagram in Mermaid.js syntax based on the following prompt: {prompt}"}
|
19 |
+
],
|
20 |
+
max_tokens=500
|
21 |
+
)
|
22 |
+
|
23 |
+
return response['choices'][0]['message']['content'].strip()
|
24 |
+
|
25 |
+
except Exception as e:
|
26 |
+
st.error(f"Error: {e}")
|
27 |
+
return None
|
28 |
+
|
29 |
+
# Function to convert SVG to PNG
|
30 |
+
def convert_svg_to_png(svg_file):
|
31 |
+
try:
|
32 |
+
# Read the uploaded SVG file and convert it to PNG using cairosvg
|
33 |
+
png_output = io.BytesIO()
|
34 |
+
cairosvg.svg2png(file_obj=svg_file, write_to=png_output)
|
35 |
+
png_output.seek(0)
|
36 |
+
return png_output
|
37 |
+
except Exception as e:
|
38 |
+
st.error(f"Error during SVG to PNG conversion: {e}")
|
39 |
+
return None
|
40 |
+
|
41 |
+
# Streamlit App UI
|
42 |
+
def main():
|
43 |
+
st.title("Generate Diagrams using AI and MermaidFlow")
|
44 |
+
|
45 |
+
# Display a logo or icon
|
46 |
+
image = Image.open("11.png") # Path to your image file
|
47 |
+
st.image(image, width=300) # You can adjust the width as needed
|
48 |
+
|
49 |
+
# Display sample prompt
|
50 |
+
with st.expander("Sample Prompt For Users"):
|
51 |
+
st.markdown("""
|
52 |
+
Create a UML diagram for a **Library Management System** that includes classes such as **Book** (with attributes like `bookID`, `title`, `author`, `publisher`, `genre`, `availabilityStatus` and methods like `checkAvailability()`, `updateAvailability()`), **Member** (with attributes such as `memberID`, `name`, `email`, `phoneNumber`, `membershipDate` and methods like `borrowBook()`, `returnBook()`, `reserveBook()`), **Staff** (with attributes such as `staffID`, `name`, `role`, `email`, `phoneNumber` and methods like `addBook()`, `removeBook()`, `updateBookInfo()`), **Transaction** (with attributes like `transactionID`, `transactionDate`, `dueDate`, `fineAmount` and methods like `calculateFine()`, `generateReceipt()`), and **Reservation** (with attributes like `reservationID`, `reservationDate`, `expirationDate` and methods like `cancelReservation()`, `checkReservationStatus()`). Define the relationships where a **Member** can borrow many **Books**, a **Staff** can manage many **Books**, a **Transaction** is linked to one **Book** and one **Member**, and a **Reservation** is made by a **Member** for a **Book**. Ensure appropriate visibility markers for methods and attributes, and include necessary relationships such as associations, multiplicities, and inheritance. Optionally, consider adding sequence or activity diagrams for processes like book borrowing.
|
53 |
+
""")
|
54 |
+
|
55 |
+
# User input for OpenAI API key
|
56 |
+
api_key = st.text_input("Enter your OpenAI API Key:", type="password")
|
57 |
+
|
58 |
+
# Check if API key is provided
|
59 |
+
if not api_key:
|
60 |
+
st.warning("Please enter your OpenAI API Key to continue.")
|
61 |
+
return
|
62 |
+
|
63 |
+
# User prompt for diagram type
|
64 |
+
prompt = st.text_area("Enter your prompt for the diagram:", "")
|
65 |
+
|
66 |
+
diagram_types = ["UML Diagram", "ER Diagram", "State Diagram", "Class Diagram", "Sequence Diagram"]
|
67 |
+
diagram_choice = st.selectbox("Select the type of diagram to generate:", diagram_types)
|
68 |
+
|
69 |
+
if st.button("Generate Diagram"):
|
70 |
+
if prompt:
|
71 |
+
diagram_code = get_diagram_code(prompt, diagram_choice, api_key)
|
72 |
+
|
73 |
+
if diagram_code:
|
74 |
+
# Render Mermaid code to Streamlit
|
75 |
+
st.code(diagram_code, language='mermaid')
|
76 |
+
|
77 |
+
# Encode the Mermaid code for use in the MermaidFlow editor URL
|
78 |
+
encoded_code = urllib.parse.quote(diagram_code) # URL encode the Mermaid code
|
79 |
+
|
80 |
+
# MermaidFlow Editor URL with the encoded Mermaid code
|
81 |
+
mermaidflow_url = f"https://www.mermaidflow.app/editor?code={encoded_code}"
|
82 |
+
|
83 |
+
# Embed MermaidFlow Editor in iframe
|
84 |
+
st.markdown(f'<iframe src="{mermaidflow_url}" width="100%" height="600px"></iframe>', unsafe_allow_html=True)
|
85 |
+
|
86 |
+
# Provide a direct link to MermaidFlow editor for convenience
|
87 |
+
st.markdown(f"Copy and paste the Generated Mermaid Code in the Live Editor and download the generated diagram by clicking on Export as SVG format to get a high-resolution generated image.")
|
88 |
+
|
89 |
+
else:
|
90 |
+
st.error("Failed to generate diagram code.")
|
91 |
+
else:
|
92 |
+
st.error("Please enter a prompt.")
|
93 |
+
|
94 |
+
if __name__ == "__main__":
|
95 |
+
main()
|