mery22 commited on
Commit
d0f636a
1 Parent(s): 3e67ebc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -53
app.py CHANGED
@@ -92,81 +92,139 @@ st.markdown('<p class="centered-orange-text">"Votre Réponse à Chaque Défi Mé
92
  user_input = st.text_input("You:", "")
93
  submit_button = st.button("Ask 📨")
94
 
95
- import os
96
- import streamlit as st
97
  import psycopg2
 
98
  from datetime import datetime
 
99
 
100
- # Function to create a connection to PostgreSQL
101
  def create_connection():
102
- return psycopg2.connect(
103
  host=os.getenv("DB_HOST"),
104
  database=os.getenv("DB_NAME"),
105
  user=os.getenv("DB_USER"),
106
  password=os.getenv("DB_PASSWORD"),
107
  port=os.getenv("DB_PORT")
108
  )
 
109
 
110
- # Function to create the feedback table if it doesn't exist
111
- def create_feedback_table(conn):
112
- cursor = conn.cursor()
113
- cursor.execute("""
114
  CREATE TABLE IF NOT EXISTS feedback (
115
  id SERIAL PRIMARY KEY,
116
- user_input TEXT NOT NULL,
117
- bot_response TEXT NOT NULL,
118
- rating INT CHECK (rating >= 1 AND rating <= 5),
119
- comment TEXT,
120
- timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
121
  );
122
- """)
123
- conn.commit()
124
- cursor.close()
125
-
126
- # Function to insert feedback into the database
127
- def insert_feedback(conn, user_input, bot_response, rating, comment):
128
- cursor = conn.cursor()
129
- cursor.execute(
130
- "INSERT INTO feedback (user_input, bot_response, rating, comment, timestamp) VALUES (%s, %s, %s, %s, %s)",
131
- (user_input, bot_response, rating, comment, datetime.now())
132
- )
133
- conn.commit()
134
- cursor.close()
135
 
136
- # Initialize connection and create the table if necessary
137
- conn = create_connection()
138
- create_feedback_table(conn)
 
139
 
140
- # Streamlit app UI and logic
141
- st.markdown("## Rate your experience")
142
 
143
- # Create a star-based rating system using radio buttons
144
- rating = st.radio(
145
- "Rating",
146
- options=[1, 2, 3, 4, 5],
147
- format_func=lambda x: "★" * x # Display stars based on the rating
148
- )
149
 
150
- # Text area for leaving a comment
151
- comment = st.text_area("Leave a comment")
152
 
153
- # Display bot response and user input for context
154
- st.markdown("### Your Question:")
155
- st.write(user_input)
156
- st.markdown("### Bot's Response:")
157
- st.write(bot_response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
- # Submit feedback
160
- if st.button("Submit Feedback"):
161
- if rating and comment:
162
- insert_feedback(conn, user_input, bot_response, rating, comment)
163
- st.success("Thank you for your feedback!")
164
- else:
165
- st.warning("Please provide a rating and a comment.")
166
 
167
- # Close the connection when done
168
- conn.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
 
 
170
 
171
  # Motivational quote at the bottom
172
  st.markdown("---")
 
92
  user_input = st.text_input("You:", "")
93
  submit_button = st.button("Ask 📨")
94
 
 
 
95
  import psycopg2
96
+ import streamlit as st
97
  from datetime import datetime
98
+ import os
99
 
100
+ # PostgreSQL connection setup using secrets from Hugging Face Spaces
101
  def create_connection():
102
+ conn = psycopg2.connect(
103
  host=os.getenv("DB_HOST"),
104
  database=os.getenv("DB_NAME"),
105
  user=os.getenv("DB_USER"),
106
  password=os.getenv("DB_PASSWORD"),
107
  port=os.getenv("DB_PORT")
108
  )
109
+ return conn
110
 
111
+ def create_table(conn):
112
+ with conn.cursor() as cur:
113
+ cur.execute('''
 
114
  CREATE TABLE IF NOT EXISTS feedback (
115
  id SERIAL PRIMARY KEY,
116
+ timestamp TIMESTAMP NOT NULL,
117
+ rating INTEGER NOT NULL,
118
+ comment TEXT NOT NULL
 
 
119
  );
120
+ ''')
121
+ conn.commit()
122
+
123
+ # Streamlit interface with improved aesthetics
124
+ st.set_page_config(page_title="Alter-IA Chat", page_icon="🤖")
 
 
 
 
 
 
 
 
125
 
126
+ # Define function to handle user input and display chatbot response
127
+ def chatbot_response(user_input):
128
+ response = qa.run(user_input)
129
+ return response
130
 
131
+ # Create columns for logos
132
+ col1, col2, col3 = st.columns([2, 3, 2])
133
 
134
+ with col1:
135
+ st.image("Design 3_22.png", width=150, use_column_width=True)
 
 
 
 
136
 
137
+ with col3:
138
+ st.image("Altereo logo 2023 original - eau et territoires durables.png", width=150, use_column_width=True)
139
 
140
+ # CSS for styling
141
+ st.markdown("""
142
+ <style>
143
+ .centered-text {
144
+ text-align: center;
145
+ }
146
+ .centered-orange-text {
147
+ text-align: center;
148
+ color: darkorange;
149
+ }
150
+ .star-rating {
151
+ display: flex;
152
+ flex-direction: row-reverse;
153
+ justify-content: center;
154
+ cursor: pointer;
155
+ }
156
+ .star-rating input[type="radio"] {
157
+ display: none;
158
+ }
159
+ .star-rating label {
160
+ font-size: 2em;
161
+ color: #ddd;
162
+ padding: 0 5px;
163
+ transition: color 0.3s;
164
+ }
165
+ .star-rating input[type="radio"]:checked ~ label {
166
+ color: gold;
167
+ }
168
+ .star-rating input[type="radio"]:hover ~ label {
169
+ color: gold;
170
+ }
171
+ </style>
172
+ """, unsafe_allow_html=True)
173
 
174
+ st.markdown('<h3 class="centered-text">🤖 AlteriaChat 🤖 </h3>', unsafe_allow_html=True)
175
+ st.markdown('<p class="centered-orange-text">"Votre Réponse à Chaque Défi Méthodologique "</p>', unsafe_allow_html=True)
176
+
177
+ # Input and button for user interaction
178
+ user_input = st.text_input("You:", "")
179
+ submit_button = st.button("Ask 📨")
 
180
 
181
+ if submit_button:
182
+ if user_input.strip() != "":
183
+ bot_response = chatbot_response(user_input)
184
+ st.markdown("### Bot:")
185
+ st.text_area("", value=bot_response, height=300)
186
+
187
+ # Add rating and comment section
188
+ st.markdown("---")
189
+ st.markdown("#### Rate the Response:")
190
+
191
+ # Custom star rating HTML
192
+ rating_html = """
193
+ <div class="star-rating">
194
+ <input type="radio" id="5-stars" name="rating" value="5"><label for="5-stars">★</label>
195
+ <input type="radio" id="4-stars" name="rating" value="4"><label for="4-stars">★</label>
196
+ <input type="radio" id="3-stars" name="rating" value="3" checked><label for="3-stars">★</label>
197
+ <input type="radio" id="2-stars" name="rating" value="2"><label for="2-stars">★</label>
198
+ <input type="radio" id="1-star" name="rating" value="1"><label for="1-star">★</label>
199
+ </div>
200
+ """
201
+ st.markdown(rating_html, unsafe_allow_html=True)
202
+
203
+ # Get the selected rating via JavaScript
204
+ rating = st.text_input("Selected Rating:", value="3", key="rating_input", label_visibility="hidden")
205
+
206
+ comment = st.text_area("Your Comment:")
207
+
208
+ # Submit feedback
209
+ feedback_button = st.button("Submit Feedback")
210
+
211
+ if feedback_button:
212
+ if comment.strip() == "":
213
+ st.warning("⚠ Please provide a comment.")
214
+ else:
215
+ st.success("Thank you for your feedback!")
216
+
217
+ # Store feedback in PostgreSQL
218
+ conn = create_connection()
219
+ with conn.cursor() as cur:
220
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
221
+ cur.execute('INSERT INTO feedback (timestamp, rating, comment) VALUES (%s, %s, %s)',
222
+ (timestamp, int(rating), comment))
223
+ conn.commit()
224
+ conn.close()
225
 
226
+ else:
227
+ st.warning("⚠ Please enter a message.")
228
 
229
  # Motivational quote at the bottom
230
  st.markdown("---")