chat
Browse files- app.py +32 -7
- chatbot_page.py +71 -0
app.py
CHANGED
@@ -115,10 +115,16 @@ def show_combined_repo_and_llm():
|
|
115 |
return combined_content, summary, df
|
116 |
|
117 |
def go_to_analysis():
|
118 |
-
return gr.update(visible=False), gr.update(visible=True)
|
119 |
|
120 |
def go_to_input():
|
121 |
-
return gr.update(visible=True), gr.update(visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
repo_id_input = gr.Textbox(label="Enter repo IDs (comma or newline separated)", lines=5, placeholder="repo1, repo2\nrepo3")
|
124 |
df_output = gr.Dataframe(headers=["repo id", "strength", "weaknesses", "speciality", "relevance rating", "Usecase"],
|
@@ -128,8 +134,14 @@ df_output = gr.Dataframe(headers=["repo id", "strength", "weaknesses", "speciali
|
|
128 |
with gr.Blocks() as demo:
|
129 |
page_state = gr.State(0)
|
130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
# --- Page 1: Input ---
|
132 |
-
with gr.Column(visible=
|
133 |
gr.Markdown("## Enter Keyword or Repo IDs")
|
134 |
keyword_input = gr.Textbox(label="Enter keyword to search repos", lines=1, placeholder="e.g. audio")
|
135 |
keyword_btn = gr.Button("Search and Update Repo List")
|
@@ -137,6 +149,7 @@ with gr.Blocks() as demo:
|
|
137 |
df_box = df_output.render()
|
138 |
submit_btn = gr.Button("Submit Repo IDs")
|
139 |
next_btn = gr.Button("Next: Go to Analysis")
|
|
|
140 |
|
141 |
# --- Page 2: Analysis ---
|
142 |
with gr.Column(visible=False) as analysis_page:
|
@@ -149,10 +162,22 @@ with gr.Blocks() as demo:
|
|
149 |
datatype=["str", "str", "str", "str", "str", "str"]
|
150 |
)
|
151 |
back_btn = gr.Button("Back to Input")
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
|
157 |
# Keyword and repo input logic
|
158 |
keyword_btn.click(keyword_search_and_update, inputs=keyword_input, outputs=df_box)
|
|
|
115 |
return combined_content, summary, df
|
116 |
|
117 |
def go_to_analysis():
|
118 |
+
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
|
119 |
|
120 |
def go_to_input():
|
121 |
+
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
122 |
+
|
123 |
+
def go_to_chatbot():
|
124 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
125 |
+
|
126 |
+
def go_to_start():
|
127 |
+
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
128 |
|
129 |
repo_id_input = gr.Textbox(label="Enter repo IDs (comma or newline separated)", lines=5, placeholder="repo1, repo2\nrepo3")
|
130 |
df_output = gr.Dataframe(headers=["repo id", "strength", "weaknesses", "speciality", "relevance rating", "Usecase"],
|
|
|
134 |
with gr.Blocks() as demo:
|
135 |
page_state = gr.State(0)
|
136 |
|
137 |
+
# --- Start Page: Option Selection ---
|
138 |
+
with gr.Column(visible=True) as start_page:
|
139 |
+
gr.Markdown("## Welcome! How would you like to proceed?")
|
140 |
+
option_a_btn = gr.Button("A) I know which repos I want to search and research about")
|
141 |
+
option_b_btn = gr.Button("B) I don't know exactly what I want (Chatbot)")
|
142 |
+
|
143 |
# --- Page 1: Input ---
|
144 |
+
with gr.Column(visible=False) as input_page:
|
145 |
gr.Markdown("## Enter Keyword or Repo IDs")
|
146 |
keyword_input = gr.Textbox(label="Enter keyword to search repos", lines=1, placeholder="e.g. audio")
|
147 |
keyword_btn = gr.Button("Search and Update Repo List")
|
|
|
149 |
df_box = df_output.render()
|
150 |
submit_btn = gr.Button("Submit Repo IDs")
|
151 |
next_btn = gr.Button("Next: Go to Analysis")
|
152 |
+
back_to_start_btn = gr.Button("Back to Start")
|
153 |
|
154 |
# --- Page 2: Analysis ---
|
155 |
with gr.Column(visible=False) as analysis_page:
|
|
|
162 |
datatype=["str", "str", "str", "str", "str", "str"]
|
163 |
)
|
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])
|
179 |
+
back_to_start_btn2.click(go_to_start, inputs=None, outputs=[start_page, input_page, chatbot_page])
|
180 |
+
back_to_start_btn3.click(go_to_start, inputs=None, outputs=[start_page, input_page, chatbot_page])
|
181 |
|
182 |
# Keyword and repo input logic
|
183 |
keyword_btn.click(keyword_search_and_update, inputs=keyword_input, outputs=df_box)
|
chatbot_page.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from analyzer import analyze_code
|
3 |
+
|
4 |
+
# System prompt for the chatbot
|
5 |
+
CHATBOT_SYSTEM_PROMPT = (
|
6 |
+
"You are a helpful assistant. Your goal is to help the user describe their ideal open-source repo. "
|
7 |
+
"Ask questions to clarify what they want, their use case, preferred language, features, etc. "
|
8 |
+
"When the user clicks 'End Chat', analyze the conversation and return about 5 keywords for repo search. "
|
9 |
+
"Return only the keywords as a comma-separated list."
|
10 |
+
)
|
11 |
+
|
12 |
+
# Store the conversation
|
13 |
+
conversation_history = []
|
14 |
+
|
15 |
+
# Function to handle chat
|
16 |
+
def chat_with_user(user_message, history):
|
17 |
+
from openai import OpenAI
|
18 |
+
client = OpenAI()
|
19 |
+
# Build the message list for the LLM
|
20 |
+
messages = [
|
21 |
+
{"role": "system", "content": CHATBOT_SYSTEM_PROMPT}
|
22 |
+
]
|
23 |
+
for msg in history:
|
24 |
+
messages.append({"role": "user", "content": msg[0]})
|
25 |
+
if msg[1]:
|
26 |
+
messages.append({"role": "assistant", "content": msg[1]})
|
27 |
+
messages.append({"role": "user", "content": user_message})
|
28 |
+
response = client.chat.completions.create(
|
29 |
+
model="gpt-4o-mini",
|
30 |
+
messages=messages,
|
31 |
+
max_tokens=256,
|
32 |
+
temperature=0.7
|
33 |
+
)
|
34 |
+
assistant_reply = response.choices[0].message.content
|
35 |
+
return assistant_reply
|
36 |
+
|
37 |
+
# Function to end chat and extract keywords
|
38 |
+
def extract_keywords_from_conversation(history):
|
39 |
+
# Combine all user and assistant messages into a single string
|
40 |
+
conversation = "\n".join([f"User: {msg[0]}\nAssistant: {msg[1]}" for msg in history if msg[1]])
|
41 |
+
prompt = (
|
42 |
+
"Given the following conversation between a user and an assistant about finding an ideal open-source repo, "
|
43 |
+
"extract about 5 keywords that best represent what the user is looking for. "
|
44 |
+
"Return only the keywords as a comma-separated list.\n\nConversation:\n" + conversation
|
45 |
+
)
|
46 |
+
keywords = analyze_code(prompt)
|
47 |
+
return keywords
|
48 |
+
|
49 |
+
with gr.Blocks() as chatbot_demo:
|
50 |
+
gr.Markdown("## Repo Recommendation Chatbot")
|
51 |
+
chatbot = gr.Chatbot()
|
52 |
+
state = gr.State([]) # conversation history
|
53 |
+
user_input = gr.Textbox(label="Your message", placeholder="Describe your ideal repo or answer the assistant's questions...")
|
54 |
+
send_btn = gr.Button("Send")
|
55 |
+
end_btn = gr.Button("End Chat and Extract Keywords")
|
56 |
+
keywords_output = gr.Textbox(label="Extracted Keywords for Repo Search", interactive=False)
|
57 |
+
|
58 |
+
def user_send(user_message, history):
|
59 |
+
assistant_reply = chat_with_user(user_message, history)
|
60 |
+
history = history + [[user_message, assistant_reply]]
|
61 |
+
return history, history, ""
|
62 |
+
|
63 |
+
def end_chat(history):
|
64 |
+
keywords = extract_keywords_from_conversation(history)
|
65 |
+
return keywords
|
66 |
+
|
67 |
+
send_btn.click(user_send, inputs=[user_input, state], outputs=[chatbot, state, user_input])
|
68 |
+
end_btn.click(end_chat, inputs=state, outputs=keywords_output)
|
69 |
+
|
70 |
+
if __name__ == "__main__":
|
71 |
+
chatbot_demo.launch()
|