Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,9 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import shutil
|
4 |
-
import logging
|
5 |
-
import time
|
6 |
import os
|
|
|
7 |
from datetime import datetime
|
8 |
-
|
9 |
-
import requests
|
10 |
-
import gradio as gr
|
11 |
-
import atexit
|
12 |
-
import subprocess
|
13 |
-
from urllib.parse import urlparse, quote
|
14 |
-
import webbrowser
|
15 |
-
from flask import Flask, request, jsonify, send_from_directory
|
16 |
-
from threading import Thread
|
17 |
|
18 |
device = "cuda"
|
19 |
|
@@ -56,296 +46,97 @@ def stream_chat(
|
|
56 |
eos_token_id=[128001, 128008, 128009],
|
57 |
streamer=streamer,
|
58 |
)
|
|
|
59 |
|
60 |
-
#
|
61 |
-
INPUT_DIRECTORY = 'input'
|
62 |
-
OUTPUT_DIRECTORY = 'output'
|
63 |
LOGS_DIRECTORY = 'logs'
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
logging.basicConfig(
|
77 |
-
level=logging.INFO,
|
78 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
79 |
-
handlers=[
|
80 |
-
logging.FileHandler(log_file),
|
81 |
-
logging.StreamHandler()
|
82 |
-
]
|
83 |
-
)
|
84 |
-
return logging.getLogger(__name__)
|
85 |
-
|
86 |
-
# Initialize environment and logger
|
87 |
-
initialize_environment(INPUT_DIRECTORY, OUTPUT_DIRECTORY)
|
88 |
-
logger = initialize_logger() # Initialize logger globally
|
89 |
-
|
90 |
-
# GitHub API handler
|
91 |
-
class GitHubAPI:
|
92 |
-
def __init__(self, token: str):
|
93 |
-
self.token = token
|
94 |
-
self.headers = {
|
95 |
-
'Authorization': f'token {token}',
|
96 |
-
'Accept': 'application/vnd.github.v3+json'
|
97 |
-
}
|
98 |
-
self.base_url = "https://api.github.com"
|
99 |
-
|
100 |
-
def _check_rate_limit(self) -> bool:
|
101 |
-
try:
|
102 |
-
response = requests.get(f"{self.base_url}/rate_limit", headers=self.headers)
|
103 |
-
response.raise_for_status()
|
104 |
-
limits = response.json()
|
105 |
-
remaining = limits['resources']['core']['remaining']
|
106 |
-
reset_time = limits['resources']['core']['reset']
|
107 |
-
|
108 |
-
if remaining < 10:
|
109 |
-
wait_time = max(0, reset_time - int(time.time()))
|
110 |
-
if wait_time > 0:
|
111 |
-
logger.warning(f"Rate limit nearly exceeded. Waiting {wait_time} seconds...")
|
112 |
-
time.sleep(wait_time)
|
113 |
-
return False
|
114 |
-
return True
|
115 |
-
except requests.exceptions.RequestException as e:
|
116 |
-
logger.error(f"Error checking rate limit: {str(e)}")
|
117 |
-
return True
|
118 |
-
|
119 |
-
def get_repository(self, owner: str, repo: str) -> Dict:
|
120 |
-
try:
|
121 |
-
response = requests.get(f"{self.base_url}/repos/{owner}/{repo}", headers=self.headers)
|
122 |
-
response.raise_for_status()
|
123 |
-
return response.json()
|
124 |
-
except requests.HTTPError as e:
|
125 |
-
logger.error(f"HTTP error getting repository info: {str(e)}")
|
126 |
-
raise
|
127 |
-
except Exception as e:
|
128 |
-
logger.error(f"Error getting repository info: {str(e)}")
|
129 |
-
raise
|
130 |
-
|
131 |
-
def get_issues(self, owner: str, repo: str, state: str = 'open') -> List[Dict]:
|
132 |
-
if not self._check_rate_limit():
|
133 |
-
return []
|
134 |
-
|
135 |
-
try:
|
136 |
-
response = requests.get(f"{self.base_url}/repos/{owner}/{repo}/issues", headers=self.headers, params={'state': state})
|
137 |
-
response.raise_for_status()
|
138 |
-
issues = response.json()
|
139 |
-
return [issue for issue in issues if 'pull_request' not in issue]
|
140 |
-
except Exception as e:
|
141 |
-
logger.error(f"Error fetching issues: {str(e)}")
|
142 |
-
return []
|
143 |
-
|
144 |
-
# GitHub Bot
|
145 |
-
class GitHubBot:
|
146 |
-
def __init__(self):
|
147 |
-
self.github_api = None
|
148 |
-
|
149 |
-
def initialize_api(self, token: str):
|
150 |
-
self.github_api = GitHubAPI(token)
|
151 |
-
|
152 |
-
def fetch_issues(self, token: str, owner: str, repo: str) -> List[Dict]:
|
153 |
-
try:
|
154 |
-
self.initialize_api(token)
|
155 |
-
return self.github_api.get_issues(owner, repo)
|
156 |
-
except Exception as e:
|
157 |
-
logger.error(f"Error fetching issues: {str(e)}")
|
158 |
-
return []
|
159 |
-
|
160 |
-
def resolve_issue(self, token: str, owner: str, repo: str, issue_number: int, resolution: str, forked_repo: str) -> str:
|
161 |
-
try:
|
162 |
-
self.initialize_api(token)
|
163 |
-
self.github_api.get_repository(owner, repo)
|
164 |
-
|
165 |
-
# Create resolution file
|
166 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
167 |
-
resolution_file = f"{RESOLUTIONS_DIRECTORY}/resolution_{issue_number}_{timestamp}.md"
|
168 |
-
|
169 |
-
with open(resolution_file, "w") as f:
|
170 |
-
f.write(f"# Resolution for Issue #{issue_number}\n\n{resolution}")
|
171 |
-
|
172 |
-
# Clone the forked repo
|
173 |
-
subprocess.run(['git', 'clone', forked_repo, '/tmp/' + forked_repo.split('/')[-1]], check=True)
|
174 |
-
|
175 |
-
# Change to the cloned directory
|
176 |
-
os.chdir('/tmp/' + forked_repo.split('/')[-1])
|
177 |
-
|
178 |
-
# Assuming manual intervention now
|
179 |
-
input(" Apply the fix manually and stage the changes (press ENTER)? ")
|
180 |
-
|
181 |
-
# Commit and push the modifications
|
182 |
-
subprocess.run(['git', 'add', '.'], check=True)
|
183 |
-
subprocess.run(['git', 'commit', '-m', f"Resolved issue #{issue_number} ({quote(resolution)})"], check=True)
|
184 |
-
subprocess.run(['git', 'push', 'origin', 'HEAD'], check=True)
|
185 |
-
|
186 |
-
# Open Pull Request page
|
187 |
-
webbrowser.open(f'https://github.com/{forked_repo.split("/")[-1]}/compare/master...{owner}:{forked_repo.split("/")[-1]}_resolved_issue_{issue_number}')
|
188 |
|
189 |
-
|
|
|
|
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
return result
|
200 |
|
201 |
-
def extract_info_from_url(url: str) -> Dict[str, Any]:
|
202 |
-
info = {}
|
203 |
try:
|
204 |
-
|
|
|
|
|
205 |
response.raise_for_status()
|
206 |
-
|
207 |
-
info
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
parts = parsed_url.path.split('/')
|
213 |
-
if len(parts) > 2:
|
214 |
-
owner = parts[1]
|
215 |
-
repo = parts[2]
|
216 |
-
issues = bot.fetch_issues(github_token, owner, repo)
|
217 |
-
info['issues'] = issues
|
218 |
-
elif 'huggingface.co' in parsed_url.netloc:
|
219 |
-
# Add Hugging Face specific handling if needed
|
220 |
-
pass
|
221 |
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
bot = GitHubBot()
|
230 |
-
|
231 |
-
# Define missing functions with validation
|
232 |
-
def fetch_issues(token, repo_url):
|
233 |
-
try:
|
234 |
-
parts = repo_url.split('/')
|
235 |
-
if len(parts) < 2:
|
236 |
-
raise ValueError("Repository URL is not in the correct format. Expected format: 'owner/repo'.")
|
237 |
-
|
238 |
-
owner, repo = parts[-2], parts[-1]
|
239 |
-
issues = bot.fetch_issues(token, owner, repo)
|
240 |
-
return issues
|
241 |
-
except Exception as e:
|
242 |
-
return str(e)
|
243 |
-
|
244 |
-
def resolve_issue(token, repo_url, issue_number, resolution, forked_repo_url):
|
245 |
-
try:
|
246 |
-
parts = repo_url.split('/')
|
247 |
-
if len(parts) < 2:
|
248 |
-
raise ValueError("Repository URL is not in the correct format. Expected format: 'owner/repo'.")
|
249 |
-
|
250 |
-
owner, repo = parts[-2], parts[-1]
|
251 |
-
result = bot.resolve_issue(token, owner, repo, issue_number, resolution, forked_repo_url)
|
252 |
-
return result
|
253 |
-
except Exception as e:
|
254 |
-
return str(e)
|
255 |
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
return info
|
260 |
-
except Exception as e:
|
261 |
-
return str(e)
|
262 |
|
263 |
-
def submit_issue(token, repo_url, issue_number, resolution, forked_repo_url):
|
264 |
try:
|
265 |
-
|
266 |
-
|
267 |
-
raise ValueError("Repository URL is not in the correct format. Expected format: 'owner/repo'.")
|
268 |
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
except Exception as e:
|
273 |
-
return str(e)
|
274 |
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
def serve_index():
|
279 |
-
return send_from_directory('.', 'index.html')
|
280 |
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
token = data.get('token')
|
285 |
-
repo_url = data.get('repoUrl')
|
286 |
-
if not token or not repo_url:
|
287 |
-
return jsonify({'error': 'Missing token or repoUrl'}), 400
|
288 |
-
issues = fetch_issues(token, repo_url)
|
289 |
-
return jsonify({'issues': issues})
|
290 |
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
issue_number = data.get('issueNumber')
|
297 |
-
resolution = data.get('resolution')
|
298 |
-
forked_repo_url = data.get('forkedRepoUrl')
|
299 |
-
if not token or not repo_url or not issue_number or not resolution:
|
300 |
-
return jsonify({'error': 'Missing required fields'}), 400
|
301 |
-
result = resolve_issue(token, repo_url, issue_number, resolution, forked_repo_url)
|
302 |
-
return jsonify({'result': result})
|
303 |
|
304 |
@app.route('/extract_info', methods=['POST'])
|
305 |
-
def
|
306 |
-
|
307 |
-
url = data.get('url')
|
308 |
if not url:
|
309 |
-
|
310 |
-
|
311 |
-
return jsonify(info)
|
312 |
-
|
313 |
-
def run_flask_app():
|
314 |
-
app.run(debug=True, use_reloader=False)
|
315 |
-
|
316 |
-
def create_gradio_interface():
|
317 |
-
with gr.Blocks(css=None, theme=None) as demo:
|
318 |
-
gr.HTML(custom_html)
|
319 |
-
|
320 |
-
return demo
|
321 |
-
|
322 |
-
# Cleanup function
|
323 |
-
def cleanup():
|
324 |
try:
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
except
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
cleanup()
|
337 |
-
sys.exit(0)
|
338 |
-
|
339 |
-
if __name__ == "__main__":
|
340 |
-
# Register cleanup handlers
|
341 |
-
atexit.register(cleanup)
|
342 |
-
signal.signal(signal.SIGINT, signal_handler)
|
343 |
-
signal.signal(signal.SIGTERM, signal_handler)
|
344 |
-
|
345 |
-
# Run Flask app in a separate thread
|
346 |
-
flask_thread = Thread(target=run_flask_app)
|
347 |
-
flask_thread.start()
|
348 |
-
|
349 |
-
# Create Gradio interface
|
350 |
-
demo = create_gradio_interface()
|
351 |
-
demo.launch()
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
import requests
|
|
|
|
|
|
|
3 |
import os
|
4 |
+
import logging
|
5 |
from datetime import datetime
|
6 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
device = "cuda"
|
9 |
|
|
|
46 |
eos_token_id=[128001, 128008, 128009],
|
47 |
streamer=streamer,
|
48 |
)
|
49 |
+
app = Flask(__name__)
|
50 |
|
51 |
+
# Configure logging
|
|
|
|
|
52 |
LOGS_DIRECTORY = 'logs'
|
53 |
+
if not os.path.exists(LOGS_DIRECTORY):
|
54 |
+
os.makedirs(LOGS_DIRECTORY)
|
55 |
+
|
56 |
+
logging.basicConfig(
|
57 |
+
level=logging.INFO,
|
58 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
59 |
+
handlers=[
|
60 |
+
logging.FileHandler(f"{LOGS_DIRECTORY}/github_bot_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log"),
|
61 |
+
logging.StreamHandler()
|
62 |
+
]
|
63 |
+
)
|
64 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
@app.route('/')
|
67 |
+
def index():
|
68 |
+
return render_template('index.html')
|
69 |
|
70 |
+
@app.route('/fetch_issues', methods=['POST'])
|
71 |
+
def fetch_issues():
|
72 |
+
github_token = request.form.get('github_token')
|
73 |
+
repo_url = request.form.get('repo_url')
|
74 |
|
75 |
+
if not github_token or not repo_url:
|
76 |
+
logger.error('GitHub Token and Repository URL are required')
|
77 |
+
return jsonify({'error': 'GitHub Token and Repository URL are required'}), 400
|
|
|
78 |
|
|
|
|
|
79 |
try:
|
80 |
+
repo_owner, repo_name = repo_url.split('/')[-2:]
|
81 |
+
headers = {'Authorization': f'token {github_token}'}
|
82 |
+
response = requests.get(f'https://api.github.com/repos/{repo_owner}/{repo_name}/issues', headers=headers)
|
83 |
response.raise_for_status()
|
84 |
+
issues = response.json()
|
85 |
+
logger.info(f'Successfully fetched issues for {repo_owner}/{repo_name}')
|
86 |
+
return jsonify(issues)
|
87 |
+
except requests.exceptions.RequestException as e:
|
88 |
+
logger.error(f'Error fetching issues: {str(e)}')
|
89 |
+
return jsonify({'error': str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
+
@app.route('/resolve_issue', methods=['POST'])
|
92 |
+
def resolve_issue():
|
93 |
+
github_token = request.form.get('github_token')
|
94 |
+
issue_number = request.form.get('issue_number')
|
95 |
+
resolution = request.form.get('resolution')
|
96 |
+
repo_url = request.form.get('repo_url')
|
97 |
+
forked_repo_url = request.form.get('forked_repo_url')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
+
if not github_token or not issue_number or not resolution or not repo_url:
|
100 |
+
logger.error('GitHub Token, Issue Number, Resolution, and Repository URL are required')
|
101 |
+
return jsonify({'error': 'GitHub Token, Issue Number, Resolution, and Repository URL are required'}), 400
|
|
|
|
|
|
|
102 |
|
|
|
103 |
try:
|
104 |
+
repo_owner, repo_name = repo_url.split('/')[-2:]
|
105 |
+
headers = {'Authorization': f'token {github_token}'}
|
|
|
106 |
|
107 |
+
# Create a comment with the resolution in the issue
|
108 |
+
comment_data = {"body": resolution}
|
109 |
+
requests.post(f'https://api.github.com/repos/{repo_owner}/{repo_name}/issues/{issue_number}/comments', headers=headers, json=comment_data)
|
|
|
|
|
110 |
|
111 |
+
if forked_repo_url:
|
112 |
+
# Extract the forked repo's owner and name
|
113 |
+
forked_repo_owner, forked_repo_name = forked_repo_url.split('/')[-2:]
|
|
|
|
|
114 |
|
115 |
+
# Close the issue in your forked repository
|
116 |
+
close_issue_data = {"state": "closed"}
|
117 |
+
requests.patch(f'https://api.github.com/repos/{forked_repo_owner}/{forked_repo_name}/issues/{issue_number}', headers=headers, json=close_issue_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
+
logger.info(f'Issue #{issue_number} resolved successfully')
|
120 |
+
return jsonify({'message': f'Issue #{issue_number} resolved successfully'})
|
121 |
+
except requests.exceptions.RequestException as e:
|
122 |
+
logger.error(f'Error resolving issue: {str(e)}')
|
123 |
+
return jsonify({'error': str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
@app.route('/extract_info', methods=['POST'])
|
126 |
+
def extract_info():
|
127 |
+
url = request.form.get('url')
|
|
|
128 |
if not url:
|
129 |
+
logger.error('URL is required')
|
130 |
+
return jsonify({'error': 'URL is required'}), 400
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
try:
|
132 |
+
headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"}
|
133 |
+
response = requests.get(url, headers=headers)
|
134 |
+
response.raise_for_status()
|
135 |
+
logger.info(f'Successfully extracted info from {url}')
|
136 |
+
return jsonify({"status": "success", "response": response.text})
|
137 |
+
except requests.exceptions.RequestException as e:
|
138 |
+
logger.error(f'Error extracting info: {str(e)}')
|
139 |
+
return jsonify({'error': str(e)}), 500
|
140 |
+
|
141 |
+
if __name__ == '__main__':
|
142 |
+
app.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|