Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,76 +3,43 @@ import os
|
|
| 3 |
import time
|
| 4 |
import tempfile
|
| 5 |
import traceback
|
|
|
|
| 6 |
from github import Github
|
| 7 |
from openai import OpenAI
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
def
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return [{"title": "Test Issue", "number": 1}]
|
| 21 |
-
|
| 22 |
-
def analyze_pull_requests(github_repo, max_prs):
|
| 23 |
-
return [{"title": "Test PR", "number": 1}]
|
| 24 |
-
|
| 25 |
-
def llm_analyze_code(client, code_analysis):
|
| 26 |
-
return "Code analysis summary"
|
| 27 |
-
|
| 28 |
-
def llm_analyze_issues(client, issues_data, repo_url):
|
| 29 |
-
return {"summary": "Issues analysis summary"}
|
| 30 |
-
|
| 31 |
-
def llm_analyze_prs(client, prs_data, repo_url):
|
| 32 |
-
return {"summary": "PRs analysis summary"}
|
| 33 |
-
|
| 34 |
-
def llm_synthesize_findings(client, code_analysis, issues_analysis, pr_analysis):
|
| 35 |
-
return "Synthesized findings"
|
| 36 |
-
|
| 37 |
-
def generate_report(repo_info, code_analysis, issues_analysis, pr_analysis, final_analysis):
|
| 38 |
-
return f"""
|
| 39 |
-
# Repository Analysis Report for {repo_info['owner']}/{repo_info['repo_name']}
|
| 40 |
-
|
| 41 |
-
## Code Analysis
|
| 42 |
-
{code_analysis['llm_analysis']}
|
| 43 |
-
|
| 44 |
-
## Issues Analysis
|
| 45 |
-
{issues_analysis['summary']}
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
"""
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
try:
|
| 60 |
-
if github_token:
|
| 61 |
-
os.environ["GITHUB_TOKEN"] = github_token
|
| 62 |
-
|
| 63 |
-
github_token = os.environ.get("GITHUB_TOKEN")
|
| 64 |
-
if not github_token:
|
| 65 |
-
return update_output("β Error: GITHUB_TOKEN environment variable not set.")
|
| 66 |
-
|
| 67 |
-
openrouter_api_key = os.environ.get("OPENROUTER_API_KEY")
|
| 68 |
-
if not openrouter_api_key:
|
| 69 |
-
return update_output("β Error: OPENROUTER_API_KEY environment variable not set.")
|
| 70 |
-
|
| 71 |
-
yield update_output("π Starting analysis...")
|
| 72 |
-
|
| 73 |
owner, repo_name = get_repo_info(repo_input)
|
| 74 |
repo_url = f"https://github.com/{owner}/{repo_name}"
|
| 75 |
-
yield update_output(f"π Analyzing repository: {repo_url}")
|
| 76 |
|
| 77 |
g = Github(github_token)
|
| 78 |
github_repo = g.get_repo(f"{owner}/{repo_name}")
|
|
@@ -84,46 +51,46 @@ def analyze_github_repo(repo_input, github_token=None):
|
|
| 84 |
|
| 85 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 86 |
repo_path = clone_repo(owner, repo_name, temp_dir)
|
| 87 |
-
yield update_output(f"π Cloned repository to temporary directory")
|
| 88 |
|
| 89 |
-
|
|
|
|
| 90 |
code_analysis = analyze_code(repo_path)
|
| 91 |
code_analysis['llm_analysis'] = llm_analyze_code(client, code_analysis)
|
| 92 |
-
yield update_output("β
Code analysis complete")
|
| 93 |
|
| 94 |
-
|
|
|
|
| 95 |
issues_data = analyze_issues(github_repo, max_issues)
|
| 96 |
issues_analysis = llm_analyze_issues(client, issues_data, repo_url)
|
| 97 |
-
yield update_output("β
Issues analysis complete")
|
| 98 |
|
| 99 |
-
|
|
|
|
| 100 |
prs_data = analyze_pull_requests(github_repo, max_prs)
|
| 101 |
pr_analysis = llm_analyze_prs(client, prs_data, repo_url)
|
| 102 |
-
yield update_output("β
Pull requests analysis complete")
|
| 103 |
|
| 104 |
-
|
|
|
|
| 105 |
final_analysis = llm_synthesize_findings(
|
| 106 |
client,
|
| 107 |
code_analysis.get('llm_analysis', ''),
|
| 108 |
issues_analysis.get('summary', ''),
|
| 109 |
pr_analysis.get('summary', '')
|
| 110 |
)
|
| 111 |
-
yield update_output("β
Findings synthesized")
|
| 112 |
|
| 113 |
repo_info = {
|
| 114 |
"owner": owner,
|
| 115 |
"repo_name": repo_name,
|
| 116 |
}
|
| 117 |
|
| 118 |
-
|
|
|
|
| 119 |
report = generate_report(repo_info, code_analysis, issues_analysis, pr_analysis, final_analysis)
|
| 120 |
-
yield update_output("β
Report generated")
|
| 121 |
|
| 122 |
-
|
|
|
|
| 123 |
except Exception as e:
|
| 124 |
error_message = f"β An error occurred: {str(e)}"
|
| 125 |
traceback.print_exc()
|
| 126 |
-
yield
|
| 127 |
|
| 128 |
# Define the Gradio interface
|
| 129 |
with gr.Blocks() as app:
|
|
@@ -136,12 +103,13 @@ with gr.Blocks() as app:
|
|
| 136 |
|
| 137 |
analyze_button = gr.Button("Analyze Repository")
|
| 138 |
|
| 139 |
-
|
|
|
|
| 140 |
|
| 141 |
analyze_button.click(
|
| 142 |
analyze_github_repo,
|
| 143 |
inputs=[repo_input, github_token],
|
| 144 |
-
outputs=
|
| 145 |
)
|
| 146 |
|
| 147 |
# Launch the app
|
|
|
|
| 3 |
import time
|
| 4 |
import tempfile
|
| 5 |
import traceback
|
| 6 |
+
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
|
| 7 |
from github import Github
|
| 8 |
from openai import OpenAI
|
| 9 |
|
| 10 |
+
# Emojis and fun statements for progress updates
|
| 11 |
+
PROGRESS_STEPS = [
|
| 12 |
+
("π΅οΈββοΈ", "Investigating the GitHub realm..."),
|
| 13 |
+
("π§¬", "Decoding repository DNA..."),
|
| 14 |
+
("π", "Hunting for bugs and features..."),
|
| 15 |
+
("π", "Examining pull request tea leaves..."),
|
| 16 |
+
("π§ ", "Activating AI brain cells..."),
|
| 17 |
+
("π", "Crafting the legendary report..."),
|
| 18 |
+
]
|
| 19 |
|
| 20 |
+
def analyze_github_repo(repo_input, github_token=None):
|
| 21 |
+
if github_token:
|
| 22 |
+
os.environ["GITHUB_TOKEN"] = github_token
|
| 23 |
+
|
| 24 |
+
github_token = os.environ.get("GITHUB_TOKEN")
|
| 25 |
+
if not github_token:
|
| 26 |
+
return "β Error: GITHUB_TOKEN environment variable not set.", gr.update(visible=True), gr.update(visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
openrouter_api_key = os.environ.get("OPENROUTER_API_KEY")
|
| 29 |
+
if not openrouter_api_key:
|
| 30 |
+
return "β Error: OPENROUTER_API_KEY environment variable not set.", gr.update(visible=True), gr.update(visible=False)
|
| 31 |
|
| 32 |
+
progress_md = ""
|
| 33 |
+
yield progress_md, gr.update(visible=True), gr.update(visible=False) # Initial empty output
|
|
|
|
| 34 |
|
| 35 |
+
for emoji, message in PROGRESS_STEPS:
|
| 36 |
+
progress_md += f"{emoji} {message} \n"
|
| 37 |
+
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
| 38 |
+
time.sleep(4)
|
| 39 |
|
| 40 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
owner, repo_name = get_repo_info(repo_input)
|
| 42 |
repo_url = f"https://github.com/{owner}/{repo_name}"
|
|
|
|
| 43 |
|
| 44 |
g = Github(github_token)
|
| 45 |
github_repo = g.get_repo(f"{owner}/{repo_name}")
|
|
|
|
| 51 |
|
| 52 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 53 |
repo_path = clone_repo(owner, repo_name, temp_dir)
|
|
|
|
| 54 |
|
| 55 |
+
progress_md += "π¬ Analyzing code structure... \n"
|
| 56 |
+
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
| 57 |
code_analysis = analyze_code(repo_path)
|
| 58 |
code_analysis['llm_analysis'] = llm_analyze_code(client, code_analysis)
|
|
|
|
| 59 |
|
| 60 |
+
progress_md += f"π Analyzing issues (max {max_issues})... \n"
|
| 61 |
+
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
| 62 |
issues_data = analyze_issues(github_repo, max_issues)
|
| 63 |
issues_analysis = llm_analyze_issues(client, issues_data, repo_url)
|
|
|
|
| 64 |
|
| 65 |
+
progress_md += f"π Analyzing pull requests (max {max_prs})... \n"
|
| 66 |
+
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
| 67 |
prs_data = analyze_pull_requests(github_repo, max_prs)
|
| 68 |
pr_analysis = llm_analyze_prs(client, prs_data, repo_url)
|
|
|
|
| 69 |
|
| 70 |
+
progress_md += "π§ Synthesizing findings... \n"
|
| 71 |
+
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
| 72 |
final_analysis = llm_synthesize_findings(
|
| 73 |
client,
|
| 74 |
code_analysis.get('llm_analysis', ''),
|
| 75 |
issues_analysis.get('summary', ''),
|
| 76 |
pr_analysis.get('summary', '')
|
| 77 |
)
|
|
|
|
| 78 |
|
| 79 |
repo_info = {
|
| 80 |
"owner": owner,
|
| 81 |
"repo_name": repo_name,
|
| 82 |
}
|
| 83 |
|
| 84 |
+
progress_md += "π Generating report... \n"
|
| 85 |
+
yield progress_md, gr.update(visible=True), gr.update(visible=False)
|
| 86 |
report = generate_report(repo_info, code_analysis, issues_analysis, pr_analysis, final_analysis)
|
|
|
|
| 87 |
|
| 88 |
+
# Return the final Markdown report
|
| 89 |
+
yield progress_md + "β
Analysis complete!", gr.update(visible=False), gr.update(visible=True, value=report)
|
| 90 |
except Exception as e:
|
| 91 |
error_message = f"β An error occurred: {str(e)}"
|
| 92 |
traceback.print_exc()
|
| 93 |
+
yield progress_md + error_message, gr.update(visible=True), gr.update(visible=False)
|
| 94 |
|
| 95 |
# Define the Gradio interface
|
| 96 |
with gr.Blocks() as app:
|
|
|
|
| 103 |
|
| 104 |
analyze_button = gr.Button("Analyze Repository")
|
| 105 |
|
| 106 |
+
progress_output = gr.Markdown(label="Progress")
|
| 107 |
+
report_output = gr.Markdown(label="Analysis Report", visible=False)
|
| 108 |
|
| 109 |
analyze_button.click(
|
| 110 |
analyze_github_repo,
|
| 111 |
inputs=[repo_input, github_token],
|
| 112 |
+
outputs=[progress_output, progress_output, report_output],
|
| 113 |
)
|
| 114 |
|
| 115 |
# Launch the app
|