Spaces:
Sleeping
Sleeping
halimbahae
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -96,16 +96,16 @@ def create_visualizations(df):
|
|
96 |
|
97 |
return figures[0] if figures else None
|
98 |
|
99 |
-
def
|
100 |
message,
|
101 |
-
|
102 |
system_message,
|
103 |
max_tokens,
|
104 |
temperature,
|
105 |
top_p,
|
106 |
-
chat_history_text
|
107 |
):
|
108 |
-
"""
|
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
|
132 |
-
|
133 |
-
|
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 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
|
|
151 |
except Exception as e:
|
152 |
-
|
|
|
|
|
153 |
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
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()
|