acecalisto3 commited on
Commit
51e2f34
·
verified ·
1 Parent(s): e663d46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -73
app.py CHANGED
@@ -18,35 +18,69 @@ max_tokens = 2048
18
  temperature = 0.71
19
  top_p = 0.95
20
 
21
- class InferenceClient:
22
- def __init__(self):
23
- pass
 
 
 
 
24
 
25
- def create_endpoint(self, repo_id, handler_path, model_id, task, description, hyperparameters):
26
- pass
 
 
 
 
 
 
 
 
 
27
 
28
- def update_endpoint(self, repo_id, handler_path, model_id, task, description, hyperparameters):
29
- pass
 
 
 
 
 
 
 
 
 
 
30
 
31
- def delete_endpoint(self, repo_id, handler_path):
32
- pass
33
-
34
- def list_endpoints(self):
35
- pass
36
-
37
- def get_endpoint_status(self, repo_id, handler_path):
38
- pass
39
-
40
- def get_endpoint_logs(self, repo_id, handler_path, num_lines):
41
- pass
42
-
43
- def get_endpoint_metrics(self, repo_id, handler_path):
44
- pass
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  class MyChatbot(gr.Chatbot):
47
  """Custom Chatbot class for enhanced functionality."""
48
  def __init__(self, **kwargs):
49
- super().__init__(type="messages", **kwargs) # Set type to 'messages'
50
  self.issues = []
51
  self.current_issue = None
52
 
@@ -69,6 +103,7 @@ class MyChatbot(gr.Chatbot):
69
  }
70
  ]
71
 
 
72
  with gr.Blocks() as demo:
73
  with gr.Row():
74
  github_api_token = gr.Textbox(label="GitHub API Token", type="password")
@@ -80,23 +115,22 @@ with gr.Blocks() as demo:
80
  label="System message",
81
  )
82
 
83
- model_dropdown = gr.Dropdown(
84
- choices=[
85
- "mistralai/Mixtral-8x7B-Instruct-v0.1",
86
- "Gabriel/Swe-review-setfit-model",
87
- "OpenBMB/multilingual-codeparrot"
88
- ],
89
- label="Select Model for Issue Resolution",
90
- value="OpenBMB/multilingual-codeparrot",
91
- )
92
-
93
- severity_dropdown = gr.Dropdown(
94
- choices=["Critical", "Major", "Minor", "Trivial"],
95
- label="Severity",
96
- value=None,
97
- )
98
-
99
- programming_language_textbox = gr.Textbox(label="Programming Language")
100
 
101
  # Create the chatbot instance
102
  chatbot = MyChatbot()
@@ -107,11 +141,11 @@ with gr.Blocks() as demo:
107
  # Create state for storing conversation history
108
  state = gr.State([])
109
 
110
- # Add buttons with correct syntax
111
- fetch_issues_button = gr.Button("Fetch Issues")
112
- resolve_issue_button = gr.Button("Resolve Issue")
113
- clear_button = gr.Button("Clear Chat")
114
- submit_button = gr.Button("Submit Message")
115
 
116
  # Add a dropdown to select an issue
117
  issue_dropdown = gr.Dropdown(
@@ -120,38 +154,80 @@ with gr.Blocks() as demo:
120
  interactive=True
121
  )
122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  def clear_chat():
124
  return [], []
125
 
126
  def fetch_issues(api_token, username, repo):
 
127
  issues = fetch_github_issues(api_token, username, repo)
128
  chatbot.issues = issues
129
  return gr.Dropdown(choices=[f"{i+1}. {issue['title']}" for i, issue in enumerate(issues)])
130
 
131
- # Connect button events
132
- fetch_issues_button.click(
133
- fetch_issues,
134
- inputs=[github_api_token, github_username, github_repository],
135
- outputs=[issue_dropdown]
136
- )
 
 
 
 
 
 
 
 
 
137
 
138
- clear_button.click(
139
- clear_chat,
140
- outputs=[chatbot, state]
141
- )
 
 
142
 
143
- submit_button.click(
144
- user,
145
- [msg, chatbot],
146
- [msg, chatbot]
147
- ).then(
148
  bot,
149
  [
150
  chatbot,
151
  system_message,
152
- gr.Slider(minimum=1, maximum=8192, value=2048, step=1, label="Max new tokens"),
153
- gr.Slider(minimum=0.1, maximum=4.0, value=0.71, step=0.1, label="Temperature"),
154
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.01, label="Top-p"),
155
  github_api_token,
156
  github_username,
157
  github_repository,
@@ -162,19 +238,14 @@ with gr.Blocks() as demo:
162
  [chatbot]
163
  )
164
 
165
- # Add message input handler
166
- msg.submit(
167
- user,
168
- [msg, chatbot],
169
- [msg, chatbot]
170
- ).then(
171
  bot,
172
  [
173
  chatbot,
174
  system_message,
175
- gr.Slider(minimum=1, maximum=8192, value=2048, step=1, label="Max new tokens"),
176
- gr.Slider(minimum=0.1, maximum=4.0, value=0.71, step=0.1, label="Temperature"),
177
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.01, label="Top-p"),
178
  github_api_token,
179
  github_username,
180
  github_repository,
@@ -185,14 +256,23 @@ with gr.Blocks() as demo:
185
  [chatbot]
186
  )
187
 
188
- # Issue selection handler
 
 
 
 
 
 
 
 
 
 
189
  issue_dropdown.change(
190
  select_issue,
191
  inputs=[issue_dropdown, chatbot],
192
  outputs=[chatbot]
193
  )
194
 
195
- # Issue resolution handler
196
  resolve_issue_button.click(
197
  resolve_selected_issue,
198
  inputs=[
 
18
  temperature = 0.71
19
  top_p = 0.95
20
 
21
+ def fetch_github_issues(github_api_token: str, github_username: str, github_repository: str) -> list:
22
+ """Fetches issues from a specified GitHub repository using the GitHub API."""
23
+ headers = {'Authorization': f'token {github_api_token}'}
24
+ url = f"https://api.github.com/repos/{github_username}/{github_repository}/issues"
25
+ response = requests.get(url, headers=headers)
26
+ issues = response.json()
27
+ return issues
28
 
29
+ def resolve_issue(github_api_token: str, github_username: str, github_repository: str, issue_number: int, resolution: str):
30
+ """Resolves an issue by pushing a commit with the resolution."""
31
+ try:
32
+ issue_text = fetch_github_issues(github_api_token, github_username, github_repository)[issue_number]['body']
33
+ commit_message = f"Resolved issue {issue_number}: {issue_text}"
34
+ commit_file = "resolution.txt"
35
+ with open(commit_file, "w") as f:
36
+ f.write(resolution)
37
+ return push_to_repo(github_api_token, github_username, github_repository, commit_message, commit_file)
38
+ except Exception as e:
39
+ return f"Error resolving issue: {e}"
40
 
41
+ def push_to_repo(github_api_token: str, github_username: str, github_repository: str, commit_message: str, commit_file: str):
42
+ """Pushes changes to a GitHub repository."""
43
+ try:
44
+ repo = git.Repo.clone_from(f"https://github.com/{github_username}/{github_repository}.git", f"{github_repository}")
45
+ repo.git.add(commit_file)
46
+ repo.git.commit(m=commit_message)
47
+ repo.git.push()
48
+ return f"Changes pushed to {github_repository} successfully."
49
+ except gitdb.exc.InvalidGitRepositoryError:
50
+ return f"Invalid repository: {github_repository}"
51
+ except Exception as e:
52
+ return f"Error pushing to repository: {e}"
53
 
54
+ def respond(command, history, system_message, max_tokens, temperature, top_p, github_api_token, github_username, github_repository, selected_model, severity, programming_language):
55
+ """Generate a response using the selected model."""
56
+ try:
57
+ model = pipeline("text-generation", model=selected_model)
58
+ prompt = f"{system_message}\nUser: {command}\nAssistant:"
59
+
60
+ response = model(
61
+ prompt,
62
+ max_length=max_tokens,
63
+ do_sample=True,
64
+ temperature=temperature,
65
+ top_p=top_p,
66
+ )
67
+
68
+ return {
69
+ 'assistant_response': response[0]['generated_text'].split("Assistant:")[-1].strip(),
70
+ 'severity': severity,
71
+ 'programming_language': programming_language
72
+ }
73
+ except Exception as e:
74
+ return {
75
+ 'assistant_response': f"Error generating response: {str(e)}",
76
+ 'severity': severity,
77
+ 'programming_language': programming_language
78
+ }
79
 
80
  class MyChatbot(gr.Chatbot):
81
  """Custom Chatbot class for enhanced functionality."""
82
  def __init__(self, **kwargs):
83
+ super().__init__(type="messages", **kwargs)
84
  self.issues = []
85
  self.current_issue = None
86
 
 
103
  }
104
  ]
105
 
106
+ # Main interface
107
  with gr.Blocks() as demo:
108
  with gr.Row():
109
  github_api_token = gr.Textbox(label="GitHub API Token", type="password")
 
115
  label="System message",
116
  )
117
 
118
+ with gr.Row():
119
+ model_dropdown = gr.Dropdown(
120
+ choices=[
121
+ "mistralai/Mixtral-8x7B-Instruct-v0.1",
122
+ "Gabriel/Swe-review-setfit-model",
123
+ "OpenBMB/multilingual-codeparrot"
124
+ ],
125
+ label="Select Model for Issue Resolution",
126
+ value="OpenBMB/multilingual-codeparrot",
127
+ )
128
+ severity_dropdown = gr.Dropdown(
129
+ choices=["Critical", "Major", "Minor", "Trivial"],
130
+ label="Severity",
131
+ value=None,
132
+ )
133
+ programming_language_textbox = gr.Textbox(label="Programming Language")
 
134
 
135
  # Create the chatbot instance
136
  chatbot = MyChatbot()
 
141
  # Create state for storing conversation history
142
  state = gr.State([])
143
 
144
+ with gr.Row():
145
+ fetch_issues_button = gr.Button("Fetch Issues")
146
+ resolve_issue_button = gr.Button("Resolve Issue")
147
+ clear_button = gr.Button("Clear Chat")
148
+ submit_button = gr.Button("Submit Message")
149
 
150
  # Add a dropdown to select an issue
151
  issue_dropdown = gr.Dropdown(
 
154
  interactive=True
155
  )
156
 
157
+ with gr.Row():
158
+ max_tokens_slider = gr.Slider(minimum=1, maximum=8192, value=2048, step=1, label="Max new tokens")
159
+ temperature_slider = gr.Slider(minimum=0.1, maximum=4.0, value=0.71, step=0.1, label="Temperature")
160
+ top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.01, label="Top-p")
161
+
162
+ def user(user_message, history):
163
+ """Handle user messages"""
164
+ return "", history + [[user_message, None]]
165
+
166
+ def bot(history, system_msg, max_tokens, temp, top_p, api_token, username, repo, model, severity, lang):
167
+ """Generate bot response"""
168
+ if len(history) == 0:
169
+ return history
170
+
171
+ user_message = history[-1][0]
172
+ response = respond(
173
+ command=user_message,
174
+ history=history[:-1],
175
+ system_message=system_msg,
176
+ max_tokens=max_tokens,
177
+ temperature=temp,
178
+ top_p=top_p,
179
+ github_api_token=api_token,
180
+ github_username=username,
181
+ github_repository=repo,
182
+ selected_model=model,
183
+ severity=severity,
184
+ programming_language=lang
185
+ )
186
+
187
+ history[-1][1] = response['assistant_response']
188
+ return history
189
+
190
  def clear_chat():
191
  return [], []
192
 
193
  def fetch_issues(api_token, username, repo):
194
+ """Fetch GitHub issues and update the chatbot"""
195
  issues = fetch_github_issues(api_token, username, repo)
196
  chatbot.issues = issues
197
  return gr.Dropdown(choices=[f"{i+1}. {issue['title']}" for i, issue in enumerate(issues)])
198
 
199
+ def select_issue(selected, chatbot):
200
+ """Handle issue selection"""
201
+ if selected:
202
+ issue_num = int(selected.split('.')[0]) - 1
203
+ chatbot.current_issue = chatbot.issues[issue_num]
204
+ return chatbot.postprocess({
205
+ 'command': str(issue_num + 1),
206
+ 'github_api_token': github_api_token.value,
207
+ 'github_username': github_username.value,
208
+ 'github_repository': github_repository.value,
209
+ 'selected_model': model_dropdown.value,
210
+ 'severity': severity_dropdown.value,
211
+ 'programming_language': programming_language_textbox.value
212
+ })
213
+ return []
214
 
215
+ def resolve_selected_issue(api_token, username, repo, resolution):
216
+ """Resolve the currently selected issue"""
217
+ if chatbot.current_issue:
218
+ issue_number = chatbot.issues.index(chatbot.current_issue)
219
+ return resolve_issue(api_token, username, repo, issue_number, resolution)
220
+ return "No issue selected"
221
 
222
+ # Connect all the components
223
+ msg.submit(user, [msg, chatbot], [msg, chatbot]).then(
 
 
 
224
  bot,
225
  [
226
  chatbot,
227
  system_message,
228
+ max_tokens_slider,
229
+ temperature_slider,
230
+ top_p_slider,
231
  github_api_token,
232
  github_username,
233
  github_repository,
 
238
  [chatbot]
239
  )
240
 
241
+ submit_button.click(user, [msg, chatbot], [msg, chatbot]).then(
 
 
 
 
 
242
  bot,
243
  [
244
  chatbot,
245
  system_message,
246
+ max_tokens_slider,
247
+ temperature_slider,
248
+ top_p_slider,
249
  github_api_token,
250
  github_username,
251
  github_repository,
 
256
  [chatbot]
257
  )
258
 
259
+ fetch_issues_button.click(
260
+ fetch_issues,
261
+ inputs=[github_api_token, github_username, github_repository],
262
+ outputs=[issue_dropdown]
263
+ )
264
+
265
+ clear_button.click(
266
+ clear_chat,
267
+ outputs=[chatbot, state]
268
+ )
269
+
270
  issue_dropdown.change(
271
  select_issue,
272
  inputs=[issue_dropdown, chatbot],
273
  outputs=[chatbot]
274
  )
275
 
 
276
  resolve_issue_button.click(
277
  resolve_selected_issue,
278
  inputs=[