Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import tempfile
|
|
@@ -6,15 +5,52 @@ import shutil
|
|
| 6 |
from pathlib import Path
|
| 7 |
from typing import Dict, Any, Tuple, List, Generator
|
| 8 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Import all the analyzer modules
|
| 11 |
-
from analyze_repository_structure import analyze_repository_structure
|
| 12 |
-
from analyze_code_style import analyze_code_style
|
| 13 |
-
from analyze_temportal_patterns import analyze_temporal_patterns
|
| 14 |
-
from analyze_project_preferences import analyze_project_preferences
|
| 15 |
-
from calculate_identity_confidence import calculate_identity_confidence
|
| 16 |
-
from repository_selector import RepositorySelector
|
| 17 |
-
from prompt_analyzer import create_handler
|
| 18 |
|
| 19 |
# Load environment variables
|
| 20 |
load_dotenv()
|
|
@@ -23,18 +59,19 @@ class StyleAnalyzerApp:
|
|
| 23 |
def __init__(self):
|
| 24 |
self.temp_dir = tempfile.mkdtemp()
|
| 25 |
self.handler = create_handler()
|
| 26 |
-
|
|
|
|
| 27 |
def cleanup(self):
|
| 28 |
if os.path.exists(self.temp_dir):
|
| 29 |
shutil.rmtree(self.temp_dir)
|
| 30 |
|
| 31 |
-
def process_analysis(self, user_path: Path, sources_to_analyze: List[str]) ->
|
| 32 |
"""Perform the main analysis steps"""
|
| 33 |
sources_data = analyze_repository_structure(sources_to_analyze, user_path)
|
| 34 |
code_style = analyze_code_style(sources_data)
|
| 35 |
temporal_patterns = analyze_temporal_patterns(sources_data, self.report_data)
|
| 36 |
project_preferences = analyze_project_preferences(sources_data)
|
| 37 |
-
|
| 38 |
identity_confidence = calculate_identity_confidence(
|
| 39 |
sources_data,
|
| 40 |
code_style,
|
|
@@ -49,16 +86,20 @@ class StyleAnalyzerApp:
|
|
| 49 |
"identity_confidence": identity_confidence,
|
| 50 |
}
|
| 51 |
|
| 52 |
-
def process_files(self,
|
| 53 |
files: List[gr.File],
|
| 54 |
username: str,
|
| 55 |
github_token: str = None,
|
| 56 |
gemini_key: str = None) -> Tuple[str, Dict]:
|
| 57 |
"""Process uploaded files and run analysis"""
|
| 58 |
try:
|
| 59 |
-
# Set environment variables if provided
|
| 60 |
gemini_key="AIzaSyBHtQa-YQwhlDGihbyUUlS2MiclnXxiN8E"
|
| 61 |
github_token="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"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
# Create user directory
|
| 64 |
user_path = Path(self.temp_dir) / username
|
|
@@ -67,6 +108,8 @@ class StyleAnalyzerApp:
|
|
| 67 |
# Process uploaded files
|
| 68 |
report_data = None
|
| 69 |
for file in files:
|
|
|
|
|
|
|
| 70 |
file_path = file.name
|
| 71 |
if file_path.endswith('report.json'):
|
| 72 |
with open(file_path, 'r') as f:
|
|
@@ -79,10 +122,10 @@ class StyleAnalyzerApp:
|
|
| 79 |
if not report_data:
|
| 80 |
return "Error: No report.json file found in uploads", None
|
| 81 |
|
| 82 |
-
self.report_data = report_data
|
| 83 |
repo_selector = RepositorySelector(self.temp_dir, username)
|
| 84 |
sources_to_analyze = repo_selector.select_repositories(report_data)
|
| 85 |
-
|
| 86 |
analysis_results = self.process_analysis(user_path, sources_to_analyze)
|
| 87 |
return "Analysis completed successfully!", analysis_results
|
| 88 |
|
|
@@ -103,11 +146,11 @@ class StyleAnalyzerApp:
|
|
| 103 |
}
|
| 104 |
.analyze-btn:hover { background-color: #1976D2; }
|
| 105 |
"""
|
| 106 |
-
|
| 107 |
with gr.Blocks(css=css, theme=gr.themes.Base()) as interface:
|
| 108 |
gr.Markdown("""
|
| 109 |
# GitHub Stylometry Analyzer
|
| 110 |
-
|
| 111 |
Analyze coding patterns and style from GitHub repositories.
|
| 112 |
""")
|
| 113 |
|
|
@@ -118,10 +161,10 @@ class StyleAnalyzerApp:
|
|
| 118 |
label="GitHub Username",
|
| 119 |
placeholder="Enter your GitHub username"
|
| 120 |
)
|
| 121 |
-
|
| 122 |
with gr.Accordion("API Configuration", open=False):
|
| 123 |
github_token = gr.Textbox(
|
| 124 |
-
label="GitHub Token",
|
| 125 |
placeholder="Your GitHub API token",
|
| 126 |
type="password"
|
| 127 |
)
|
|
@@ -130,13 +173,13 @@ class StyleAnalyzerApp:
|
|
| 130 |
placeholder="Your Gemini API key",
|
| 131 |
type="password"
|
| 132 |
)
|
| 133 |
-
|
| 134 |
files = gr.File(
|
| 135 |
label="Upload Repository Files",
|
| 136 |
file_count="multiple",
|
| 137 |
file_types=[".json", ".git", ".zip"]
|
| 138 |
)
|
| 139 |
-
|
| 140 |
analyze_btn = gr.Button(
|
| 141 |
"Start Analysis",
|
| 142 |
elem_classes=["analyze-btn"]
|
|
@@ -149,7 +192,7 @@ class StyleAnalyzerApp:
|
|
| 149 |
)
|
| 150 |
results = gr.JSON(
|
| 151 |
label="Analysis Results",
|
| 152 |
-
interactive=False
|
| 153 |
)
|
| 154 |
|
| 155 |
analyze_btn.click(
|
|
@@ -163,12 +206,12 @@ class StyleAnalyzerApp:
|
|
| 163 |
def main():
|
| 164 |
app = StyleAnalyzerApp()
|
| 165 |
interface = app.create_interface()
|
| 166 |
-
|
| 167 |
try:
|
| 168 |
interface.queue().launch(
|
| 169 |
server_name="0.0.0.0",
|
| 170 |
server_port=7860,
|
| 171 |
-
share=True,
|
| 172 |
show_error=True
|
| 173 |
)
|
| 174 |
finally:
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import tempfile
|
|
|
|
| 5 |
from pathlib import Path
|
| 6 |
from typing import Dict, Any, Tuple, List, Generator
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
+
import gradio as gr # Import Gradio at the top level
|
| 9 |
+
|
| 10 |
+
# Import all the analyzer modules (Placeholder implementations for now)
|
| 11 |
+
# These would be your actual analysis modules. I'm providing
|
| 12 |
+
# simple stubs so the code runs without errors.
|
| 13 |
+
def analyze_repository_structure(sources_to_analyze, user_path):
|
| 14 |
+
"""Placeholder for repository structure analysis."""
|
| 15 |
+
print(f"Analyzing repository structure: {sources_to_analyze} in {user_path}")
|
| 16 |
+
return {"repo_structure": "example_structure"}
|
| 17 |
+
|
| 18 |
+
def analyze_code_style(sources_data):
|
| 19 |
+
"""Placeholder for code style analysis."""
|
| 20 |
+
print(f"Analyzing code style: {sources_data}")
|
| 21 |
+
return {"code_style": "example_style"}
|
| 22 |
+
|
| 23 |
+
def analyze_temporal_patterns(sources_data, report_data):
|
| 24 |
+
"""Placeholder for temporal pattern analysis."""
|
| 25 |
+
print(f"Analyzing temporal patterns: {sources_data}, report: {report_data}")
|
| 26 |
+
return {"temporal_patterns": "example_patterns"}
|
| 27 |
+
|
| 28 |
+
def analyze_project_preferences(sources_data):
|
| 29 |
+
"""Placeholder for project preferences analysis."""
|
| 30 |
+
print(f"Analyzing project preferences: {sources_data}")
|
| 31 |
+
return {"project_preferences": "example_preferences"}
|
| 32 |
+
|
| 33 |
+
def calculate_identity_confidence(sources_data, code_style, project_preferences, temporal_patterns):
|
| 34 |
+
"""Placeholder for identity confidence calculation."""
|
| 35 |
+
print(f"Calculating identity confidence: {sources_data}, {code_style}, {project_preferences}, {temporal_patterns}")
|
| 36 |
+
return 0.95 # Example confidence score
|
| 37 |
+
|
| 38 |
+
class RepositorySelector:
|
| 39 |
+
"""Placeholder RepositorySelector"""
|
| 40 |
+
def __init__(self, temp_dir, username):
|
| 41 |
+
self.temp_dir = temp_dir
|
| 42 |
+
self.username = username
|
| 43 |
+
|
| 44 |
+
def select_repositories(self, report_data):
|
| 45 |
+
"""Placeholder for repository selection"""
|
| 46 |
+
print("Selecting repositories...")
|
| 47 |
+
return ["repo1", "repo2"] # Return a list of repo names (strings)
|
| 48 |
+
|
| 49 |
+
def create_handler(): # Placeholder for prompt_analyzer.create_handler
|
| 50 |
+
"""Placeholder create_handler function"""
|
| 51 |
+
print("Creating handler...")
|
| 52 |
+
return None # Replace with your actual handler
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# Load environment variables
|
| 56 |
load_dotenv()
|
|
|
|
| 59 |
def __init__(self):
|
| 60 |
self.temp_dir = tempfile.mkdtemp()
|
| 61 |
self.handler = create_handler()
|
| 62 |
+
self.report_data = None # Initialize report_data
|
| 63 |
+
|
| 64 |
def cleanup(self):
|
| 65 |
if os.path.exists(self.temp_dir):
|
| 66 |
shutil.rmtree(self.temp_dir)
|
| 67 |
|
| 68 |
+
def process_analysis(self, user_path: Path, sources_to_analyze: List[str]) -> Dict:
|
| 69 |
"""Perform the main analysis steps"""
|
| 70 |
sources_data = analyze_repository_structure(sources_to_analyze, user_path)
|
| 71 |
code_style = analyze_code_style(sources_data)
|
| 72 |
temporal_patterns = analyze_temporal_patterns(sources_data, self.report_data)
|
| 73 |
project_preferences = analyze_project_preferences(sources_data)
|
| 74 |
+
|
| 75 |
identity_confidence = calculate_identity_confidence(
|
| 76 |
sources_data,
|
| 77 |
code_style,
|
|
|
|
| 86 |
"identity_confidence": identity_confidence,
|
| 87 |
}
|
| 88 |
|
| 89 |
+
def process_files(self,
|
| 90 |
files: List[gr.File],
|
| 91 |
username: str,
|
| 92 |
github_token: str = None,
|
| 93 |
gemini_key: str = None) -> Tuple[str, Dict]:
|
| 94 |
"""Process uploaded files and run analysis"""
|
| 95 |
try:
|
| 96 |
+
# Set environment variables if provided (Best practice: Use os.environ.get)
|
| 97 |
gemini_key="AIzaSyBHtQa-YQwhlDGihbyUUlS2MiclnXxiN8E"
|
| 98 |
github_token="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"
|
| 99 |
+
# It's generally safer *not* to hardcode API keys, but if you must, provide defaults:
|
| 100 |
+
# os.environ["GEMINI_API_KEY"] = gemini_key or "AIzaSy..."
|
| 101 |
+
# os.environ["GITHUB_TOKEN"] = github_token or "ghp_..."
|
| 102 |
+
|
| 103 |
|
| 104 |
# Create user directory
|
| 105 |
user_path = Path(self.temp_dir) / username
|
|
|
|
| 108 |
# Process uploaded files
|
| 109 |
report_data = None
|
| 110 |
for file in files:
|
| 111 |
+
if file is None: # important to check file is None
|
| 112 |
+
continue
|
| 113 |
file_path = file.name
|
| 114 |
if file_path.endswith('report.json'):
|
| 115 |
with open(file_path, 'r') as f:
|
|
|
|
| 122 |
if not report_data:
|
| 123 |
return "Error: No report.json file found in uploads", None
|
| 124 |
|
| 125 |
+
self.report_data = report_data # Store report_data
|
| 126 |
repo_selector = RepositorySelector(self.temp_dir, username)
|
| 127 |
sources_to_analyze = repo_selector.select_repositories(report_data)
|
| 128 |
+
|
| 129 |
analysis_results = self.process_analysis(user_path, sources_to_analyze)
|
| 130 |
return "Analysis completed successfully!", analysis_results
|
| 131 |
|
|
|
|
| 146 |
}
|
| 147 |
.analyze-btn:hover { background-color: #1976D2; }
|
| 148 |
"""
|
| 149 |
+
|
| 150 |
with gr.Blocks(css=css, theme=gr.themes.Base()) as interface:
|
| 151 |
gr.Markdown("""
|
| 152 |
# GitHub Stylometry Analyzer
|
| 153 |
+
|
| 154 |
Analyze coding patterns and style from GitHub repositories.
|
| 155 |
""")
|
| 156 |
|
|
|
|
| 161 |
label="GitHub Username",
|
| 162 |
placeholder="Enter your GitHub username"
|
| 163 |
)
|
| 164 |
+
|
| 165 |
with gr.Accordion("API Configuration", open=False):
|
| 166 |
github_token = gr.Textbox(
|
| 167 |
+
label="GitHub Token",
|
| 168 |
placeholder="Your GitHub API token",
|
| 169 |
type="password"
|
| 170 |
)
|
|
|
|
| 173 |
placeholder="Your Gemini API key",
|
| 174 |
type="password"
|
| 175 |
)
|
| 176 |
+
|
| 177 |
files = gr.File(
|
| 178 |
label="Upload Repository Files",
|
| 179 |
file_count="multiple",
|
| 180 |
file_types=[".json", ".git", ".zip"]
|
| 181 |
)
|
| 182 |
+
|
| 183 |
analyze_btn = gr.Button(
|
| 184 |
"Start Analysis",
|
| 185 |
elem_classes=["analyze-btn"]
|
|
|
|
| 192 |
)
|
| 193 |
results = gr.JSON(
|
| 194 |
label="Analysis Results",
|
| 195 |
+
# interactive=False <- Remove this line
|
| 196 |
)
|
| 197 |
|
| 198 |
analyze_btn.click(
|
|
|
|
| 206 |
def main():
|
| 207 |
app = StyleAnalyzerApp()
|
| 208 |
interface = app.create_interface()
|
| 209 |
+
|
| 210 |
try:
|
| 211 |
interface.queue().launch(
|
| 212 |
server_name="0.0.0.0",
|
| 213 |
server_port=7860,
|
| 214 |
+
share=True, # Set share=True to create a public link
|
| 215 |
show_error=True
|
| 216 |
)
|
| 217 |
finally:
|