jyo01 commited on
Commit
9f7c1cd
·
verified ·
1 Parent(s): 176927d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -129
app.py CHANGED
@@ -125,106 +125,11 @@ def get_gemini_flash_response(prompt: str) -> str:
125
 
126
 
127
 
128
- # ############################################
129
- # # Gradio Interface Functions
130
- # ############################################
131
-
132
- # # For file content retrieval, we now use the file path directly.
133
- # def get_file_content_for_choice(github_url: str, file_path: str):
134
- # try:
135
- # owner, repo = extract_repo_info(github_url)
136
- # except Exception as e:
137
- # return str(e)
138
- # content = get_file_content(owner, repo, file_path)
139
- # return content, file_path
140
-
141
- # def chat_with_file(github_url: str, file_path: str, user_query: str):
142
- # # Retrieve file content using the file path directly.
143
- # result = get_file_content_for_choice(github_url, file_path)
144
- # if isinstance(result, str):
145
- # return result # Return error message if occurred.
146
- # file_content, selected_file = result
147
-
148
- # # Preprocess file content and extract context.
149
- # preprocessed = preprocess_text(file_content)
150
- # context_snippet = preprocessed[:5000] # Use first 1000 characters as context.
151
-
152
- # # Generate the prompt based on context and user query.
153
- # prompt = generate_prompt(user_query, [context_snippet])
154
-
155
- # # Use Gemini Flash to generate a response.
156
- # llm_response = get_gemini_flash_response(prompt)
157
-
158
- # return f"File: {selected_file}\n\nLLM Response:\n{llm_response}"
159
-
160
-
161
- # def load_repo_contents_backend(github_url: str):
162
- # try:
163
- # owner, repo = extract_repo_info(github_url)
164
- # except Exception as e:
165
- # return f"Error: {str(e)}"
166
- # repo_data = get_repo_metadata(owner, repo)
167
- # default_branch = repo_data.get("default_branch", "main")
168
- # tree_data = get_repo_tree(owner, repo, default_branch)
169
- # if "tree" not in tree_data:
170
- # return "Error: Could not fetch repository tree."
171
- # file_list = [item["path"] for item in tree_data["tree"] if item["type"] == "blob"]
172
- # return file_list
173
-
174
  ############################################
175
- # Gradio Interface Setup
176
  ############################################
177
 
178
- # with gr.Blocks() as demo:
179
- # gr.Markdown("# RepoChat - Chat with Repository Files")
180
-
181
- # with gr.Row():
182
- # with gr.Column(scale=1):
183
- # gr.Markdown("### Repository Information")
184
- # github_url_input = gr.Textbox(label="GitHub Repository URL", placeholder="https://github.com/username/repository")
185
- # load_repo_btn = gr.Button("Load Repository Contents")
186
- # # Dropdown with choices as file paths; default value is empty.
187
- # file_dropdown = gr.Dropdown(label="Select a File", interactive=True, value="", choices=[])
188
- # # repo_content_output = gr.Textbox(label="File Content", interactive=False, lines=30)
189
- # repo_content_output = gr.Chatbot(label="Chat Conversation")
190
- # with gr.Column(scale=2):
191
- # gr.Markdown("### Chat Interface")
192
- # chat_query_input = gr.Textbox(label="Your Query", placeholder="Type your query here")
193
- # chat_output = gr.Textbox(label="Chatbot Response", interactive=False, lines=10)
194
- # chat_btn = gr.Button("Send Query")
195
-
196
- # # Callback: Update file dropdown choices.
197
- # def update_file_dropdown(github_url):
198
- # files = load_repo_contents_backend(github_url)
199
- # if isinstance(files, str): # Error message
200
- # print("Error loading files:", files)
201
- # return gr.update(choices=[], value="")
202
- # print("Files loaded:", files)
203
- # # Do not pre-select any file (empty value)
204
- # return gr.update(choices=files, value="")
205
-
206
- # load_repo_btn.click(fn=update_file_dropdown, inputs=[github_url_input], outputs=[file_dropdown])
207
-
208
- # # Callback: Update repository content when a file is selected.
209
- # def update_repo_content(github_url, file_choice):
210
- # if not file_choice:
211
- # return "No file selected."
212
- # content, _ = get_file_content_for_choice(github_url, file_choice)
213
- # return content
214
-
215
- # file_dropdown.change(fn=update_repo_content, inputs=[github_url_input, file_dropdown], outputs=[repo_content_output])
216
-
217
- # # Callback: Process chat query.
218
- # def process_chat(github_url, file_choice, chat_query):
219
- # if not file_choice:
220
- # return "Please select a file first."
221
- # return chat_with_file(github_url, file_choice, chat_query)
222
-
223
- # chat_btn.click(fn=process_chat, inputs=[github_url_input, file_dropdown, chat_query_input], outputs=[chat_output])
224
-
225
- # demo.launch(share=True)
226
-
227
-
228
  def get_file_content_for_choice(github_url: str, file_path: str):
229
  try:
230
  owner, repo = extract_repo_info(github_url)
@@ -234,16 +139,25 @@ def get_file_content_for_choice(github_url: str, file_path: str):
234
  return content, file_path
235
 
236
  def chat_with_file(github_url: str, file_path: str, user_query: str):
 
237
  result = get_file_content_for_choice(github_url, file_path)
238
  if isinstance(result, str):
239
  return result # Return error message if occurred.
240
  file_content, selected_file = result
 
 
241
  preprocessed = preprocess_text(file_content)
242
- context_snippet = preprocessed[:1000] # Use the first 1000 characters as context
 
 
243
  prompt = generate_prompt(user_query, [context_snippet])
 
 
244
  llm_response = get_gemini_flash_response(prompt)
 
245
  return f"File: {selected_file}\n\nLLM Response:\n{llm_response}"
246
 
 
247
  def load_repo_contents_backend(github_url: str):
248
  try:
249
  owner, repo = extract_repo_info(github_url)
@@ -252,61 +166,71 @@ def load_repo_contents_backend(github_url: str):
252
  repo_data = get_repo_metadata(owner, repo)
253
  default_branch = repo_data.get("default_branch", "main")
254
  tree_data = get_repo_tree(owner, repo, default_branch)
255
- if not isinstance(tree_data, dict) or "tree" not in tree_data:
256
  return "Error: Could not fetch repository tree."
257
  file_list = [item["path"] for item in tree_data["tree"] if item["type"] == "blob"]
258
  return file_list
259
 
260
- ############################################
261
- # Gradio Interface Setup
262
- ############################################
263
 
264
  with gr.Blocks() as demo:
265
  gr.Markdown("# RepoChat - Chat with Repository Files")
266
-
267
  with gr.Row():
268
  with gr.Column(scale=1):
269
  gr.Markdown("### Repository Information")
270
  github_url_input = gr.Textbox(label="GitHub Repository URL", placeholder="https://github.com/username/repository")
271
  load_repo_btn = gr.Button("Load Repository Contents")
272
- file_dropdown = gr.Dropdown(label="Select a File", interactive=True, choices=[])
273
- repo_content_output = gr.Textbox(label="File Content", interactive=False, lines=30)
274
- # repo_content_output = gr.Chatbot(label="File Content")
275
-
276
  with gr.Column(scale=2):
277
  gr.Markdown("### Chat Interface")
278
- chat_output = gr.Chatbot(label="Chat Conversation")
279
  chat_query_input = gr.Textbox(label="Your Query", placeholder="Type your query here")
280
- chat_btn = gr.Button("Send Query")
281
-
282
- # State to hold conversation history
283
- conversation_history = gr.State([])
284
 
 
 
 
285
  def update_file_dropdown(github_url):
286
  files = load_repo_contents_backend(github_url)
287
  if isinstance(files, str): # Error message
288
- return gr.update(choices=[]), files
289
- return gr.update(choices=files), ""
 
 
 
 
 
 
 
 
 
 
 
 
290
 
291
  def update_repo_content(github_url, file_choice):
292
  if not file_choice:
293
- return "No file selected."
294
  content, _ = get_file_content_for_choice(github_url, file_choice)
295
- return content
 
296
 
297
 
298
-
299
-
300
- def process_chat(github_url, file_choice, chat_query, history):
301
- if not file_choice:
302
- history.append(("System", "Please select a file first."))
303
- return history, history
304
- response = chat_with_file(github_url, file_choice, chat_query)
305
- history.append((chat_query, response))
306
- return history, history
307
-
308
- load_repo_btn.click(fn=update_file_dropdown, inputs=[github_url_input], outputs=[file_dropdown, repo_content_output])
309
  file_dropdown.change(fn=update_repo_content, inputs=[github_url_input, file_dropdown], outputs=[repo_content_output])
310
- chat_btn.click(fn=process_chat, inputs=[github_url_input, file_dropdown, chat_query_input, conversation_history], outputs=[chat_output, conversation_history])
 
 
 
 
 
 
 
 
 
311
 
312
- demo.launch(server_port=7862)
 
125
 
126
 
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  ############################################
129
+ # Gradio Interface Functions
130
  ############################################
131
 
132
+ # For file content retrieval, we now use the file path directly.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  def get_file_content_for_choice(github_url: str, file_path: str):
134
  try:
135
  owner, repo = extract_repo_info(github_url)
 
139
  return content, file_path
140
 
141
  def chat_with_file(github_url: str, file_path: str, user_query: str):
142
+ # Retrieve file content using the file path directly.
143
  result = get_file_content_for_choice(github_url, file_path)
144
  if isinstance(result, str):
145
  return result # Return error message if occurred.
146
  file_content, selected_file = result
147
+
148
+ # Preprocess file content and extract context.
149
  preprocessed = preprocess_text(file_content)
150
+ context_snippet = preprocessed[:5000] # Use first 1000 characters as context.
151
+
152
+ # Generate the prompt based on context and user query.
153
  prompt = generate_prompt(user_query, [context_snippet])
154
+
155
+ # Use Gemini Flash to generate a response.
156
  llm_response = get_gemini_flash_response(prompt)
157
+
158
  return f"File: {selected_file}\n\nLLM Response:\n{llm_response}"
159
 
160
+
161
  def load_repo_contents_backend(github_url: str):
162
  try:
163
  owner, repo = extract_repo_info(github_url)
 
166
  repo_data = get_repo_metadata(owner, repo)
167
  default_branch = repo_data.get("default_branch", "main")
168
  tree_data = get_repo_tree(owner, repo, default_branch)
169
+ if "tree" not in tree_data:
170
  return "Error: Could not fetch repository tree."
171
  file_list = [item["path"] for item in tree_data["tree"] if item["type"] == "blob"]
172
  return file_list
173
 
174
+ ###########################################
175
+ Gradio Interface Setup
176
+ ###########################################
177
 
178
  with gr.Blocks() as demo:
179
  gr.Markdown("# RepoChat - Chat with Repository Files")
180
+
181
  with gr.Row():
182
  with gr.Column(scale=1):
183
  gr.Markdown("### Repository Information")
184
  github_url_input = gr.Textbox(label="GitHub Repository URL", placeholder="https://github.com/username/repository")
185
  load_repo_btn = gr.Button("Load Repository Contents")
186
+ # Dropdown with choices as file paths; default value is empty.
187
+ file_dropdown = gr.Dropdown(label="Select a File", interactive=True, value="", choices=[])
188
+ # repo_content_output = gr.Textbox(label="File Content", interactive=False, lines=30)
189
+ repo_content_output = gr.Chatbot(label="Chat Conversation")
190
  with gr.Column(scale=2):
191
  gr.Markdown("### Chat Interface")
 
192
  chat_query_input = gr.Textbox(label="Your Query", placeholder="Type your query here")
193
+ # chat_output = gr.Textbox(label="Chatbot Response", interactive=False, lines=10)
194
+ chat_output = gr.Chatbot(label="File Content")
 
 
195
 
196
+ chat_btn = gr.Button("Send Query")
197
+
198
+ # Callback: Update file dropdown choices.
199
  def update_file_dropdown(github_url):
200
  files = load_repo_contents_backend(github_url)
201
  if isinstance(files, str): # Error message
202
+ print("Error loading files:", files)
203
+ return gr.update(choices=[], value="")
204
+ print("Files loaded:", files)
205
+ # Do not pre-select any file (empty value)
206
+ return gr.update(choices=files, value="")
207
+
208
+ load_repo_btn.click(fn=update_file_dropdown, inputs=[github_url_input], outputs=[file_dropdown])
209
+
210
+ # Callback: Update repository content when a file is selected.
211
+ # def update_repo_content(github_url, file_choice):
212
+ # if not file_choice:
213
+ # return "No file selected."
214
+ # content, _ = get_file_content_for_choice(github_url, file_choice)
215
+ # return content
216
 
217
  def update_repo_content(github_url, file_choice):
218
  if not file_choice:
219
+ return [("System", "No file selected.")]
220
  content, _ = get_file_content_for_choice(github_url, file_choice)
221
+ # Wrap the content in a tuple so it displays like a chat message.
222
+ return [("File Content", content)]
223
 
224
 
 
 
 
 
 
 
 
 
 
 
 
225
  file_dropdown.change(fn=update_repo_content, inputs=[github_url_input, file_dropdown], outputs=[repo_content_output])
226
+
227
+ # Callback: Process chat query.
228
+ def process_chat(github_url, file_choice, chat_query):
229
+ if not file_choice:
230
+ return "Please select a file first."
231
+ return chat_with_file(github_url, file_choice, chat_query)
232
+
233
+ chat_btn.click(fn=process_chat, inputs=[github_url_input, file_dropdown, chat_query_input], outputs=[chat_output])
234
+
235
+ demo.launch(share=True)
236