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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -87
app.py CHANGED
@@ -68,34 +68,7 @@ qa = RetrievalQA.from_chain_type(
68
  chain_type_kwargs={"prompt": prompt},
69
  )
70
 
71
- # PostgreSQL connection setup using secrets from Hugging Face Spaces
72
- def create_connection():
73
- return psycopg2.connect(
74
- host=os.getenv("DB_HOST"),
75
- database=os.getenv("DB_NAME"),
76
- user=os.getenv("DB_USER"),
77
- password=os.getenv("DB_PASSWORD"),
78
- port=os.getenv("DB_PORT")
79
- )
80
 
81
- def insert_feedback(rating, comment):
82
- try:
83
- conn = create_connection()
84
- with conn.cursor() as cur:
85
- timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
86
- cur.execute('INSERT INTO feedback (timestamp, rating, comment) VALUES (%s, %s, %s)',
87
- (timestamp, int(rating), comment))
88
- conn.commit()
89
- return True
90
- except Exception as e:
91
- st.error(f"Error inserting feedback: {e}")
92
- return False
93
- finally:
94
- if conn:
95
- conn.close()
96
-
97
- # Streamlit interface with improved aesthetics
98
- st.set_page_config(page_title="Alter-IA Chat", page_icon="🤖")
99
 
100
  def chatbot_response(user_input):
101
  response = qa.run(user_input)
@@ -110,39 +83,7 @@ with col1:
110
  with col3:
111
  st.image("Altereo logo 2023 original - eau et territoires durables.png", width=150, use_column_width=True)
112
 
113
- # CSS for styling
114
- st.markdown("""
115
- <style>
116
- .centered-text {
117
- text-align: center;
118
- }
119
- .centered-orange-text {
120
- text-align: center;
121
- color: darkorange;
122
- }
123
- .star-rating {
124
- display: flex;
125
- flex-direction: row-reverse;
126
- justify-content: center;
127
- cursor: pointer;
128
- }
129
- .star-rating input[type="radio"] {
130
- display: none;
131
- }
132
- .star-rating label {
133
- font-size: 2em;
134
- color: #ddd;
135
- padding: 0 5px;
136
- transition: color 0.3s;
137
- }
138
- .star-rating input[type="radio"]:checked ~ label {
139
- color: gold;
140
- }
141
- .star-rating input[type="radio"]:hover ~ label {
142
- color: gold;
143
- }
144
- </style>
145
- """, unsafe_allow_html=True)
146
 
147
  st.markdown('<h3 class="centered-text">🤖 AlteriaChat 🤖 </h3>', unsafe_allow_html=True)
148
  st.markdown('<p class="centered-orange-text">"Votre Réponse à Chaque Défi Méthodologique "</p>', unsafe_allow_html=True)
@@ -151,34 +92,81 @@ st.markdown('<p class="centered-orange-text">"Votre Réponse à Chaque Défi Mé
151
  user_input = st.text_input("You:", "")
152
  submit_button = st.button("Ask 📨")
153
 
154
- if submit_button:
155
- if user_input.strip() != "":
156
- bot_response = chatbot_response(user_input)
157
- st.markdown("### Bot:")
158
- st.text_area("", value=bot_response, height=300)
159
-
160
- # Add rating and comment section
161
- st.markdown("---")
162
- st.markdown("#### Rate the Response:")
163
-
164
- # Use Streamlit form for feedback
165
- with st.form(key='feedback_form'):
166
- rating = st.select_slider("Rating:", options=[1, 2, 3, 4, 5], value=3)
167
- comment = st.text_area("Your Comment:")
168
-
169
- submit_feedback_button = st.form_submit_button("Submit Feedback")
170
-
171
- if submit_feedback_button:
172
- if comment.strip() == "":
173
- st.warning("⚠ Please provide a comment.")
174
- else:
175
- # Store feedback in PostgreSQL
176
- if insert_feedback(rating, comment):
177
- st.success("Thank you for your feedback!")
178
- else:
179
- st.error("Failed to submit feedback.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  else:
181
- st.warning("Please enter a message.")
 
 
 
 
182
 
183
  # Motivational quote at the bottom
184
  st.markdown("---")
 
68
  chain_type_kwargs={"prompt": prompt},
69
  )
70
 
 
 
 
 
 
 
 
 
 
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  def chatbot_response(user_input):
74
  response = qa.run(user_input)
 
83
  with col3:
84
  st.image("Altereo logo 2023 original - eau et territoires durables.png", width=150, use_column_width=True)
85
 
86
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  st.markdown('<h3 class="centered-text">🤖 AlteriaChat 🤖 </h3>', unsafe_allow_html=True)
89
  st.markdown('<p class="centered-orange-text">"Votre Réponse à Chaque Défi Méthodologique "</p>', unsafe_allow_html=True)
 
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("---")