Spaces:
Sleeping
Sleeping
halimbahae
commited on
Commit
•
3938c4d
1
Parent(s):
d2f3083
Update app.py
Browse files
app.py
CHANGED
@@ -96,41 +96,43 @@ def create_visualizations(df):
|
|
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
|
109 |
try:
|
110 |
-
# Process chat history if provided
|
111 |
-
if chat_history_text:
|
112 |
-
df = create_researcher_df(chat_history_text)
|
113 |
-
|
114 |
-
# Generate analysis summary
|
115 |
-
summary = f"Analysis of {len(df)} researchers:\n"
|
116 |
-
if 'affiliation' in df.columns:
|
117 |
-
summary += f"- Institutions represented: {df['affiliation'].nunique()}\n"
|
118 |
-
|
119 |
-
field_counts = analyze_research_fields(df)
|
120 |
-
if not field_counts.empty:
|
121 |
-
top_fields = field_counts.nlargest(3)
|
122 |
-
summary += "- Top research fields:\n"
|
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:
|
@@ -148,27 +150,45 @@ def process_message(
|
|
148 |
bot_message = response.choices[0].message.content
|
149 |
chat_history.append((message, bot_message))
|
150 |
|
151 |
-
return 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
|
157 |
|
158 |
with gr.Blocks(title="CohortBot") as demo:
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
msg.submit(
|
169 |
process_message,
|
170 |
-
[msg, chatbot, system_msg, max_tokens, temperature, top_p
|
171 |
-
[chatbot
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
)
|
173 |
|
174 |
if __name__ == "__main__":
|
|
|
96 |
|
97 |
return figures[0] if figures else None
|
98 |
|
99 |
+
def analyze_chat_history(chat_history_text):
|
100 |
+
"""Analyze chat history and return DataFrame, plot, and summary."""
|
101 |
+
if not chat_history_text.strip():
|
102 |
+
return None, None, "No chat history provided."
|
103 |
+
|
104 |
+
df = create_researcher_df(chat_history_text)
|
105 |
+
|
106 |
+
if df.empty:
|
107 |
+
return None, None, "No data could be extracted from the chat history."
|
108 |
+
|
109 |
+
# Generate analysis summary
|
110 |
+
summary = f"Analysis of {len(df)} researchers:\n"
|
111 |
+
if 'affiliation' in df.columns:
|
112 |
+
summary += f"- Institutions represented: {df['affiliation'].nunique()}\n"
|
113 |
+
|
114 |
+
field_counts = analyze_research_fields(df)
|
115 |
+
if not field_counts.empty:
|
116 |
+
top_fields = field_counts.nlargest(3)
|
117 |
+
summary += "- Top research fields:\n"
|
118 |
+
for field, count in top_fields.items():
|
119 |
+
summary += f" • {field}: {count} researchers\n"
|
120 |
+
|
121 |
+
# Create visualization
|
122 |
+
fig = create_visualizations(df)
|
123 |
+
|
124 |
+
return df, fig, summary
|
125 |
+
|
126 |
def process_message(
|
127 |
message,
|
128 |
chat_history,
|
129 |
system_message,
|
130 |
max_tokens,
|
131 |
temperature,
|
132 |
+
top_p
|
|
|
133 |
):
|
134 |
+
"""Process message and return response."""
|
135 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
# Generate response using the LLM
|
137 |
messages = [{"role": "system", "content": system_message}]
|
138 |
for user_msg, bot_msg in chat_history:
|
|
|
150 |
bot_message = response.choices[0].message.content
|
151 |
chat_history.append((message, bot_message))
|
152 |
|
153 |
+
return chat_history
|
154 |
|
155 |
except Exception as e:
|
156 |
error_message = f"Error: {str(e)}"
|
157 |
chat_history.append((message, error_message))
|
158 |
+
return chat_history
|
159 |
|
160 |
with gr.Blocks(title="CohortBot") as demo:
|
161 |
+
with gr.Row():
|
162 |
+
with gr.Column(scale=2):
|
163 |
+
chatbot = gr.Chatbot(label="Chat History")
|
164 |
+
msg = gr.Textbox(label="Message", placeholder="Type your message here...")
|
165 |
+
with gr.Row():
|
166 |
+
system_msg = gr.Textbox(value="You are a friendly Research Community Chatbot.", label="System message")
|
167 |
+
with gr.Row():
|
168 |
+
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
169 |
+
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
170 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
|
171 |
+
|
172 |
+
with gr.Column(scale=1):
|
173 |
+
chat_history_text = gr.Textbox(label="Chat History for Analysis", lines=10)
|
174 |
+
analyze_btn = gr.Button("Analyze Chat History", variant="primary")
|
175 |
+
with gr.Row():
|
176 |
+
analysis_text = gr.Textbox(label="Analysis Summary", lines=4)
|
177 |
+
with gr.Row():
|
178 |
+
researcher_table = gr.Dataframe(label="Extracted Researcher Data")
|
179 |
+
with gr.Row():
|
180 |
+
plot = gr.Plot(label="Community Analysis")
|
181 |
|
182 |
msg.submit(
|
183 |
process_message,
|
184 |
+
[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
185 |
+
[chatbot]
|
186 |
+
)
|
187 |
+
|
188 |
+
analyze_btn.click(
|
189 |
+
analyze_chat_history,
|
190 |
+
inputs=[chat_history_text],
|
191 |
+
outputs=[researcher_table, plot, analysis_text]
|
192 |
)
|
193 |
|
194 |
if __name__ == "__main__":
|