shukdevdatta123 commited on
Commit
9ed03e2
·
verified ·
1 Parent(s): df2f6f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -7
app.py CHANGED
@@ -13,7 +13,7 @@ def get_diagram_code(prompt, diagram_type, api_key):
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
  ],
@@ -38,18 +38,83 @@ def convert_svg_to_png(svg_file):
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
@@ -66,11 +131,20 @@ def main():
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
 
@@ -85,7 +159,6 @@ def main():
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:
 
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
  ],
 
38
  st.error(f"Error during SVG to PNG conversion: {e}")
39
  return None
40
 
41
+ # Adding custom CSS
42
+ def custom_css():
43
+ st.markdown("""
44
+ <style>
45
+ body {
46
+ background-color: #f0f4f8;
47
+ font-family: 'Roboto', sans-serif;
48
+ }
49
+ .stButton>button {
50
+ background-color: #4CAF50;
51
+ color: white;
52
+ font-size: 18px;
53
+ padding: 12px 24px;
54
+ border-radius: 8px;
55
+ transition: background-color 0.3s ease;
56
+ }
57
+ .stButton>button:hover {
58
+ background-color: #45a049;
59
+ }
60
+ .stTextInput, .stTextArea {
61
+ background-color: #fff;
62
+ border: 2px solid #ddd;
63
+ border-radius: 8px;
64
+ padding: 12px;
65
+ font-size: 16px;
66
+ transition: border 0.3s ease;
67
+ }
68
+ .stTextInput:focus, .stTextArea:focus {
69
+ border-color: #4CAF50;
70
+ }
71
+ .stImage {
72
+ margin-bottom: 20px;
73
+ border-radius: 10px;
74
+ }
75
+ h1 {
76
+ color: #333;
77
+ text-align: center;
78
+ font-size: 36px;
79
+ }
80
+ .expandable-section {
81
+ margin-top: 20px;
82
+ }
83
+ iframe {
84
+ border: 0;
85
+ border-radius: 10px;
86
+ }
87
+ .loading-spinner {
88
+ display: none;
89
+ animation: spin 2s infinite linear;
90
+ border: 6px solid #f3f3f3;
91
+ border-top: 6px solid #3498db;
92
+ border-radius: 50%;
93
+ width: 50px;
94
+ height: 50px;
95
+ margin: 0 auto;
96
+ }
97
+ @keyframes spin {
98
+ 0% { transform: rotate(0deg); }
99
+ 100% { transform: rotate(360deg); }
100
+ }
101
+ </style>
102
+ """, unsafe_allow_html=True)
103
+
104
  # Streamlit App UI
105
  def main():
106
+ custom_css() # Apply custom CSS
107
+
108
  st.title("Generate Diagrams using AI and MermaidFlow")
109
+
110
+ # Display a logo or icon with subtle animation
111
  image = Image.open("11.png") # Path to your image file
112
+ st.image(image, width=300, use_column_width=False)
113
 
114
  # Display sample prompt
115
+ with st.expander("Sample Prompt For Users", expanded=True):
116
  st.markdown("""
117
+ 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.
118
  """)
119
 
120
  # User input for OpenAI API key
 
131
  diagram_types = ["UML Diagram", "ER Diagram", "State Diagram", "Class Diagram", "Sequence Diagram"]
132
  diagram_choice = st.selectbox("Select the type of diagram to generate:", diagram_types)
133
 
134
+ # Show loading spinner when generating the diagram
135
+ loading_spinner = st.empty()
136
+
137
  if st.button("Generate Diagram"):
138
  if prompt:
139
+ # Show loading animation
140
+ loading_spinner.markdown("<div class='loading-spinner'></div>", unsafe_allow_html=True)
141
+
142
  diagram_code = get_diagram_code(prompt, diagram_choice, api_key)
143
 
144
  if diagram_code:
145
+ # Hide the spinner after diagram code is generated
146
+ loading_spinner.empty()
147
+
148
  # Render Mermaid code to Streamlit
149
  st.code(diagram_code, language='mermaid')
150
 
 
159
 
160
  # Provide a direct link to MermaidFlow editor for convenience
161
  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.")
 
162
  else:
163
  st.error("Failed to generate diagram code.")
164
  else: