codelion commited on
Commit
d3b6911
Β·
verified Β·
1 Parent(s): 1f3d4e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -74
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
- # Mock functions for testing - replace these with your actual functions
10
- def get_repo_info(repo_input):
11
- return repo_input.split('/')[-2:]
 
 
 
 
 
 
12
 
13
- def clone_repo(owner, repo_name, temp_dir):
14
- return os.path.join(temp_dir, f"{owner}_{repo_name}")
15
-
16
- def analyze_code(repo_path):
17
- return {"files": 10, "lines": 1000}
18
-
19
- def analyze_issues(github_repo, max_issues):
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
- ## Pull Requests Analysis
48
- {pr_analysis['summary']}
 
49
 
50
- ## Final Analysis
51
- {final_analysis}
52
- """
53
 
54
- def analyze_github_repo(repo_input, github_token=None):
55
- def update_output(message):
56
- print(message) # Console log
57
- return message # Update Gradio interface
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
- yield update_output("πŸ”¬ Analyzing code structure...")
 
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
- yield update_output(f"πŸ“Š Analyzing issues (max {max_issues})...")
 
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
- yield update_output(f"πŸ”€ Analyzing pull requests (max {max_prs})...")
 
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
- yield update_output("🧠 Synthesizing findings...")
 
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
- yield update_output("πŸ“ Generating report...")
 
119
  report = generate_report(repo_info, code_analysis, issues_analysis, pr_analysis, final_analysis)
120
- yield update_output("βœ… Report generated")
121
 
122
- yield update_output("πŸŽ‰ Analysis complete! Here's the report:\n\n" + report)
 
123
  except Exception as e:
124
  error_message = f"❌ An error occurred: {str(e)}"
125
  traceback.print_exc()
126
- yield update_output(error_message)
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
- output = gr.Markdown(label="Analysis Output")
 
140
 
141
  analyze_button.click(
142
  analyze_github_repo,
143
  inputs=[repo_input, github_token],
144
- outputs=output,
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