Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -137,7 +137,6 @@ def create_app():
|
|
| 137 |
gr.Markdown("Generate customer personas through AI-powered interviews")
|
| 138 |
|
| 139 |
api_key_state = gr.State("")
|
| 140 |
-
client_state = gr.State(None)
|
| 141 |
questions_state = gr.State([])
|
| 142 |
current_q_index = gr.State(0)
|
| 143 |
interview_data = gr.State([])
|
|
@@ -154,16 +153,23 @@ def create_app():
|
|
| 154 |
setup_status = gr.Textbox(label="Status", interactive=False)
|
| 155 |
|
| 156 |
def initialize_system(api_key):
|
|
|
|
|
|
|
| 157 |
try:
|
| 158 |
client = Groq(api_key=api_key)
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
except Exception as e:
|
| 161 |
-
return f"Error: {str(e)}", ""
|
| 162 |
|
| 163 |
setup_btn.click(
|
| 164 |
initialize_system,
|
| 165 |
inputs=[api_key_input],
|
| 166 |
-
outputs=[setup_status, api_key_state
|
| 167 |
)
|
| 168 |
|
| 169 |
with gr.Tab("Input Query"):
|
|
@@ -180,17 +186,21 @@ def create_app():
|
|
| 180 |
generate_btn = gr.Button("Generate Questions", variant="primary")
|
| 181 |
questions_output = gr.JSON(label="Generated Questions")
|
| 182 |
|
| 183 |
-
def generate_questions(
|
| 184 |
-
if
|
| 185 |
return {"error": "Please initialize system first"}, query, []
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
generate_btn.click(
|
| 192 |
generate_questions,
|
| 193 |
-
inputs=[
|
| 194 |
outputs=[questions_output, query_state, questions_state]
|
| 195 |
)
|
| 196 |
|
|
@@ -207,25 +217,30 @@ def create_app():
|
|
| 207 |
finish_interview_btn = gr.Button("Finish This Interview", variant="secondary")
|
| 208 |
interview_status = gr.Textbox(label="Status", interactive=False)
|
| 209 |
|
| 210 |
-
def start_next_question(
|
| 211 |
-
if
|
| 212 |
return history, "Initialize system first", current_idx, interview_hist
|
| 213 |
|
| 214 |
if current_idx >= len(questions):
|
| 215 |
return history, "Interview complete. Click 'Finish This Interview'", current_idx, interview_hist
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
def submit_answer(answer, questions, current_idx, history, interview_hist):
|
| 225 |
if not answer:
|
| 226 |
return history, "Please provide an answer", current_idx, interview_hist, ""
|
| 227 |
|
| 228 |
-
history
|
|
|
|
| 229 |
|
| 230 |
interview_hist.append({
|
| 231 |
'q': questions[current_idx]['question'],
|
|
@@ -235,7 +250,7 @@ def create_app():
|
|
| 235 |
|
| 236 |
new_idx = current_idx + 1
|
| 237 |
|
| 238 |
-
return history,
|
| 239 |
|
| 240 |
def finish_interview(all_interviews, interview_hist, interview_number):
|
| 241 |
if interview_hist:
|
|
@@ -252,12 +267,8 @@ def create_app():
|
|
| 252 |
|
| 253 |
next_btn.click(
|
| 254 |
start_next_question,
|
| 255 |
-
inputs=[
|
| 256 |
outputs=[chatbot, interview_status, current_q_index, interview_data]
|
| 257 |
-
).then(
|
| 258 |
-
lambda: None,
|
| 259 |
-
None,
|
| 260 |
-
None
|
| 261 |
)
|
| 262 |
|
| 263 |
customer_answer.submit(
|
|
@@ -277,20 +288,24 @@ def create_app():
|
|
| 277 |
generate_persona_btn = gr.Button("Generate Persona from All Interviews", variant="primary")
|
| 278 |
persona_output = gr.JSON(label="Customer Persona")
|
| 279 |
|
| 280 |
-
def generate_persona(
|
| 281 |
-
if
|
| 282 |
return {"error": "Initialize system first"}
|
| 283 |
|
| 284 |
if not all_interviews:
|
| 285 |
return {"error": "No interviews completed"}
|
| 286 |
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
generate_persona_btn.click(
|
| 292 |
generate_persona,
|
| 293 |
-
inputs=[
|
| 294 |
outputs=[persona_output]
|
| 295 |
)
|
| 296 |
|
|
|
|
| 137 |
gr.Markdown("Generate customer personas through AI-powered interviews")
|
| 138 |
|
| 139 |
api_key_state = gr.State("")
|
|
|
|
| 140 |
questions_state = gr.State([])
|
| 141 |
current_q_index = gr.State(0)
|
| 142 |
interview_data = gr.State([])
|
|
|
|
| 153 |
setup_status = gr.Textbox(label="Status", interactive=False)
|
| 154 |
|
| 155 |
def initialize_system(api_key):
|
| 156 |
+
if not api_key:
|
| 157 |
+
return "Please enter API key", ""
|
| 158 |
try:
|
| 159 |
client = Groq(api_key=api_key)
|
| 160 |
+
test = client.chat.completions.create(
|
| 161 |
+
model="llama-3.3-70b-versatile",
|
| 162 |
+
messages=[{"role": "user", "content": "test"}],
|
| 163 |
+
max_completion_tokens=10
|
| 164 |
+
)
|
| 165 |
+
return "System initialized successfully", api_key
|
| 166 |
except Exception as e:
|
| 167 |
+
return f"Error: {str(e)}", ""
|
| 168 |
|
| 169 |
setup_btn.click(
|
| 170 |
initialize_system,
|
| 171 |
inputs=[api_key_input],
|
| 172 |
+
outputs=[setup_status, api_key_state]
|
| 173 |
)
|
| 174 |
|
| 175 |
with gr.Tab("Input Query"):
|
|
|
|
| 186 |
generate_btn = gr.Button("Generate Questions", variant="primary")
|
| 187 |
questions_output = gr.JSON(label="Generated Questions")
|
| 188 |
|
| 189 |
+
def generate_questions(api_key, query, context):
|
| 190 |
+
if not api_key:
|
| 191 |
return {"error": "Please initialize system first"}, query, []
|
| 192 |
|
| 193 |
+
try:
|
| 194 |
+
client = Groq(api_key=api_key)
|
| 195 |
+
agent = QuestionGeneratorAgent(client)
|
| 196 |
+
result = agent.generate(query, context)
|
| 197 |
+
return result, query, result.get('questions', [])
|
| 198 |
+
except Exception as e:
|
| 199 |
+
return {"error": str(e)}, query, []
|
| 200 |
|
| 201 |
generate_btn.click(
|
| 202 |
generate_questions,
|
| 203 |
+
inputs=[api_key_state, query_input, context_input],
|
| 204 |
outputs=[questions_output, query_state, questions_state]
|
| 205 |
)
|
| 206 |
|
|
|
|
| 217 |
finish_interview_btn = gr.Button("Finish This Interview", variant="secondary")
|
| 218 |
interview_status = gr.Textbox(label="Status", interactive=False)
|
| 219 |
|
| 220 |
+
def start_next_question(api_key, questions, current_idx, history, interview_hist):
|
| 221 |
+
if not api_key:
|
| 222 |
return history, "Initialize system first", current_idx, interview_hist
|
| 223 |
|
| 224 |
if current_idx >= len(questions):
|
| 225 |
return history, "Interview complete. Click 'Finish This Interview'", current_idx, interview_hist
|
| 226 |
|
| 227 |
+
try:
|
| 228 |
+
client = Groq(api_key=api_key)
|
| 229 |
+
conductor = InterviewConductorAgent(client)
|
| 230 |
+
q = questions[current_idx]
|
| 231 |
+
agent_q = conductor.ask_question(q)
|
| 232 |
+
|
| 233 |
+
history.append((agent_q, None))
|
| 234 |
+
return history, f"Question {current_idx + 1}/{len(questions)}", current_idx, interview_hist
|
| 235 |
+
except Exception as e:
|
| 236 |
+
return history, f"Error: {str(e)}", current_idx, interview_hist
|
| 237 |
|
| 238 |
def submit_answer(answer, questions, current_idx, history, interview_hist):
|
| 239 |
if not answer:
|
| 240 |
return history, "Please provide an answer", current_idx, interview_hist, ""
|
| 241 |
|
| 242 |
+
if history and history[-1][1] is None:
|
| 243 |
+
history[-1] = (history[-1][0], answer)
|
| 244 |
|
| 245 |
interview_hist.append({
|
| 246 |
'q': questions[current_idx]['question'],
|
|
|
|
| 250 |
|
| 251 |
new_idx = current_idx + 1
|
| 252 |
|
| 253 |
+
return history, "Answer recorded", new_idx, interview_hist, ""
|
| 254 |
|
| 255 |
def finish_interview(all_interviews, interview_hist, interview_number):
|
| 256 |
if interview_hist:
|
|
|
|
| 267 |
|
| 268 |
next_btn.click(
|
| 269 |
start_next_question,
|
| 270 |
+
inputs=[api_key_state, questions_state, current_q_index, chatbot, interview_data],
|
| 271 |
outputs=[chatbot, interview_status, current_q_index, interview_data]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
)
|
| 273 |
|
| 274 |
customer_answer.submit(
|
|
|
|
| 288 |
generate_persona_btn = gr.Button("Generate Persona from All Interviews", variant="primary")
|
| 289 |
persona_output = gr.JSON(label="Customer Persona")
|
| 290 |
|
| 291 |
+
def generate_persona(api_key, all_interviews, query):
|
| 292 |
+
if not api_key:
|
| 293 |
return {"error": "Initialize system first"}
|
| 294 |
|
| 295 |
if not all_interviews:
|
| 296 |
return {"error": "No interviews completed"}
|
| 297 |
|
| 298 |
+
try:
|
| 299 |
+
client = Groq(api_key=api_key)
|
| 300 |
+
synthesizer = SynthesisAgent(client)
|
| 301 |
+
persona = synthesizer.aggregate_interviews(all_interviews, query)
|
| 302 |
+
return persona
|
| 303 |
+
except Exception as e:
|
| 304 |
+
return {"error": str(e)}
|
| 305 |
|
| 306 |
generate_persona_btn.click(
|
| 307 |
generate_persona,
|
| 308 |
+
inputs=[api_key_state, all_interviews_data, query_state],
|
| 309 |
outputs=[persona_output]
|
| 310 |
)
|
| 311 |
|