chukbert commited on
Commit
9b22ed0
·
verified ·
1 Parent(s): 3a532c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -61,47 +61,67 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
61
 
62
  start_time = time.time()
63
 
 
64
  if os.path.exists('faiss.index') and os.path.exists('embeddings.pkl'):
65
  log_output.write("Loading FAISS index from disk...\n")
66
  faiss_index, question_embeddings = load_faiss_index('faiss.index', 'embeddings.pkl')
 
67
  else:
68
  log_output.write("Creating and saving FAISS index...\n")
69
 
70
  embedding_model = OpenAIEmbeddings(openai_api_key=openai.api_key)
71
  faiss_index, question_embeddings = create_and_save_faiss_index(questions, embedding_model, 'faiss.index', 'embeddings.pkl')
 
72
 
 
 
 
 
 
73
  messages = [{"role": "system", "content": system_message}]
74
  for user_message, bot_response in history:
75
  messages.append({"role": "user", "content": user_message})
76
  if bot_response:
77
  messages.append({"role": "assistant", "content": bot_response})
78
 
 
79
  user_message = message
80
  messages.append({"role": "user", "content": user_message})
81
 
82
- response_text = retrieve_answer(user_message, faiss_index, OpenAIEmbeddings(openai_api_key=openai.api_key), answers=["..."], threshold=0.35)
 
83
 
84
- if response_text == "No good match found in dataset. Using GPT-4o-mini to generate an answer.":
85
- log_output.write("No good match found in dataset. Using GPT-4o-mini to generate an answer.\n")
86
  response_text = ask_openai_gpt4(user_message)
87
 
88
  # Stop the timer and calculate response time
89
  end_time = time.time()
90
  response_time = end_time - start_time # Time in seconds
91
 
92
- # Yield the response with the logs and response time
93
- yield response_text, f"Response time: {response_time:.4f} seconds", log_output.getvalue()
94
 
95
 
96
  # Gradio ChatInterface with additional inputs for model settings and response time
97
  demo = gr.ChatInterface(
98
  fn=respond,
 
 
 
 
 
 
99
  title="Medical Chatbot with Customizable Parameters and Response Time",
100
- description="A chatbot with customizable parameters using FAISS for quick responses or fallback to GPT-4 when no relevant answer is found. Response time is also tracked."
 
101
  )
102
 
103
  if __name__ == "__main__":
 
104
  df = pd.read_csv("medquad.csv")
105
  questions = df['question'].tolist()
106
  answers = df['answer'].tolist()
107
- demo.launch(cache_examples=False)
 
 
 
61
 
62
  start_time = time.time()
63
 
64
+ # Debugging - Ensure that FAISS index and embeddings are correctly loaded
65
  if os.path.exists('faiss.index') and os.path.exists('embeddings.pkl'):
66
  log_output.write("Loading FAISS index from disk...\n")
67
  faiss_index, question_embeddings = load_faiss_index('faiss.index', 'embeddings.pkl')
68
+ print(f"FAISS index and embeddings loaded successfully. Number of embeddings: {len(question_embeddings)}")
69
  else:
70
  log_output.write("Creating and saving FAISS index...\n")
71
 
72
  embedding_model = OpenAIEmbeddings(openai_api_key=openai.api_key)
73
  faiss_index, question_embeddings = create_and_save_faiss_index(questions, embedding_model, 'faiss.index', 'embeddings.pkl')
74
+ print(f"Created new FAISS index. Number of questions: {len(questions)}")
75
 
76
+ # Debugging - Ensure questions and answers lists are valid
77
+ print(f"questions list length: {len(questions)}") # Debugging print
78
+ print(f"answers list length: {len(answers)}") # Debugging print
79
+
80
+ # Prepare message history
81
  messages = [{"role": "system", "content": system_message}]
82
  for user_message, bot_response in history:
83
  messages.append({"role": "user", "content": user_message})
84
  if bot_response:
85
  messages.append({"role": "assistant", "content": bot_response})
86
 
87
+ # Add the current user message
88
  user_message = message
89
  messages.append({"role": "user", "content": user_message})
90
 
91
+ # Retrieve answer from FAISS or fallback to GPT-4
92
+ response_text = retrieve_answer(user_message, faiss_index, OpenAIEmbeddings(openai_api_key=openai.api_key), answers, threshold=0.35)
93
 
94
+ if response_text == "No good match found in dataset. Using GPT-4 to generate an answer.":
95
+ log_output.write("No good match found in dataset. Using GPT-4 to generate an answer.\n")
96
  response_text = ask_openai_gpt4(user_message)
97
 
98
  # Stop the timer and calculate response time
99
  end_time = time.time()
100
  response_time = end_time - start_time # Time in seconds
101
 
102
+ # Return the response, response time, and logs
103
+ return response_text, f"Response time: {response_time:.4f} seconds", log_output.getvalue()
104
 
105
 
106
  # Gradio ChatInterface with additional inputs for model settings and response time
107
  demo = gr.ChatInterface(
108
  fn=respond,
109
+ additional_inputs=[
110
+ gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
111
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
112
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
113
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
114
+ ],
115
  title="Medical Chatbot with Customizable Parameters and Response Time",
116
+ description="A chatbot with customizable parameters using FAISS for quick responses or fallback to GPT-4 when no relevant answer is found. Response time is also tracked.",
117
+ type='messages' # Set type to 'messages' instead of 'tuples'
118
  )
119
 
120
  if __name__ == "__main__":
121
+ # Load dataset
122
  df = pd.read_csv("medquad.csv")
123
  questions = df['question'].tolist()
124
  answers = df['answer'].tolist()
125
+
126
+ print(f"Loaded questions and answers. Number of questions: {len(questions)}, Number of answers: {len(answers)}")
127
+ demo.launch()