Update app.py
Browse files
app.py
CHANGED
|
@@ -2,15 +2,105 @@ 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 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def fetch_github_info(github_url, github_token):
|
| 11 |
try:
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
repo_name = github_url.split('github.com/')[-1]
|
|
|
|
|
|
|
|
|
|
| 14 |
repo = g.get_repo(repo_name)
|
| 15 |
|
| 16 |
file_content = ""
|
|
@@ -18,8 +108,10 @@ def fetch_github_info(github_url, github_token):
|
|
| 18 |
try:
|
| 19 |
contents = repo.get_contents(file_name)
|
| 20 |
file_content = contents.decoded_content.decode('utf-8')
|
|
|
|
| 21 |
break
|
| 22 |
-
except GithubException:
|
|
|
|
| 23 |
continue
|
| 24 |
|
| 25 |
if not file_content:
|
|
@@ -28,9 +120,11 @@ def fetch_github_info(github_url, github_token):
|
|
| 28 |
return file_content
|
| 29 |
except GithubException as e:
|
| 30 |
if e.status == 404:
|
| 31 |
-
return f"Error: Repository not found. Please check the URL and ensure you have the correct access permissions."
|
| 32 |
else:
|
| 33 |
-
return f"Error: {str(e)}"
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def process_input(file, github_url, github_token, gemini_api_key):
|
| 36 |
if file is not None and github_url:
|
|
@@ -39,13 +133,20 @@ def process_input(file, github_url, github_token, gemini_api_key):
|
|
| 39 |
if file is not None:
|
| 40 |
file_content = file.decode('utf-8')
|
| 41 |
elif github_url and github_token:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
file_content = fetch_github_info(github_url, github_token)
|
| 43 |
if file_content.startswith("Error:"):
|
| 44 |
return file_content
|
| 45 |
else:
|
| 46 |
return "Error: Please either upload a file OR provide both GitHub URL and access token."
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
iface = gr.Interface(
|
| 51 |
fn=process_input,
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import re
|
| 5 |
+
from github import Github, GithubException, Auth
|
| 6 |
import google.generativeai as genai
|
| 7 |
|
| 8 |
+
def extract_licenses(file_content, github_url, github_token, gemini_api_key):
|
| 9 |
+
# Parse the dependency file
|
| 10 |
+
dependencies = parse_dependency_file(file_content)
|
| 11 |
+
|
| 12 |
+
# Fetch license information
|
| 13 |
+
licenses = fetch_license_info(dependencies)
|
| 14 |
+
|
| 15 |
+
# Determine probable packages
|
| 16 |
+
probable_packages = determine_probable_packages(file_content, dependencies)
|
| 17 |
+
|
| 18 |
+
# Enrich information using Gemini
|
| 19 |
+
enriched_info = enrich_with_gemini(licenses, probable_packages, gemini_api_key)
|
| 20 |
+
|
| 21 |
+
return enriched_info
|
| 22 |
+
|
| 23 |
+
def parse_dependency_file(file_content):
|
| 24 |
+
dependencies = []
|
| 25 |
+
lines = file_content.split('\n')
|
| 26 |
+
for line in lines:
|
| 27 |
+
if '=' in line or '@' in line or ':' in line:
|
| 28 |
+
parts = re.split(r'[=@:]', line)
|
| 29 |
+
package = parts[0].strip()
|
| 30 |
+
version = parts[1].strip() if len(parts) > 1 else "latest"
|
| 31 |
+
dependencies.append((package, version))
|
| 32 |
+
return dependencies
|
| 33 |
+
|
| 34 |
+
def fetch_license_info(dependencies):
|
| 35 |
+
licenses = []
|
| 36 |
+
for package, version in dependencies:
|
| 37 |
+
try:
|
| 38 |
+
response = requests.get(f"https://pypi.org/pypi/{package}/{version}/json")
|
| 39 |
+
data = response.json()
|
| 40 |
+
license = data['info'].get('license', 'Unknown')
|
| 41 |
+
description = data['info'].get('summary', 'No description available')
|
| 42 |
+
licenses.append(f"Package: {package}\nVersion: {version}\nLicense: {license}\nDescription: {description}\n")
|
| 43 |
+
except:
|
| 44 |
+
licenses.append(f"Package: {package}\nVersion: {version}\nLicense: Unknown\nDescription: Unable to fetch information\n")
|
| 45 |
+
return "\n".join(licenses)
|
| 46 |
+
|
| 47 |
+
def determine_probable_packages(file_content, dependencies):
|
| 48 |
+
probable_packages = []
|
| 49 |
+
if "package.json" in file_content.lower():
|
| 50 |
+
probable_packages.append("npm (Node Package Manager)")
|
| 51 |
+
elif "gemfile" in file_content.lower():
|
| 52 |
+
probable_packages.append("Bundler (Ruby)")
|
| 53 |
+
elif "requirements.txt" in file_content.lower():
|
| 54 |
+
probable_packages.append("pip (Python Package Installer)")
|
| 55 |
+
|
| 56 |
+
# Add more probable packages based on common dependencies
|
| 57 |
+
common_packages = {
|
| 58 |
+
"react": "React (JavaScript library)",
|
| 59 |
+
"django": "Django (Python web framework)",
|
| 60 |
+
"rails": "Ruby on Rails (Web application framework)",
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
for package, _ in dependencies:
|
| 64 |
+
if package.lower() in common_packages:
|
| 65 |
+
probable_packages.append(common_packages[package.lower()])
|
| 66 |
+
|
| 67 |
+
return "\n".join(probable_packages)
|
| 68 |
+
|
| 69 |
+
def enrich_with_gemini(licenses, probable_packages, api_key):
|
| 70 |
+
genai.configure(api_key=api_key)
|
| 71 |
+
model = genai.GenerativeModel('gemini-pro')
|
| 72 |
+
|
| 73 |
+
prompt = f"""
|
| 74 |
+
Analyze the following open-source license information and probable packages:
|
| 75 |
+
|
| 76 |
+
License Information:
|
| 77 |
+
{licenses}
|
| 78 |
+
|
| 79 |
+
Probable Packages:
|
| 80 |
+
{probable_packages}
|
| 81 |
+
|
| 82 |
+
Please provide a summary of the project based on these dependencies, including:
|
| 83 |
+
1. The likely type of project (e.g., web application, data science, etc.)
|
| 84 |
+
2. Any potential license conflicts or considerations
|
| 85 |
+
3. Suggestions for best practices in open-source license management for this project
|
| 86 |
+
"""
|
| 87 |
+
|
| 88 |
+
response = model.generate_content(prompt)
|
| 89 |
+
return response.text
|
| 90 |
|
| 91 |
def fetch_github_info(github_url, github_token):
|
| 92 |
try:
|
| 93 |
+
# Create an authentication object
|
| 94 |
+
auth = Auth.Token(github_token)
|
| 95 |
+
|
| 96 |
+
# Create a Github instance with authentication
|
| 97 |
+
g = Github(auth=auth)
|
| 98 |
+
|
| 99 |
+
# Extract the repository name from the URL
|
| 100 |
repo_name = github_url.split('github.com/')[-1]
|
| 101 |
+
print(f"Attempting to access repository: {repo_name}")
|
| 102 |
+
|
| 103 |
+
# Get the repository
|
| 104 |
repo = g.get_repo(repo_name)
|
| 105 |
|
| 106 |
file_content = ""
|
|
|
|
| 108 |
try:
|
| 109 |
contents = repo.get_contents(file_name)
|
| 110 |
file_content = contents.decoded_content.decode('utf-8')
|
| 111 |
+
print(f"Successfully retrieved {file_name}")
|
| 112 |
break
|
| 113 |
+
except GithubException as e:
|
| 114 |
+
print(f"Failed to retrieve {file_name}: {str(e)}")
|
| 115 |
continue
|
| 116 |
|
| 117 |
if not file_content:
|
|
|
|
| 120 |
return file_content
|
| 121 |
except GithubException as e:
|
| 122 |
if e.status == 404:
|
| 123 |
+
return f"Error: Repository not found or access denied. Please check the URL and ensure you have the correct access permissions. Details: {str(e)}"
|
| 124 |
else:
|
| 125 |
+
return f"Error accessing GitHub: {str(e)}"
|
| 126 |
+
except Exception as e:
|
| 127 |
+
return f"Unexpected error: {str(e)}"
|
| 128 |
|
| 129 |
def process_input(file, github_url, github_token, gemini_api_key):
|
| 130 |
if file is not None and github_url:
|
|
|
|
| 133 |
if file is not None:
|
| 134 |
file_content = file.decode('utf-8')
|
| 135 |
elif github_url and github_token:
|
| 136 |
+
if not github_url.startswith("https://github.com/"):
|
| 137 |
+
return "Error: Invalid GitHub URL. Please use the format: https://github.com/username/repository"
|
| 138 |
+
if not github_token.strip():
|
| 139 |
+
return "Error: GitHub Personal Access Token is empty. Please provide a valid token."
|
| 140 |
file_content = fetch_github_info(github_url, github_token)
|
| 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 access token."
|
| 145 |
|
| 146 |
+
try:
|
| 147 |
+
return extract_licenses(file_content, github_url, github_token, gemini_api_key)
|
| 148 |
+
except Exception as e:
|
| 149 |
+
return f"Error processing the file: {str(e)}"
|
| 150 |
|
| 151 |
iface = gr.Interface(
|
| 152 |
fn=process_input,
|