bluenevus's picture
Update app.py
dc02eaf verified
raw
history blame
5.17 kB
import gradio as gr
import google.generativeai as genai
import paramiko
import io
import re
def parse_github_url(github_url):
if github_url.startswith("[email protected]:"):
# SSH format
parts = github_url.split(':')[1].split('/')
owner = parts[0]
repo = parts[1].split('.git')[0]
return owner, repo, None, None
elif github_url.startswith("https://github.com/"):
# HTTPS format
parts = github_url.split('/')
owner = parts[3]
repo = parts[4]
branch = parts[6] if len(parts) > 6 else None
file_path = '/'.join(parts[7:]) if len(parts) > 7 else None
return owner, repo, branch, file_path
else:
raise ValueError("Invalid GitHub URL format")
def fetch_github_file(github_url, ssh_private_key):
try:
owner, repo, branch, file_path = parse_github_url(github_url)
# Set up SSH client
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Load the private key
try:
private_key = paramiko.RSAKey.from_private_key(io.StringIO(ssh_private_key.strip()))
except paramiko.ssh_exception.SSHException as e:
return f"Error: Invalid SSH key - {str(e)}"
# Connect to GitHub using SSH
ssh.connect('github.com', username='git', pkey=private_key)
# Construct the git command to fetch the file content
if branch and file_path:
git_command = f"git archive [email protected]:{owner}/{repo}.git {branch} {file_path} | tar -xO"
else:
git_command = f"git ls-tree -r --name-only HEAD | grep -E '(requirements\.txt|package\.json|Gemfile)'"
# Execute the command
stdin, stdout, stderr = ssh.exec_command(git_command)
# Read the file content
file_content = stdout.read().decode('utf-8')
# Close the SSH connection
ssh.close()
if not file_content:
return {"error": "Error: File not found or empty."}
return {file_path if file_path else "repository_files": file_content}
except paramiko.AuthenticationException:
return {"error": "Error: Authentication failed. Please check your SSH key."}
except paramiko.SSHException as ssh_err:
return {"error": f"SSH Error: {str(ssh_err)}"}
except Exception as e:
return {"error": f"Error accessing GitHub: {str(e)}"}
def process_with_gemini(file_content, gemini_api_key):
genai.configure(api_key=gemini_api_key)
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
combined_content = "\n\n".join([f"File: {name}\n{content}" for name, content in file_content.items()])
prompt = f"""
Analyze the following file content for open-source license information:
{combined_content}
Please provide:
1. A list of dependencies and their versions
2. The licenses associated with each dependency (if available)
3. A summary of the project based on these dependencies
4. Any potential license conflicts or considerations
5. Suggestions for best practices in open-source license management for this project
"""
response = model.generate_content(prompt)
return response.text
def process_input(file, github_url, ssh_private_key, gemini_api_key):
if file is not None and github_url:
return "Error: Please either upload a file OR provide a GitHub URL, not both."
if file is not None:
file_content = {file.name: file.read().decode('utf-8')}
elif github_url and ssh_private_key:
if not ssh_private_key.strip():
return "Error: SSH Private Key is empty. Please provide a valid key."
if not ssh_private_key.strip().startswith("-----BEGIN"):
return "Error: Invalid SSH key format. Please ensure you've copied the entire key, including the BEGIN and END lines."
try:
file_content = fetch_github_file(github_url, ssh_private_key)
if "error" in file_content:
return file_content["error"]
except ValueError as e:
return str(e)
else:
return "Error: Please either upload a file OR provide both GitHub URL and SSH Private Key."
try:
# Process the file content with Gemini
analysis = process_with_gemini(file_content, gemini_api_key)
return analysis
except Exception as e:
return f"Error processing the file: {str(e)}"
iface = gr.Interface(
fn=process_input,
inputs=[
gr.File(label="Upload dependency file (e.g., requirements.txt, package.json, Gemfile)"),
gr.Textbox(label="GitHub Repository URL (optional, SSH or HTTPS format)"),
gr.TextArea(label="SSH Private Key (required if using GitHub URL)", lines=10),
gr.Textbox(label="Gemini API Key", type="password"),
],
outputs=gr.Textbox(label="License Information and Analysis"),
title="Open Source License Extractor",
description="Upload a dependency file or provide a GitHub repository URL to analyze open-source licenses.",
)
if __name__ == "__main__":
iface.launch()