Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,36 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
from sentence_transformers import SentenceTransformer, util
|
4 |
import os
|
5 |
import requests
|
6 |
-
import json
|
7 |
|
8 |
-
|
9 |
-
GITHUB_API_BASE_URL = "https://api.github.com/repos"
|
10 |
-
DEFAULT_MODEL = "microsoft/CodeBERT-base" # Default model for issue resolution
|
11 |
-
MAX_RELATED_ISSUES = 3 # Maximum number of related issues to display
|
12 |
-
SYSTEM_MESSAGE = "You are GitBot, the Github project guardian angel. You resolve issues and propose implementation of feature requests."
|
13 |
|
14 |
-
#
|
15 |
-
model_name = "enricoros/big-agi" # Choose your preferred model
|
16 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
17 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
18 |
-
similarity_model = SentenceTransformer('all-mpnet-base-v2') # For issue similarity
|
19 |
|
20 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
def analyze_issues(issue_text: str, model_name: str, severity: str = None, programming_language: str = None) ->
|
23 |
-
"""Analyzes
|
24 |
model = pipeline("text-generation", model=model_name)
|
25 |
response = model(
|
26 |
-
f"{
|
27 |
-
max_length=
|
28 |
do_sample=True,
|
29 |
-
temperature=
|
30 |
-
top_k=
|
31 |
)
|
32 |
assistant_response = response[0]['generated_text'].strip()
|
33 |
|
34 |
-
# Extract
|
35 |
if "Severity" in assistant_response:
|
36 |
severity = assistant_response.split(":")[1].strip()
|
37 |
if "Programming Language" in assistant_response:
|
@@ -44,120 +43,167 @@ def analyze_issues(issue_text: str, model_name: str, severity: str = None, progr
|
|
44 |
}
|
45 |
|
46 |
def find_related_issues(issue_text: str, issues: list) -> list:
|
47 |
-
"""Finds related issues based on text
|
48 |
issue_embedding = similarity_model.encode(issue_text)
|
49 |
similarities = [util.cos_sim(issue_embedding, similarity_model.encode(issue['title'])) for issue in issues]
|
50 |
sorted_issues = sorted(enumerate(similarities), key=lambda x: x[1], reverse=True)
|
51 |
-
|
|
|
52 |
|
53 |
def fetch_github_issues(github_api_token: str, github_username: str, github_repository: str) -> list:
|
54 |
-
"""Fetches issues from the GitHub API."""
|
55 |
headers = {'Authorization': f'token {github_api_token}'}
|
56 |
url = f"{GITHUB_API_BASE_URL}/{github_username}/{github_repository}/issues"
|
57 |
response = requests.get(url, headers=headers)
|
58 |
-
|
59 |
-
|
60 |
-
else:
|
61 |
-
return []
|
62 |
|
63 |
def respond(
|
64 |
-
command
|
65 |
-
history
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
75 |
*args,
|
76 |
**kwargs,
|
77 |
) -> dict:
|
78 |
-
"""
|
79 |
-
model = pipeline("text-generation", model=
|
80 |
-
|
81 |
-
# Fetch issues if the command is /github
|
82 |
-
if command == "/github":
|
83 |
-
issues = fetch_github_issues(github_api_token, github_username, github_repository)
|
84 |
-
if issues:
|
85 |
-
related_issues = find_related_issues(history, issues)
|
86 |
-
related_issues_text = "\n".join(
|
87 |
-
f"## Related Issue {i+1}: {issue['title']}\n{issue['body']}" for i, issue in enumerate(related_issues)
|
88 |
-
)
|
89 |
-
history += f"\n{related_issues_text}"
|
90 |
-
|
91 |
-
# Generate a response from the LLM
|
92 |
response = model(
|
93 |
-
f"{
|
94 |
max_length=max_tokens,
|
95 |
do_sample=True,
|
96 |
temperature=temperature,
|
97 |
top_k=top_p,
|
98 |
)
|
99 |
assistant_response = response[0]['generated_text'].strip()
|
100 |
-
|
101 |
-
# Analyze the response for severity and programming language
|
102 |
-
analyzed_data = analyze_issues(assistant_response, selected_model, severity, programming_language)
|
103 |
-
|
104 |
return {
|
105 |
'assistant_response': assistant_response,
|
106 |
-
'severity':
|
107 |
-
'programming_language':
|
108 |
}
|
109 |
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
with gr.Blocks() as demo:
|
113 |
-
gr.Markdown("## GitBot: Your GitHub Assistant")
|
114 |
-
|
115 |
with gr.Row():
|
116 |
github_api_token = gr.Textbox(label="GitHub API Token", type="password")
|
117 |
github_username = gr.Textbox(label="GitHub Username")
|
118 |
github_repository = gr.Textbox(label="GitHub Repository")
|
119 |
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
value=DEFAULT_MODEL,
|
125 |
-
)
|
126 |
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
|
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
respond,
|
150 |
additional_inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
github_api_token,
|
152 |
github_username,
|
153 |
github_repository,
|
154 |
model_dropdown,
|
155 |
severity_dropdown,
|
156 |
programming_language_textbox,
|
157 |
-
gr.Slider(minimum=1, maximum=8192, value=2048, step=1, label="Max new tokens"),
|
158 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
159 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.01, label="Top-p (nucleus sampling)"),
|
160 |
],
|
161 |
)
|
162 |
|
163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
from sentence_transformers import SentenceTransformer, util
|
4 |
import os
|
5 |
import requests
|
|
|
6 |
|
7 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
model_name = "enricoros/big-agi" # You can change this to other models if desired
|
|
|
10 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
|
|
12 |
|
13 |
+
# Constants for enhanced organization
|
14 |
+
GITHUB_API_BASE_URL = "https://api.github.com/repos"
|
15 |
+
DEFAULT_MODEL = "microsoft/CodeBERT-base"
|
16 |
+
MAX_RELATED_ISSUES = 3
|
17 |
+
|
18 |
+
# Load a pre-trained model for sentence similarity
|
19 |
+
similarity_model = SentenceTransformer('all-mpnet-base-v2')
|
20 |
|
21 |
+
def analyze_issues(issue_text: str, model_name: str, severity: str = None, programming_language: str = None) -> str:
|
22 |
+
"""Analyzes issues and provides solutions using a specified language model."""
|
23 |
model = pipeline("text-generation", model=model_name)
|
24 |
response = model(
|
25 |
+
f"{system_message}\n{issue_text}\nAssistant: ",
|
26 |
+
max_length=max_tokens,
|
27 |
do_sample=True,
|
28 |
+
temperature=temperature,
|
29 |
+
top_k=top_p,
|
30 |
)
|
31 |
assistant_response = response[0]['generated_text'].strip()
|
32 |
|
33 |
+
# Extract severity and programming language from the response
|
34 |
if "Severity" in assistant_response:
|
35 |
severity = assistant_response.split(":")[1].strip()
|
36 |
if "Programming Language" in assistant_response:
|
|
|
43 |
}
|
44 |
|
45 |
def find_related_issues(issue_text: str, issues: list) -> list:
|
46 |
+
"""Finds semantically related issues from a list of issues based on the input issue text."""
|
47 |
issue_embedding = similarity_model.encode(issue_text)
|
48 |
similarities = [util.cos_sim(issue_embedding, similarity_model.encode(issue['title'])) for issue in issues]
|
49 |
sorted_issues = sorted(enumerate(similarities), key=lambda x: x[1], reverse=True)
|
50 |
+
related_issues = [issues[i] for i, similarity in sorted_issues[:MAX_RELATED_ISSUES]]
|
51 |
+
return related_issues
|
52 |
|
53 |
def fetch_github_issues(github_api_token: str, github_username: str, github_repository: str) -> list:
|
54 |
+
"""Fetches issues from a specified GitHub repository using the GitHub API."""
|
55 |
headers = {'Authorization': f'token {github_api_token}'}
|
56 |
url = f"{GITHUB_API_BASE_URL}/{github_username}/{github_repository}/issues"
|
57 |
response = requests.get(url, headers=headers)
|
58 |
+
issues = response.json()
|
59 |
+
return issues
|
|
|
|
|
60 |
|
61 |
def respond(
|
62 |
+
command,
|
63 |
+
history,
|
64 |
+
system_message,
|
65 |
+
max_tokens,
|
66 |
+
temperature,
|
67 |
+
top_p,
|
68 |
+
github_api_token,
|
69 |
+
github_username,
|
70 |
+
github_repository,
|
71 |
+
selected_model,
|
72 |
+
severity,
|
73 |
+
programming_language,
|
74 |
*args,
|
75 |
**kwargs,
|
76 |
) -> dict:
|
77 |
+
"""Handles user commands and generates responses using the selected language model."""
|
78 |
+
model = pipeline("text-generation", model="enricoros/big-agi")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
response = model(
|
80 |
+
f"{system_message}\n{command}\n{history}\n{github_username}/{github_repository}\n{severity}\n{programming_language}\nAssistant: ",
|
81 |
max_length=max_tokens,
|
82 |
do_sample=True,
|
83 |
temperature=temperature,
|
84 |
top_k=top_p,
|
85 |
)
|
86 |
assistant_response = response[0]['generated_text'].strip()
|
|
|
|
|
|
|
|
|
87 |
return {
|
88 |
'assistant_response': assistant_response,
|
89 |
+
'severity': severity,
|
90 |
+
'programming_language': programming_language,
|
91 |
}
|
92 |
|
93 |
+
class MyChatbot(gr.Chatbot):
|
94 |
+
"""Custom Chatbot class for enhanced functionality."""
|
95 |
+
def __init__(self, fn, **kwargs):
|
96 |
+
super().__init__(fn, **kwargs)
|
97 |
+
self.issues = [] # Store fetched issues
|
98 |
+
|
99 |
+
def postprocess(self, y):
|
100 |
+
"""Post-processes the response to handle commands and display results."""
|
101 |
+
# Extract the response from the dictionary
|
102 |
+
assistant_response = y['assistant_response']
|
103 |
+
|
104 |
+
# Handle commands
|
105 |
+
if y['command'] == "/github":
|
106 |
+
if not y['github_api_token']:
|
107 |
+
return "Please enter your GitHub API token first."
|
108 |
+
else:
|
109 |
+
try:
|
110 |
+
self.issues = fetch_github_issues(y['github_api_token'], y['github_username'], y['github_repository'])
|
111 |
+
issue_list = "\n".join([f"{i+1}. {issue['title']}" for i, issue in enumerate(self.issues)])
|
112 |
+
return f"Available GitHub Issues:\n{issue_list}\n\nEnter the issue number to analyze:"
|
113 |
+
except Exception as e:
|
114 |
+
return f"Error fetching GitHub issues: {e}"
|
115 |
+
elif y['command'] == "/help":
|
116 |
+
return """Available commands:
|
117 |
+
- `/github`: Analyze a GitHub issue
|
118 |
+
- `/help`: Show this help message
|
119 |
+
- `/generate_code [code description]`: Generate code based on the description
|
120 |
+
- `/explain_concept [concept]`: Explain a concept
|
121 |
+
- `/write_documentation [topic]`: Write documentation for a given topic
|
122 |
+
- `/translate_code [code] to [target language]`: Translate code to another language"""
|
123 |
+
elif y['command'].isdigit() and self.issues:
|
124 |
+
try:
|
125 |
+
issue_number = int(y['command']) - 1
|
126 |
+
issue = self.issues[issue_number]
|
127 |
+
issue_text = issue['title'] + "\n\n" + issue['body']
|
128 |
+
resolution = analyze_issues(issue_text, y['selected_model'], y['severity'], y['programming_language'])
|
129 |
+
related_issues = find_related_issues(issue_text, self.issues)
|
130 |
+
related_issue_text = "\n".join(
|
131 |
+
[f"- {issue['title']} (Similarity: {similarity:.2f})" for issue, similarity in related_issues]
|
132 |
+
)
|
133 |
+
return f"Resolution for Issue '{issue['title']}':\n{resolution['assistant_response']}\n\nRelated Issues:\n{related_issue_text}"
|
134 |
+
except Exception as e:
|
135 |
+
return f"Error analyzing issue: {e}"
|
136 |
+
else:
|
137 |
+
# For other commands or free-form text, simply display the assistant's response
|
138 |
+
return assistant_response
|
139 |
|
140 |
with gr.Blocks() as demo:
|
|
|
|
|
141 |
with gr.Row():
|
142 |
github_api_token = gr.Textbox(label="GitHub API Token", type="password")
|
143 |
github_username = gr.Textbox(label="GitHub Username")
|
144 |
github_repository = gr.Textbox(label="GitHub Repository")
|
145 |
|
146 |
+
system_message = gr.Textbox(
|
147 |
+
value="You are GitBot, the Github project guardian angel. You resolve issues and propose implementation of feature requests",
|
148 |
+
label="System message",
|
149 |
+
)
|
|
|
|
|
150 |
|
151 |
+
model_dropdown = gr.Dropdown(
|
152 |
+
choices=[
|
153 |
+
"microsoft/CodeBERT-base",
|
154 |
+
"Salesforce/codegen-45M-mono",
|
155 |
+
|
156 |
+
],
|
157 |
+
label="Select Model for Issue Resolution",
|
158 |
+
value=DEFAULT_MODEL,
|
159 |
+
)
|
160 |
|
161 |
+
severity_dropdown = gr.Dropdown(
|
162 |
+
choices=["Critical", "Major", "Minor", "Trivial"],
|
163 |
+
label="Severity",
|
164 |
+
value=None,
|
165 |
+
)
|
166 |
+
|
167 |
+
programming_language_textbox = gr.Textbox(label="Programming Language")
|
168 |
+
|
169 |
+
command_dropdown = gr.Dropdown(
|
170 |
+
choices=[
|
171 |
+
"/github",
|
172 |
+
"/help",
|
173 |
+
"/generate_code",
|
174 |
+
"/explain_concept",
|
175 |
+
"/write_documentation",
|
176 |
+
"/translate_code",
|
177 |
+
],
|
178 |
+
label="Select Command",
|
179 |
+
)
|
180 |
+
|
181 |
+
chatbot = MyChatbot(
|
182 |
respond,
|
183 |
additional_inputs=[
|
184 |
+
command_dropdown,
|
185 |
+
system_message,
|
186 |
+
gr.Slider(minimum=1, maximum=8192, value=2048, step=1, label="Max new tokens"),
|
187 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.71, step=0.1, label="Temperature"),
|
188 |
+
gr.Slider(
|
189 |
+
minimum=0.1,
|
190 |
+
maximum=1.0,
|
191 |
+
value=0.95,
|
192 |
+
step=0.01,
|
193 |
+
label="Top-p (nucleus sampling)",
|
194 |
+
),
|
195 |
github_api_token,
|
196 |
github_username,
|
197 |
github_repository,
|
198 |
model_dropdown,
|
199 |
severity_dropdown,
|
200 |
programming_language_textbox,
|
|
|
|
|
|
|
201 |
],
|
202 |
)
|
203 |
|
204 |
+
if __name__ == "__main__":
|
205 |
+
demo.queue().launch(
|
206 |
+
share=True,
|
207 |
+
server_name="0.0.0.0",
|
208 |
+
server_port=7860
|
209 |
+
)
|