Spaces:
Running
Running
import gradio as gr | |
import os | |
import time | |
import markdown | |
import tempfile | |
import traceback | |
from github_repo_analyzer import get_repo_info, clone_repo, analyze_code, analyze_issues, analyze_pull_requests, llm_analyze_code, llm_analyze_issues, llm_analyze_prs, llm_synthesize_findings, generate_report | |
from github import Github | |
from openai import OpenAI | |
# Emojis and fun statements for progress updates | |
PROGRESS_STEPS = [ | |
("π΅οΈββοΈ", "Investigating the GitHub realm..."), | |
("π§¬", "Decoding repository DNA..."), | |
("π", "Hunting for bugs and features..."), | |
("π", "Examining pull request tea leaves..."), | |
("π§ ", "Activating AI brain cells..."), | |
("π", "Crafting the legendary report..."), | |
] | |
def analyze_github_repo(repo_input, github_token=None): | |
if github_token: | |
os.environ["GITHUB_TOKEN"] = github_token | |
github_token = os.environ.get("GITHUB_TOKEN") | |
if not github_token: | |
return "<p>β Error: GITHUB_TOKEN environment variable not set.</p>", "" | |
openrouter_api_key = os.environ.get("OPENROUTER_API_KEY") | |
if not openrouter_api_key: | |
return "<p>β Error: OPENROUTER_API_KEY environment variable not set.</p>", "" | |
progress_html = "" | |
yield progress_html, "" # Initial empty output | |
for emoji, message in PROGRESS_STEPS: | |
progress_html += f"<p>{emoji} {message}</p>" | |
yield progress_html, "" | |
time.sleep(4) # Simulate work being done | |
try: | |
owner, repo_name = get_repo_info(repo_input) | |
repo_url = f"https://github.com/{owner}/{repo_name}" | |
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) | |
progress_html += "<p>π¬ Analyzing code structure...</p>" | |
yield progress_html, "" | |
code_analysis = analyze_code(repo_path) | |
code_analysis['llm_analysis'] = llm_analyze_code(client, code_analysis) | |
progress_html += f"<p>π Analyzing issues (max {max_issues})...</p>" | |
yield progress_html, "" | |
issues_data = analyze_issues(github_repo, max_issues) | |
issues_analysis = llm_analyze_issues(client, issues_data, repo_url) | |
progress_html += f"<p>π Analyzing pull requests (max {max_prs})...</p>" | |
yield progress_html, "" | |
prs_data = analyze_pull_requests(github_repo, max_prs) | |
pr_analysis = llm_analyze_prs(client, prs_data, repo_url) | |
progress_html += "<p>π§ Synthesizing findings...</p>" | |
yield progress_html, "" | |
final_analysis = llm_synthesize_findings( | |
client, | |
code_analysis.get('llm_analysis', ''), | |
issues_analysis.get('summary', ''), | |
pr_analysis.get('summary', '') | |
) | |
repo_info = { | |
"owner": owner, | |
"repo_name": repo_name, | |
} | |
progress_html += "<p>π Generating report...</p>" | |
yield progress_html, "" | |
report = generate_report(repo_info, code_analysis, issues_analysis, pr_analysis, final_analysis) | |
# Convert markdown to HTML | |
html_report = markdown.markdown(report) | |
return progress_html + "<p>β Analysis complete!</p>", html_report | |
except Exception as e: | |
error_message = f"<p>β An error occurred: {str(e)}</p>" | |
traceback.print_exc() | |
return progress_html + 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") | |
progress_output = gr.HTML(label="Progress") | |
report_output = gr.HTML(label="Analysis Report") | |
analyze_button.click( | |
analyze_github_repo, | |
inputs=[repo_input, github_token], | |
outputs=[progress_output, report_output], | |
) | |
# Launch the app | |
if __name__ == "__main__": | |
app.launch() |