halimbahae commited on
Commit
d2f3083
·
verified ·
1 Parent(s): 7ac7a5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -40
app.py CHANGED
@@ -96,16 +96,16 @@ def create_visualizations(df):
96
 
97
  return figures[0] if figures else None
98
 
99
- def respond(
100
  message,
101
- history: list[tuple[str, str]],
102
  system_message,
103
  max_tokens,
104
  temperature,
105
  top_p,
106
- chat_history_text=""
107
  ):
108
- """Enhanced response function with data analysis capabilities."""
109
  try:
110
  # Process chat history if provided
111
  if chat_history_text:
@@ -123,53 +123,53 @@ def respond(
123
  for field, count in top_fields.items():
124
  summary += f" • {field}: {count} researchers\n"
125
 
 
 
 
126
  # Add analysis to message
127
  message += f"\n\nCommunity Analysis:\n{summary}"
128
-
 
 
129
  # Generate response using the LLM
130
  messages = [{"role": "system", "content": system_message}]
131
- for val in history:
132
- if val[0]:
133
- messages.append({"role": "user", "content": val[0]})
134
- if val[1]:
135
- messages.append({"role": "assistant", "content": val[1]})
136
-
137
  messages.append({"role": "user", "content": message})
138
-
139
- response = ""
140
- for token in client.chat_completion(
141
  messages,
142
  max_tokens=max_tokens,
143
- stream=True,
144
  temperature=temperature,
145
  top_p=top_p,
146
- ):
147
- token_content = token.choices[0].delta.content
148
- response += token_content
149
- yield response
150
-
 
 
151
  except Exception as e:
152
- yield f"Error: {str(e)}"
 
 
153
 
154
- # Create Gradio interface
155
- demo = gr.Interface(
156
- fn=respond,
157
- inputs=[
158
- gr.Textbox(label="Message"),
159
- gr.State([]), # history
160
- gr.Textbox(value="You are a friendly Research Community Chatbot.", label="System message"),
161
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
162
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
163
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
164
- gr.Textbox(label="Chat History", lines=10)
165
- ],
166
- outputs=[
167
- gr.Textbox(label="Response"),
168
- gr.Plot(label="Community Analysis")
169
- ],
170
- title="CohortBot",
171
- description="A chatbot that analyzes research community data and provides visualizations."
172
- )
173
 
174
  if __name__ == "__main__":
175
  demo.launch()
 
96
 
97
  return figures[0] if figures else None
98
 
99
+ def process_message(
100
  message,
101
+ chat_history,
102
  system_message,
103
  max_tokens,
104
  temperature,
105
  top_p,
106
+ chat_history_text
107
  ):
108
+ """Process message and return response with analysis."""
109
  try:
110
  # Process chat history if provided
111
  if chat_history_text:
 
123
  for field, count in top_fields.items():
124
  summary += f" • {field}: {count} researchers\n"
125
 
126
+ # Create visualization
127
+ fig = create_visualizations(df)
128
+
129
  # Add analysis to message
130
  message += f"\n\nCommunity Analysis:\n{summary}"
131
+ else:
132
+ fig = None
133
+
134
  # Generate response using the LLM
135
  messages = [{"role": "system", "content": system_message}]
136
+ for user_msg, bot_msg in chat_history:
137
+ messages.append({"role": "user", "content": user_msg})
138
+ messages.append({"role": "assistant", "content": bot_msg})
 
 
 
139
  messages.append({"role": "user", "content": message})
140
+
141
+ response = client.chat_completion(
 
142
  messages,
143
  max_tokens=max_tokens,
 
144
  temperature=temperature,
145
  top_p=top_p,
146
+ )
147
+
148
+ bot_message = response.choices[0].message.content
149
+ chat_history.append((message, bot_message))
150
+
151
+ return chat_history, fig, chat_history
152
+
153
  except Exception as e:
154
+ error_message = f"Error: {str(e)}"
155
+ chat_history.append((message, error_message))
156
+ return chat_history, None, chat_history
157
 
158
+ with gr.Blocks(title="CohortBot") as demo:
159
+ chatbot = gr.Chatbot(label="Chat History")
160
+ msg = gr.Textbox(label="Message", placeholder="Type your message here...")
161
+ system_msg = gr.Textbox(value="You are a friendly Research Community Chatbot.", label="System message")
162
+ max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
163
+ temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
164
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
165
+ chat_history_text = gr.Textbox(label="Chat History for Analysis", lines=10)
166
+ plot = gr.Plot(label="Community Analysis")
167
+
168
+ msg.submit(
169
+ process_message,
170
+ [msg, chatbot, system_msg, max_tokens, temperature, top_p, chat_history_text],
171
+ [chatbot, plot, chatbot]
172
+ )
 
 
 
 
173
 
174
  if __name__ == "__main__":
175
  demo.launch()