naman1102 commited on
Commit
885c1f9
·
1 Parent(s): 5b2420c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -6
app.py CHANGED
@@ -5,7 +5,16 @@ import pandas as pd
5
  from analyzer import combine_repo_files_for_llm, analyze_combined_file, parse_llm_json_response
6
  from hf_utils import download_space_repo, search_top_spaces
7
 
8
- # from hf_utils import download_space_repo
 
 
 
 
 
 
 
 
 
9
 
10
  def read_csv_as_text(csv_filename):
11
  return pd.read_csv(csv_filename, dtype=str)
@@ -131,6 +140,36 @@ df_output = gr.Dataframe(headers=["repo id", "strength", "weaknesses", "speciali
131
  datatype=["str", "str", "str", "str", "str", "str"]
132
  )
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  with gr.Blocks() as demo:
135
  page_state = gr.State(0)
136
 
@@ -164,15 +203,20 @@ with gr.Blocks() as demo:
164
  back_btn = gr.Button("Back to Input")
165
  back_to_start_btn2 = gr.Button("Back to Start")
166
 
167
- # --- Page 3: Chatbot Placeholder ---
168
  with gr.Column(visible=False) as chatbot_page:
169
- gr.Markdown("## Chatbot (Coming Soon)")
170
- gr.Markdown("This is where you will be able to chat and get repo recommendations!")
 
 
 
 
 
171
  back_to_start_btn3 = gr.Button("Back to Start")
172
 
173
  # Navigation logic
174
- option_a_btn.click(go_to_analysis, inputs=None, outputs=[start_page, analysis_page, chatbot_page])
175
- option_b_btn.click(go_to_chatbot, inputs=None, outputs=[start_page, analysis_page, chatbot_page])
176
  next_btn.click(go_to_analysis, inputs=None, outputs=[input_page, analysis_page, chatbot_page])
177
  back_btn.click(go_to_input, inputs=None, outputs=[input_page, analysis_page, chatbot_page])
178
  back_to_start_btn.click(go_to_start, inputs=None, outputs=[start_page, input_page, chatbot_page])
@@ -186,4 +230,17 @@ with gr.Blocks() as demo:
186
  # Analysis logic
187
  combine_btn.click(show_combined_repo_and_llm, inputs=None, outputs=[combined_txt, llm_output_txt, df_display])
188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  demo.launch()
 
5
  from analyzer import combine_repo_files_for_llm, analyze_combined_file, parse_llm_json_response
6
  from hf_utils import download_space_repo, search_top_spaces
7
 
8
+ # Import chatbot logic
9
+ from analyzer import analyze_code
10
+
11
+ # Chatbot system prompt
12
+ CHATBOT_SYSTEM_PROMPT = (
13
+ "You are a helpful assistant. Your goal is to help the user describe their ideal open-source repo. "
14
+ "Ask questions to clarify what they want, their use case, preferred language, features, etc. "
15
+ "When the user clicks 'End Chat', analyze the conversation and return about 5 keywords for repo search. "
16
+ "Return only the keywords as a comma-separated list."
17
+ )
18
 
19
  def read_csv_as_text(csv_filename):
20
  return pd.read_csv(csv_filename, dtype=str)
 
140
  datatype=["str", "str", "str", "str", "str", "str"]
141
  )
142
 
143
+ def chat_with_user(user_message, history):
144
+ from openai import OpenAI
145
+ client = OpenAI()
146
+ messages = [
147
+ {"role": "system", "content": CHATBOT_SYSTEM_PROMPT}
148
+ ]
149
+ for msg in history:
150
+ messages.append({"role": "user", "content": msg[0]})
151
+ if msg[1]:
152
+ messages.append({"role": "assistant", "content": msg[1]})
153
+ messages.append({"role": "user", "content": user_message})
154
+ response = client.chat.completions.create(
155
+ model="gpt-4o-mini",
156
+ messages=messages,
157
+ max_tokens=256,
158
+ temperature=0.7
159
+ )
160
+ assistant_reply = response.choices[0].message.content
161
+ return assistant_reply
162
+
163
+ def extract_keywords_from_conversation(history):
164
+ conversation = "\n".join([f"User: {msg[0]}\nAssistant: {msg[1]}" for msg in history if msg[1]])
165
+ prompt = (
166
+ "Given the following conversation between a user and an assistant about finding an ideal open-source repo, "
167
+ "extract about 5 keywords that best represent what the user is looking for. "
168
+ "Return only the keywords as a comma-separated list.\n\nConversation:\n" + conversation
169
+ )
170
+ keywords = analyze_code(prompt)
171
+ return keywords
172
+
173
  with gr.Blocks() as demo:
174
  page_state = gr.State(0)
175
 
 
203
  back_btn = gr.Button("Back to Input")
204
  back_to_start_btn2 = gr.Button("Back to Start")
205
 
206
+ # --- Page 3: Chatbot ---
207
  with gr.Column(visible=False) as chatbot_page:
208
+ gr.Markdown("## Repo Recommendation Chatbot")
209
+ chatbot = gr.Chatbot()
210
+ state = gr.State([])
211
+ user_input = gr.Textbox(label="Your message", placeholder="Describe your ideal repo or answer the assistant's questions...")
212
+ send_btn = gr.Button("Send")
213
+ end_btn = gr.Button("End Chat and Extract Keywords")
214
+ keywords_output = gr.Textbox(label="Extracted Keywords for Repo Search", interactive=False)
215
  back_to_start_btn3 = gr.Button("Back to Start")
216
 
217
  # Navigation logic
218
+ option_a_btn.click(go_to_input, inputs=None, outputs=[start_page, input_page, chatbot_page])
219
+ option_b_btn.click(go_to_chatbot, inputs=None, outputs=[start_page, input_page, chatbot_page])
220
  next_btn.click(go_to_analysis, inputs=None, outputs=[input_page, analysis_page, chatbot_page])
221
  back_btn.click(go_to_input, inputs=None, outputs=[input_page, analysis_page, chatbot_page])
222
  back_to_start_btn.click(go_to_start, inputs=None, outputs=[start_page, input_page, chatbot_page])
 
230
  # Analysis logic
231
  combine_btn.click(show_combined_repo_and_llm, inputs=None, outputs=[combined_txt, llm_output_txt, df_display])
232
 
233
+ # Chatbot logic
234
+ def user_send(user_message, history):
235
+ assistant_reply = chat_with_user(user_message, history)
236
+ history = history + [[user_message, assistant_reply]]
237
+ return history, history, ""
238
+
239
+ def end_chat(history):
240
+ keywords = extract_keywords_from_conversation(history)
241
+ return keywords
242
+
243
+ send_btn.click(user_send, inputs=[user_input, state], outputs=[chatbot, state, user_input])
244
+ end_btn.click(end_chat, inputs=state, outputs=keywords_output)
245
+
246
  demo.launch()