Update app.py
Browse files
app.py
CHANGED
@@ -1,97 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import json
|
4 |
-
import re
|
5 |
-
from github import Github, GithubException
|
6 |
-
import google.generativeai as genai
|
7 |
import paramiko
|
8 |
-
import base64
|
9 |
import os
|
|
|
10 |
|
11 |
-
def
|
12 |
-
# Parse the dependency file
|
13 |
-
dependencies = parse_dependency_file(file_content)
|
14 |
-
|
15 |
-
# Fetch license information
|
16 |
-
licenses = fetch_license_info(dependencies)
|
17 |
-
|
18 |
-
# Determine probable packages
|
19 |
-
probable_packages = determine_probable_packages(file_content, dependencies)
|
20 |
-
|
21 |
-
# Enrich information using Gemini
|
22 |
-
enriched_info = enrich_with_gemini(licenses, probable_packages, gemini_api_key)
|
23 |
-
|
24 |
-
return enriched_info
|
25 |
-
|
26 |
-
def parse_dependency_file(file_content):
|
27 |
-
dependencies = []
|
28 |
-
lines = file_content.split('\n')
|
29 |
-
for line in lines:
|
30 |
-
if '=' in line or '@' in line or ':' in line:
|
31 |
-
parts = re.split(r'[=@:]', line)
|
32 |
-
package = parts[0].strip()
|
33 |
-
version = parts[1].strip() if len(parts) > 1 else "latest"
|
34 |
-
dependencies.append((package, version))
|
35 |
-
return dependencies
|
36 |
-
|
37 |
-
def fetch_license_info(dependencies):
|
38 |
-
licenses = []
|
39 |
-
for package, version in dependencies:
|
40 |
-
try:
|
41 |
-
response = requests.get(f"https://pypi.org/pypi/{package}/{version}/json")
|
42 |
-
data = response.json()
|
43 |
-
license = data['info'].get('license', 'Unknown')
|
44 |
-
description = data['info'].get('summary', 'No description available')
|
45 |
-
licenses.append(f"Package: {package}\nVersion: {version}\nLicense: {license}\nDescription: {description}\n")
|
46 |
-
except:
|
47 |
-
licenses.append(f"Package: {package}\nVersion: {version}\nLicense: Unknown\nDescription: Unable to fetch information\n")
|
48 |
-
return "\n".join(licenses)
|
49 |
-
|
50 |
-
def determine_probable_packages(file_content, dependencies):
|
51 |
-
probable_packages = []
|
52 |
-
if "package.json" in file_content.lower():
|
53 |
-
probable_packages.append("npm (Node Package Manager)")
|
54 |
-
elif "gemfile" in file_content.lower():
|
55 |
-
probable_packages.append("Bundler (Ruby)")
|
56 |
-
elif "requirements.txt" in file_content.lower():
|
57 |
-
probable_packages.append("pip (Python Package Installer)")
|
58 |
-
|
59 |
-
# Add more probable packages based on common dependencies
|
60 |
-
common_packages = {
|
61 |
-
"react": "React (JavaScript library)",
|
62 |
-
"django": "Django (Python web framework)",
|
63 |
-
"rails": "Ruby on Rails (Web application framework)",
|
64 |
-
}
|
65 |
-
|
66 |
-
for package, _ in dependencies:
|
67 |
-
if package.lower() in common_packages:
|
68 |
-
probable_packages.append(common_packages[package.lower()])
|
69 |
-
|
70 |
-
return "\n".join(probable_packages)
|
71 |
-
|
72 |
-
def enrich_with_gemini(licenses, probable_packages, api_key):
|
73 |
-
genai.configure(api_key=api_key)
|
74 |
-
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
|
75 |
-
|
76 |
-
prompt = f"""
|
77 |
-
Analyze the following open-source license information and probable packages:
|
78 |
-
|
79 |
-
License Information:
|
80 |
-
{licenses}
|
81 |
-
|
82 |
-
Probable Packages:
|
83 |
-
{probable_packages}
|
84 |
-
|
85 |
-
Please provide a summary of the project based on these dependencies, including:
|
86 |
-
1. The likely type of project (e.g., web application, data science, etc.)
|
87 |
-
2. Any potential license conflicts or considerations
|
88 |
-
3. Suggestions for best practices in open-source license management for this project
|
89 |
-
"""
|
90 |
-
|
91 |
-
response = model.generate_content(prompt)
|
92 |
-
return response.text
|
93 |
-
|
94 |
-
def fetch_github_info(github_url, ssh_private_key):
|
95 |
try:
|
96 |
# Parse the GitHub URL
|
97 |
parts = github_url.split('/')
|
@@ -126,6 +40,26 @@ def fetch_github_info(github_url, ssh_private_key):
|
|
126 |
except Exception as e:
|
127 |
return f"Error accessing GitHub: {str(e)}"
|
128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
def process_input(file, github_url, ssh_private_key, gemini_api_key):
|
130 |
if file is not None and github_url:
|
131 |
return "Error: Please either upload a file OR provide a GitHub URL, not both."
|
@@ -137,14 +71,16 @@ def process_input(file, github_url, ssh_private_key, gemini_api_key):
|
|
137 |
return "Error: Invalid GitHub URL. Please use the format: https://github.com/username/repository/blob/branch/path/to/file"
|
138 |
if not ssh_private_key.strip():
|
139 |
return "Error: SSH Private Key is empty. Please provide a valid key."
|
140 |
-
file_content =
|
141 |
if file_content.startswith("Error:"):
|
142 |
return file_content
|
143 |
else:
|
144 |
return "Error: Please either upload a file OR provide both GitHub URL and SSH Private Key."
|
145 |
|
146 |
try:
|
147 |
-
|
|
|
|
|
148 |
except Exception as e:
|
149 |
return f"Error processing the file: {str(e)}"
|
150 |
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import json
|
|
|
|
|
|
|
4 |
import paramiko
|
|
|
5 |
import os
|
6 |
+
import google.generativeai as genai
|
7 |
|
8 |
+
def fetch_github_file(github_url, ssh_private_key):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
try:
|
10 |
# Parse the GitHub URL
|
11 |
parts = github_url.split('/')
|
|
|
40 |
except Exception as e:
|
41 |
return f"Error accessing GitHub: {str(e)}"
|
42 |
|
43 |
+
def process_with_gemini(file_content, gemini_api_key):
|
44 |
+
genai.configure(api_key=gemini_api_key)
|
45 |
+
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
|
46 |
+
|
47 |
+
prompt = f"""
|
48 |
+
Analyze the following file content for open-source license information:
|
49 |
+
|
50 |
+
{file_content}
|
51 |
+
|
52 |
+
Please provide:
|
53 |
+
1. A list of dependencies and their versions
|
54 |
+
2. The licenses associated with each dependency (if available)
|
55 |
+
3. A summary of the project based on these dependencies
|
56 |
+
4. Any potential license conflicts or considerations
|
57 |
+
5. Suggestions for best practices in open-source license management for this project
|
58 |
+
"""
|
59 |
+
|
60 |
+
response = model.generate_content(prompt)
|
61 |
+
return response.text
|
62 |
+
|
63 |
def process_input(file, github_url, ssh_private_key, gemini_api_key):
|
64 |
if file is not None and github_url:
|
65 |
return "Error: Please either upload a file OR provide a GitHub URL, not both."
|
|
|
71 |
return "Error: Invalid GitHub URL. Please use the format: https://github.com/username/repository/blob/branch/path/to/file"
|
72 |
if not ssh_private_key.strip():
|
73 |
return "Error: SSH Private Key is empty. Please provide a valid key."
|
74 |
+
file_content = fetch_github_file(github_url, ssh_private_key)
|
75 |
if file_content.startswith("Error:"):
|
76 |
return file_content
|
77 |
else:
|
78 |
return "Error: Please either upload a file OR provide both GitHub URL and SSH Private Key."
|
79 |
|
80 |
try:
|
81 |
+
# Process the file content with Gemini
|
82 |
+
analysis = process_with_gemini(file_content, gemini_api_key)
|
83 |
+
return analysis
|
84 |
except Exception as e:
|
85 |
return f"Error processing the file: {str(e)}"
|
86 |
|