Dorcatz123 commited on
Commit
3b61851
·
verified ·
1 Parent(s): ab2e242

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -16
app.py CHANGED
@@ -217,27 +217,38 @@ def ask_cancer_question(question, session_id="user_session"):
217
 
218
  # return chat_loop
219
 
220
- def interactive_chat(question,api_key):
221
- os.environ["OPENAI_API_KEY"]=api_key
222
- chat_history = ""
223
 
224
- # Inside chat_loop
 
 
 
 
 
 
225
  if question.lower() == "exit":
226
- return chat_history + "\nExiting the interactive question loop."
227
-
 
228
  response = ask_cancer_question(question, session_id)
229
- chat_history += f"\nUser: {question}\nBot: {response}"
230
- return chat_history
231
 
232
- demo = gr.Interface(
233
- fn=interactive_chat,
234
- inputs=[gr.Textbox(lines=2, placeholder="Ask a cancer-related question...", elem_id="query_input"),
235
- gr.Textbox(label="Enter Open_AI_API Key",placeholder="Enter OpenAI API Key",type="password",lines=1,elem_id="openai_input")],
236
- outputs=gr.Textbox(lines=10, elem_id="Output"),
237
- title="Cancer_Researcher_AI",
238
- description="Ask cancer-related questions and receive responses based on relevant research."
239
- )
240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
 
242
 
243
  title="Cancer_Researcher_AI",
 
217
 
218
  # return chat_loop
219
 
220
+ def interactive_chat(question, api_key):
221
+ global chat_history # Maintain history across function calls
 
222
 
223
+ # Set OpenAI API Key (only if it's not set already)
224
+ if "OPENAI_API_KEY" not in os.environ:
225
+ os.environ["OPENAI_API_KEY"] = api_key
226
+
227
+ session_id = "user_session" # Define session ID
228
+
229
+ # Exit condition
230
  if question.lower() == "exit":
231
+ return "\n".join(chat_history) + "\nExiting the interactive question loop."
232
+
233
+ # Call cancer question function
234
  response = ask_cancer_question(question, session_id)
 
 
235
 
236
+ # Store conversation
237
+ chat_history.append(f"User: {question}\nBot: {response}")
 
 
 
 
 
 
238
 
239
+ return "\n".join(chat_history) # Return full chat history
240
+
241
+ # 🎨 Gradio UI
242
+ demo = gr.Interface(
243
+ fn=interactive_chat,
244
+ inputs=[
245
+ gr.Textbox(lines=2, placeholder="Ask a cancer-related question...", elem_id="query_input"),
246
+ gr.Textbox(label="🔑 Enter OpenAI API Key", placeholder="Enter OpenAI API Key", type="password", lines=1, elem_id="openai_input"),
247
+ ],
248
+ outputs=gr.Textbox(lines=10, elem_id="Output"),
249
+ title="🧬 Cancer Research Chatbot",
250
+ description="Ask cancer-related questions and receive responses based on relevant research."
251
+ )
252
 
253
 
254
  title="Cancer_Researcher_AI",