Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -163,133 +163,125 @@ def respond(
|
|
163 |
logging.info(f"Command received: {command}")
|
164 |
|
165 |
if command == "/github":
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
try:
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
resolution = analyze_issues(issue_text, selected_model, severity, programming_language)
|
193 |
-
|
194 |
-
# Find and display related issues
|
195 |
-
related_issues = find_related_issues(issue_text, issues)
|
196 |
-
related_issue_text = "\n".join(
|
197 |
-
[f"- {issue['title']} (Similarity: {similarity:.2f})" for issue, similarity in related_issues]
|
198 |
-
)
|
199 |
-
|
200 |
-
yield f"Resolution for Issue '{issue['title']}':\n{resolution}\n\nRelated Issues:\n{related_issue_text}"
|
201 |
except Exception as e:
|
202 |
-
logging.error(f"Error
|
203 |
-
yield f"Error
|
204 |
-
|
205 |
-
elif command.startswith("/generate_code"):
|
206 |
-
# Extract the code description from the command
|
207 |
-
code_description = command.replace("/generate_code", "").strip()
|
208 |
-
if not code_description:
|
209 |
-
yield "Please provide a description of the code you want to generate."
|
210 |
-
else:
|
211 |
-
prompt = f"Generate code for the following: {code_description}\nProgramming Language: {programming_language}"
|
212 |
-
try:
|
213 |
-
generated_code = analyze_issues(prompt, selected_model)
|
214 |
-
code_output = f"
|
215 |
-
|
216 |
-
|
217 |
-
{programming_language}\n{generated_code}\n
|
218 |
-
|
219 |
-
yield code_output # Yield the formatted string
|
220 |
-
except Exception as e:
|
221 |
-
logging.error(f"Error generating code: {e}")
|
222 |
-
yield f"Error generating code: {e}"
|
223 |
-
|
224 |
-
elif command.startswith("/explain_concept"):
|
225 |
-
concept = command.replace("/explain_concept", "").strip()
|
226 |
-
if not concept:
|
227 |
-
yield "Please provide a concept to explain."
|
228 |
-
else:
|
229 |
-
prompt = f"Explain the concept of {concept} in detail."
|
230 |
-
try:
|
231 |
-
explanation = analyze_issues(prompt, selected_model) # Reuse analyze_issues for explanation
|
232 |
-
yield explanation
|
233 |
-
except Exception as e:
|
234 |
-
logging.error(f"Error explaining concept: {e}")
|
235 |
-
yield f"Error explaining concept: {e}"
|
236 |
-
|
237 |
-
elif command.startswith("/write_documentation"):
|
238 |
-
topic = command.replace("/write_documentation", "").strip()
|
239 |
-
if not topic:
|
240 |
-
yield "Please provide a topic for documentation."
|
241 |
-
else:
|
242 |
-
prompt = f"Write comprehensive documentation for the following topic: {topic}"
|
243 |
-
try:
|
244 |
-
documentation = analyze_issues(prompt, selected_model)
|
245 |
-
yield documentation
|
246 |
-
except Exception as e:
|
247 |
-
logging.error(f"Error writing documentation: {e}")
|
248 |
-
yield f"Error writing documentation: {e}"
|
249 |
-
|
250 |
-
elif command.startswith("/translate_code"):
|
251 |
-
parts = command.replace("/translate_code", "").strip().split(" to ")
|
252 |
-
if len(parts) != 2:
|
253 |
-
yield "Invalid command format. Use: /translate_code [code] to [target language]"
|
254 |
-
else:
|
255 |
-
code, target_language = parts
|
256 |
-
prompt = f"Translate the following code to {target_language}:\n
|
257 |
-
|
258 |
-
|
259 |
-
\n{code}\n
|
260 |
|
261 |
-
|
262 |
-
|
263 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
|
266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
267 |
|
268 |
-
yield code_output # Yield the formatted string
|
269 |
-
except Exception as e:
|
270 |
-
logging.error(f"Error translating code: {e}")
|
271 |
-
yield f"Error translating code: {e}"
|
272 |
|
273 |
-
|
274 |
-
messages.append({"role": "user", "content": command})
|
275 |
-
logging.info(f"User message: {command}")
|
276 |
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
293 |
|
294 |
with gr.Blocks() as demo:
|
295 |
with gr.Row():
|
|
|
163 |
logging.info(f"Command received: {command}")
|
164 |
|
165 |
if command == "/github":
|
166 |
+
if not github_api_token:
|
167 |
+
yield "Please enter your GitHub API token first. <https://github.com/settings/tokens>"
|
168 |
+
else:
|
169 |
+
try:
|
170 |
+
issues = fetch_github_issues(github_api_token, github_username, github_repository)
|
171 |
+
issue_list = "\n".join([f"{i+1}. {issue['title']}" for i, issue in enumerate(issues)])
|
172 |
+
yield f"Available GitHub Issues:\n{issue_list}\n\nEnter the issue number to analyze:"
|
173 |
+
except Exception as e:
|
174 |
+
logging.error(f"Error fetching GitHub issues: {e}")
|
175 |
+
yield f"Error fetching GitHub issues: {e}"
|
176 |
+
|
177 |
+
elif command == "/help":
|
178 |
+
help_message = f"""Available commands:
|
179 |
+
- `/github`: Analyze a GitHub issue
|
180 |
+
- `/help`: Show this help message
|
181 |
+
- `/generate_code [code description]`: Generate code based on the description
|
182 |
+
- `/explain_concept [concept]`: Explain a concept
|
183 |
+
- `/write_documentation [topic]`: Write documentation for a given topic
|
184 |
+
- `/translate_code [code] to [target language]`: Translate code to another language"""
|
185 |
+
yield help_message # Yield the pre-formatted help message
|
186 |
+
|
187 |
+
elif command.isdigit() and issues:
|
188 |
+
try:
|
189 |
+
issue_number = int(command) - 1
|
190 |
+
issue = issues[issue_number]
|
191 |
+
issue_text = issue['title'] + "\n\n" + issue['body']
|
192 |
+
resolution = analyze_issues(issue_text, selected_model, severity, programming_language)
|
193 |
+
|
194 |
+
# Find and display related issues
|
195 |
+
related_issues = find_related_issues(issue_text, issues)
|
196 |
+
related_issue_text = "\n".join(
|
197 |
+
[f"- {issue['title']} (Similarity: {similarity:.2f})" for issue, similarity in related_issues]
|
198 |
+
)
|
199 |
+
|
200 |
+
yield f"Resolution for Issue '{issue['title']}':\n{resolution}\n\nRelated Issues:\n{related_issue_text}"
|
201 |
+
except Exception as e:
|
202 |
+
logging.error(f"Error analyzing issue: {e}")
|
203 |
+
yield f"Error analyzing issue: {e}"
|
204 |
+
|
205 |
+
elif command.startswith("/generate_code"):
|
206 |
+
# Extract the code description from the command
|
207 |
+
code_description = command.replace("/generate_code", "").strip()
|
208 |
+
if not code_description:
|
209 |
+
yield "Please provide a description of the code you want to generate."
|
210 |
+
else:
|
211 |
+
prompt = f"Generate code for the following: {code_description}\nProgramming Language: {programming_language}"
|
212 |
try:
|
213 |
+
generated_code = analyze_issues(prompt, selected_model)
|
214 |
+
code_output = f"<pre>{programming_language}\n{generated_code}</pre>"
|
215 |
+
yield code_output # Yield the formatted string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
except Exception as e:
|
217 |
+
logging.error(f"Error generating code: {e}")
|
218 |
+
yield f"Error generating code: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
|
220 |
+
elif command.startswith("/explain_concept"):
|
221 |
+
concept = command.replace("/explain_concept", "").strip()
|
222 |
+
if not concept:
|
223 |
+
yield "Please provide a concept to explain."
|
224 |
+
else:
|
225 |
+
prompt = f"Explain the concept of {concept} in detail."
|
226 |
+
try:
|
227 |
+
explanation = analyze_issues(prompt, selected_model)
|
228 |
+
yield f"<pre>{explanation}</pre>"
|
229 |
+
except Exception as e:
|
230 |
+
logging.error(f"Error explaining concept: {e}")
|
231 |
+
yield f"Error explaining concept: {e}"
|
232 |
|
233 |
+
elif command.startswith("/write_documentation"):
|
234 |
+
topic = command.replace("/write_documentation", "").strip()
|
235 |
+
if not topic:
|
236 |
+
yield "Please provide a topic for documentation."
|
237 |
+
else:
|
238 |
+
prompt = f"Write comprehensive documentation for the following topic: {topic}"
|
239 |
+
try:
|
240 |
+
documentation = analyze_issues(prompt, selected_model)
|
241 |
+
yield f"<pre>{documentation}</pre>"
|
242 |
+
except Exception as e:
|
243 |
+
logging.error(f"Error writing documentation: {e}")
|
244 |
+
yield f"Error writing documentation: {e}"
|
245 |
|
246 |
+
elif command.startswith("/translate_code"):
|
247 |
+
parts = command.replace("/translate_code", "").strip().split(" to ")
|
248 |
+
if len(parts) != 2:
|
249 |
+
yield "Invalid command format. Use: /translate_code [code] to [target language]"
|
250 |
+
else:
|
251 |
+
code, target_language = parts
|
252 |
+
prompt = f"Translate the following code to {target_language}:\n\n
|
253 |
|
|
|
|
|
|
|
|
|
254 |
|
255 |
+
{code}\n
|
|
|
|
|
256 |
|
257 |
+
try:
|
258 |
+
translated_code = analyze_issues(prompt, selected_model)
|
259 |
+
code_output = f"<pre>{target_language}\n{translated_code}</pre>"
|
260 |
+
yield code_output # Yield the formatted string
|
261 |
+
except Exception as e:
|
262 |
+
logging.error(f"Error translating code: {e}")
|
263 |
+
yield f"Error translating code: {e}"
|
264 |
+
|
265 |
+
else:
|
266 |
+
messages.append({"role": "user", "content": command})
|
267 |
+
logging.info(f"User message: {command}")
|
268 |
+
|
269 |
+
response = ""
|
270 |
+
try:
|
271 |
+
for message in client.chat_completion(
|
272 |
+
messages,
|
273 |
+
max_tokens=max_tokens,
|
274 |
+
stream=True,
|
275 |
+
temperature=temperature,
|
276 |
+
top_p=top_p,
|
277 |
+
):
|
278 |
+
logging.info(f"Received message from chat completion: {message}")
|
279 |
+
token = message.choices[0].delta.content
|
280 |
+
response += token
|
281 |
+
yield response
|
282 |
+
except Exception as e:
|
283 |
+
logging.error(f"Error during chat completion: {e}")
|
284 |
+
yield f"An error occurred: {e}"
|
285 |
|
286 |
with gr.Blocks() as demo:
|
287 |
with gr.Row():
|