acecalisto3 commited on
Commit
58b349f
·
verified ·
1 Parent(s): 73622f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -83
app.py CHANGED
@@ -3,41 +3,37 @@ from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
3
  from sentence_transformers import SentenceTransformer, util
4
  import os
5
  import requests
 
6
 
7
- # Constants for enhanced organization
8
  GITHUB_API_BASE_URL = "https://api.github.com/repos"
9
- DEFAULT_MODEL = "microsoft/CodeBERT-base"
10
- MAX_RELATED_ISSUES = 3
 
11
 
12
- # Load a pre-trained model for sentence similarity
13
- similarity_model = SentenceTransformer('all-mpnet-base-v2')
14
-
15
- # Define models for issue analysis
16
- model_name = "enricoros/big-agi"
17
  tokenizer = AutoTokenizer.from_pretrained(model_name)
18
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
 
 
 
19
 
20
  def analyze_issues(issue_text: str, model_name: str, severity: str = None, programming_language: str = None) -> dict:
21
- # Initialize the model
22
  model = pipeline("text-generation", model=model_name)
23
-
24
- # Generate a response
25
  response = model(
26
- f"{issue_text}\nAssistant: ",
27
- max_length=512,
28
  do_sample=True,
29
- temperature=0.7,
30
- top_k=50,
31
- top_p=0.9,
32
  )
33
-
34
- # Extract the assistant's response
35
  assistant_response = response[0]['generated_text'].strip()
36
 
37
- # Analyze the response
38
  if "Severity" in assistant_response:
39
  severity = assistant_response.split(":")[1].strip()
40
-
41
  if "Programming Language" in assistant_response:
42
  programming_language = assistant_response.split(":")[1].strip()
43
 
@@ -48,109 +44,120 @@ def analyze_issues(issue_text: str, model_name: str, severity: str = None, progr
48
  }
49
 
50
  def find_related_issues(issue_text: str, issues: list) -> list:
51
- # Calculate the similarity between the issue and other issues
52
  issue_embedding = similarity_model.encode(issue_text)
53
  similarities = [util.cos_sim(issue_embedding, similarity_model.encode(issue['title'])) for issue in issues]
54
-
55
- # Sort the issues by similarity
56
  sorted_issues = sorted(enumerate(similarities), key=lambda x: x[1], reverse=True)
57
-
58
- # Select the top related issues
59
- related_issues = [issues[i] for i, similarity in sorted_issues[:MAX_RELATED_ISSUES]]
60
-
61
- return related_issues
62
 
63
  def fetch_github_issues(github_api_token: str, github_username: str, github_repository: str) -> list:
64
- # Fetch the issues from the GitHub API
65
  headers = {'Authorization': f'token {github_api_token}'}
66
  url = f"{GITHUB_API_BASE_URL}/{github_username}/{github_repository}/issues"
67
  response = requests.get(url, headers=headers)
68
-
69
- # Parse the JSON response
70
- issues = response.json()
71
-
72
- return issues
73
 
74
  def respond(
75
- command, history, system_message, max_tokens, temperature, top_p,
76
- github_api_token, github_username, github_repository,
77
- selected_model, severity, programming_language, *args, **kwargs
 
 
 
 
 
 
 
 
 
 
78
  ) -> dict:
79
- # Initialize the model
80
- model = pipeline("text-generation", model="enricoros/big-agi")
81
-
82
- # Generate a response
 
 
 
 
 
 
 
 
 
 
83
  response = model(
84
- f"{system_message}\n{command}\n{history}\n{github_username}/{github_repository}\nSeverity: {severity}\nProgramming Language: {programming_language}\nAssistant: ",
85
  max_length=max_tokens,
86
  do_sample=True,
87
  temperature=temperature,
88
- top_k=50,
89
- top_p=top_p,
90
  )
91
-
92
- # Extract the assistant's response
93
  assistant_response = response[0]['generated_text'].strip()
94
 
 
 
 
95
  return {
96
  'assistant_response': assistant_response,
97
- 'severity': severity,
98
- 'programming_language': programming_language,
99
  }
100
 
 
 
101
  with gr.Blocks() as demo:
 
 
102
  with gr.Row():
103
  github_api_token = gr.Textbox(label="GitHub API Token", type="password")
104
  github_username = gr.Textbox(label="GitHub Username")
105
  github_repository = gr.Textbox(label="GitHub Repository")
106
 
107
- system_message = gr.Textbox(
108
- value="You are GitBot, the Github project guardian angel. You resolve issues and propose implementation of feature requests",
109
- label="System message",
110
- )
111
-
112
- model_dropdown = gr.Dropdown(
113
- choices=["microsoft/CodeBERT-base", "Salesforce/codegen-45M-mono"],
114
- label="Select Model for Issue Resolution",
115
- value=DEFAULT_MODEL,
116
- )
117
-
118
- severity_dropdown = gr.Dropdown(
119
- choices=["Critical", "Major", "Minor", "Trivial"],
120
- label="Severity",
121
- value=None,
122
- )
123
 
124
- programming_language_textbox = gr.Textbox(label="Programming Language")
 
 
 
 
 
 
125
 
126
- command_dropdown = gr.Dropdown(
127
- choices=[
128
- "/github",
129
- "/help",
130
- "/generate_code",
131
- "/explain_concept",
132
- "/write_documentation",
133
- "/translate_code",
134
- ],
135
- label="Select Command",
136
- )
 
137
 
138
  chatbot = gr.Chatbot(
139
  respond,
140
  additional_inputs=[
141
- command_dropdown,
142
- system_message,
143
- gr.Slider(minimum=1, maximum=8192, value=2048, step=1, label="Max new tokens"),
144
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
145
- gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.1, label="Top-p (nucleus sampling)"),
146
  github_api_token,
147
  github_username,
148
  github_repository,
149
  model_dropdown,
150
  severity_dropdown,
151
  programming_language_textbox,
 
 
 
152
  ],
153
  )
154
 
155
- if __name__ == "__main__":
156
- demo.queue().launch(share=True, server_name="0.0.0.0", server_port=7860)
 
3
  from sentence_transformers import SentenceTransformer, util
4
  import os
5
  import requests
6
+ import json
7
 
8
+ # --- Constants ---
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
+ # --- Model Setup ---
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
+ # --- Functions ---
21
 
22
  def analyze_issues(issue_text: str, model_name: str, severity: str = None, programming_language: str = None) -> dict:
23
+ """Analyzes an issue description and extracts severity and programming language."""
24
  model = pipeline("text-generation", model=model_name)
 
 
25
  response = model(
26
+ f"{SYSTEM_MESSAGE}\n{issue_text}\nAssistant: ",
27
+ max_length=2048, # Adjust as needed
28
  do_sample=True,
29
+ temperature=0.7, # Adjust as needed
30
+ top_k=50, # Adjust as needed
 
31
  )
 
 
32
  assistant_response = response[0]['generated_text'].strip()
33
 
34
+ # Extract information from the response
35
  if "Severity" in assistant_response:
36
  severity = assistant_response.split(":")[1].strip()
 
37
  if "Programming Language" in assistant_response:
38
  programming_language = assistant_response.split(":")[1].strip()
39
 
 
44
  }
45
 
46
  def find_related_issues(issue_text: str, issues: list) -> list:
47
+ """Finds related issues based on text similarity."""
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
+ return [issues[i] for i, similarity in sorted_issues[:MAX_RELATED_ISSUES]]
 
 
 
 
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
+ if response.status_code == 200:
59
+ return response.json()
60
+ else:
61
+ return []
 
62
 
63
  def respond(
64
+ command: str,
65
+ history: str,
66
+ github_api_token: str,
67
+ github_username: str,
68
+ github_repository: str,
69
+ selected_model: str,
70
+ severity: str,
71
+ programming_language: str,
72
+ max_tokens: int,
73
+ temperature: float,
74
+ top_p: float,
75
+ *args,
76
+ **kwargs,
77
  ) -> dict:
78
+ """Generates a response based on the command, history, and other parameters."""
79
+ model = pipeline("text-generation", model=selected_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"{SYSTEM_MESSAGE}\n{command}\n{history}\n{github_username}/{github_repository}\n{severity}\n{programming_language}\nAssistant: ",
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': analyzed_data['severity'],
107
+ 'programming_language': analyzed_data['programming_language'],
108
  }
109
 
110
+ # --- Gradio Interface ---
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
+ with gr.Row():
121
+ model_dropdown = gr.Dropdown(
122
+ choices=["microsoft/CodeBERT-base", "Salesforce/codegen-350M-mono", "enricoros/big-agi"], # Add more models
123
+ label="Select Model for Issue Resolution",
124
+ value=DEFAULT_MODEL,
125
+ )
 
 
 
 
 
 
 
 
 
 
126
 
127
+ with gr.Row():
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
+ with gr.Row():
136
+ command_dropdown = gr.Dropdown(
137
+ choices=[
138
+ "/github",
139
+ "/help",
140
+ "/generate_code",
141
+ "/explain_concept",
142
+ "/write_documentation",
143
+ "/translate_code",
144
+ ],
145
+ label="Select Command",
146
+ )
147
 
148
  chatbot = gr.Chatbot(
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
+ demo.launch(share=True)