Spaces:
Running
Running
| import gradio as gr | |
| import os | |
| import time | |
| import tempfile | |
| import traceback | |
| from github import Github | |
| from openai import OpenAI | |
| # Mock functions for testing - replace these with your actual functions | |
| def get_repo_info(repo_input): | |
| return repo_input.split('/')[-2:] | |
| def clone_repo(owner, repo_name, temp_dir): | |
| return os.path.join(temp_dir, f"{owner}_{repo_name}") | |
| def analyze_code(repo_path): | |
| return {"files": 10, "lines": 1000} | |
| def analyze_issues(github_repo, max_issues): | |
| return [{"title": "Test Issue", "number": 1}] | |
| def analyze_pull_requests(github_repo, max_prs): | |
| return [{"title": "Test PR", "number": 1}] | |
| def llm_analyze_code(client, code_analysis): | |
| return "Code analysis summary" | |
| def llm_analyze_issues(client, issues_data, repo_url): | |
| return {"summary": "Issues analysis summary"} | |
| def llm_analyze_prs(client, prs_data, repo_url): | |
| return {"summary": "PRs analysis summary"} | |
| def llm_synthesize_findings(client, code_analysis, issues_analysis, pr_analysis): | |
| return "Synthesized findings" | |
| def generate_report(repo_info, code_analysis, issues_analysis, pr_analysis, final_analysis): | |
| return f""" | |
| # Repository Analysis Report for {repo_info['owner']}/{repo_info['repo_name']} | |
| ## Code Analysis | |
| {code_analysis['llm_analysis']} | |
| ## Issues Analysis | |
| {issues_analysis['summary']} | |
| ## Pull Requests Analysis | |
| {pr_analysis['summary']} | |
| ## Final Analysis | |
| {final_analysis} | |
| """ | |
| def analyze_github_repo(repo_input, github_token=None): | |
| def update_output(message): | |
| print(message) # Console log | |
| return message # Update Gradio interface | |
| try: | |
| if github_token: | |
| os.environ["GITHUB_TOKEN"] = github_token | |
| github_token = os.environ.get("GITHUB_TOKEN") | |
| if not github_token: | |
| return update_output("β Error: GITHUB_TOKEN environment variable not set.") | |
| openrouter_api_key = os.environ.get("OPENROUTER_API_KEY") | |
| if not openrouter_api_key: | |
| return update_output("β Error: OPENROUTER_API_KEY environment variable not set.") | |
| yield update_output("π Starting analysis...") | |
| owner, repo_name = get_repo_info(repo_input) | |
| repo_url = f"https://github.com/{owner}/{repo_name}" | |
| yield update_output(f"π Analyzing repository: {repo_url}") | |
| g = Github(github_token) | |
| github_repo = g.get_repo(f"{owner}/{repo_name}") | |
| client = OpenAI(api_key=openrouter_api_key, base_url="https://openrouter.ai/api/v1") | |
| max_issues = 4 | |
| max_prs = 4 | |
| with tempfile.TemporaryDirectory() as temp_dir: | |
| repo_path = clone_repo(owner, repo_name, temp_dir) | |
| yield update_output(f"π Cloned repository to temporary directory") | |
| yield update_output("π¬ Analyzing code structure...") | |
| code_analysis = analyze_code(repo_path) | |
| code_analysis['llm_analysis'] = llm_analyze_code(client, code_analysis) | |
| yield update_output("β Code analysis complete") | |
| yield update_output(f"π Analyzing issues (max {max_issues})...") | |
| issues_data = analyze_issues(github_repo, max_issues) | |
| issues_analysis = llm_analyze_issues(client, issues_data, repo_url) | |
| yield update_output("β Issues analysis complete") | |
| yield update_output(f"π Analyzing pull requests (max {max_prs})...") | |
| prs_data = analyze_pull_requests(github_repo, max_prs) | |
| pr_analysis = llm_analyze_prs(client, prs_data, repo_url) | |
| yield update_output("β Pull requests analysis complete") | |
| yield update_output("π§ Synthesizing findings...") | |
| final_analysis = llm_synthesize_findings( | |
| client, | |
| code_analysis.get('llm_analysis', ''), | |
| issues_analysis.get('summary', ''), | |
| pr_analysis.get('summary', '') | |
| ) | |
| yield update_output("β Findings synthesized") | |
| repo_info = { | |
| "owner": owner, | |
| "repo_name": repo_name, | |
| } | |
| yield update_output("π Generating report...") | |
| report = generate_report(repo_info, code_analysis, issues_analysis, pr_analysis, final_analysis) | |
| yield update_output("β Report generated") | |
| yield update_output("π Analysis complete! Here's the report:\n\n" + report) | |
| except Exception as e: | |
| error_message = f"β An error occurred: {str(e)}" | |
| traceback.print_exc() | |
| yield update_output(error_message) | |
| # Define the Gradio interface | |
| with gr.Blocks() as app: | |
| gr.Markdown("# GitHub Repository Analyzer") | |
| repo_input = gr.Textbox(label="Enter GitHub Repository Slug or URL") | |
| with gr.Accordion("Advanced Settings", open=False): | |
| github_token = gr.Textbox(label="GitHub Token (optional)", type="password") | |
| analyze_button = gr.Button("Analyze Repository") | |
| output = gr.Markdown(label="Analysis Output") | |
| analyze_button.click( | |
| analyze_github_repo, | |
| inputs=[repo_input, github_token], | |
| outputs=output, | |
| ) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| app.launch() |