bluenevus's picture
Update app.py
d599929 verified
raw
history blame
3.96 kB
import gradio as gr
import google.generativeai as genai
from github import Github, GithubException
def fetch_github_file(github_url, ssh_private_key):
try:
# Parse the GitHub URL
parts = github_url.split('/')
owner = parts[3]
repo = parts[4]
branch = parts[6]
file_path = '/'.join(parts[7:])
# Create a Github instance using SSH key or Personal Access Token
g = Github(login_or_token=ssh_private_key)
# Get the repository
repository = g.get_repo(f"{owner}/{repo}")
try:
# Get the file content
file_content = repository.get_contents(file_path, ref=branch).decoded_content.decode('utf-8')
except GithubException as e:
if e.status == 404:
return f"Error: File not found. Please check the path: {file_path}"
else:
raise
if not file_content:
return "Error: File is empty."
return file_content
except GithubException as e:
if e.status == 401:
return "Error: Authentication failed. Please check your SSH key or token."
elif e.status == 404:
return f"Error: Repository not found: {owner}/{repo}"
else:
return f"GitHub Error: {str(e)}"
except Exception as e:
return 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')
prompt = f"""
Analyze the following file content for open-source license information:
{file_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.decode('utf-8')
elif github_url and ssh_private_key:
if not github_url.startswith("https://github.com/"):
return "Error: Invalid GitHub URL. Please use the format: https://github.com/username/repository/blob/branch/path/to/file"
if not ssh_private_key.strip():
return "Error: SSH Private Key or Personal Access Token is empty. Please provide a valid key or token."
file_content = fetch_github_file(github_url, ssh_private_key)
if file_content.startswith("Error:"):
return file_content
else:
return "Error: Please either upload a file OR provide both GitHub URL and SSH Private Key or Personal Access Token."
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 File URL (optional)"),
gr.Textbox(label="SSH Private Key or Personal Access Token (required if using GitHub URL)", type="password"),
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 file URL to extract and analyze open-source license information.",
)
if __name__ == "__main__":
iface.launch()