Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,13 @@ 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 |
-
def extract_licenses(file_content, github_url,
|
| 9 |
# Parse the dependency file
|
| 10 |
dependencies = parse_dependency_file(file_content)
|
| 11 |
|
|
@@ -88,57 +91,60 @@ def enrich_with_gemini(licenses, probable_packages, api_key):
|
|
| 88 |
response = model.generate_content(prompt)
|
| 89 |
return response.text
|
| 90 |
|
| 91 |
-
def fetch_github_info(github_url,
|
| 92 |
try:
|
| 93 |
-
#
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
return file_content
|
| 115 |
-
except GithubException as e:
|
| 116 |
-
if e.status == 404:
|
| 117 |
-
return f"Error: File or repository not found. Please check the URL and ensure you have the correct access permissions. Details: {str(e)}"
|
| 118 |
-
else:
|
| 119 |
-
return f"Error accessing GitHub: {str(e)}"
|
| 120 |
except Exception as e:
|
| 121 |
-
return f"
|
| 122 |
|
| 123 |
-
def process_input(file, github_url,
|
| 124 |
if file is not None and github_url:
|
| 125 |
return "Error: Please either upload a file OR provide a GitHub URL, not both."
|
| 126 |
|
| 127 |
if file is not None:
|
| 128 |
file_content = file.decode('utf-8')
|
| 129 |
-
elif github_url and
|
| 130 |
if not github_url.startswith("https://github.com/"):
|
| 131 |
return "Error: Invalid GitHub URL. Please use the format: https://github.com/username/repository/blob/branch/path/to/file"
|
| 132 |
-
if not
|
| 133 |
-
return "Error:
|
| 134 |
-
file_content = fetch_github_info(github_url,
|
| 135 |
if file_content.startswith("Error:"):
|
| 136 |
return file_content
|
| 137 |
else:
|
| 138 |
-
return "Error: Please either upload a file OR provide both GitHub URL and
|
| 139 |
|
| 140 |
try:
|
| 141 |
-
return extract_licenses(file_content, github_url,
|
| 142 |
except Exception as e:
|
| 143 |
return f"Error processing the file: {str(e)}"
|
| 144 |
|
|
@@ -147,7 +153,7 @@ iface = gr.Interface(
|
|
| 147 |
inputs=[
|
| 148 |
gr.File(label="Upload dependency file (e.g., requirements.txt, package.json, Gemfile)"),
|
| 149 |
gr.Textbox(label="GitHub File URL (optional)"),
|
| 150 |
-
gr.Textbox(label="
|
| 151 |
gr.Textbox(label="Gemini API Key", type="password"),
|
| 152 |
],
|
| 153 |
outputs=gr.Textbox(label="License Information and Analysis"),
|
|
|
|
| 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 extract_licenses(file_content, github_url, ssh_private_key, gemini_api_key):
|
| 12 |
# Parse the dependency file
|
| 13 |
dependencies = parse_dependency_file(file_content)
|
| 14 |
|
|
|
|
| 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('/')
|
| 98 |
+
owner = parts[3]
|
| 99 |
+
repo = parts[4]
|
| 100 |
+
branch = parts[6]
|
| 101 |
+
file_path = '/'.join(parts[7:])
|
| 102 |
+
|
| 103 |
+
# Set up SSH client
|
| 104 |
+
ssh = paramiko.SSHClient()
|
| 105 |
+
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
| 106 |
+
|
| 107 |
+
# Create a temporary file to store the SSH private key
|
| 108 |
+
with open('temp_key', 'w') as key_file:
|
| 109 |
+
key_file.write(ssh_private_key)
|
| 110 |
+
|
| 111 |
+
# Connect to GitHub using SSH
|
| 112 |
+
ssh.connect('github.com', username='git', key_filename='temp_key')
|
| 113 |
+
|
| 114 |
+
# Execute git command to fetch file content
|
| 115 |
+
stdin, stdout, stderr = ssh.exec_command(f"git archive --remote=git@github.com:{owner}/{repo}.git {branch} {file_path} | tar -xO")
|
| 116 |
+
file_content = stdout.read().decode('utf-8')
|
| 117 |
+
|
| 118 |
+
# Close SSH connection and remove temporary key file
|
| 119 |
+
ssh.close()
|
| 120 |
+
os.remove('temp_key')
|
| 121 |
+
|
| 122 |
+
if not file_content:
|
| 123 |
+
return "Error: File not found or empty."
|
| 124 |
+
|
| 125 |
return file_content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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."
|
| 132 |
|
| 133 |
if file is not None:
|
| 134 |
file_content = file.decode('utf-8')
|
| 135 |
+
elif github_url and ssh_private_key:
|
| 136 |
if not github_url.startswith("https://github.com/"):
|
| 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 = fetch_github_info(github_url, ssh_private_key)
|
| 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 |
+
return extract_licenses(file_content, github_url, ssh_private_key, gemini_api_key)
|
| 148 |
except Exception as e:
|
| 149 |
return f"Error processing the file: {str(e)}"
|
| 150 |
|
|
|
|
| 153 |
inputs=[
|
| 154 |
gr.File(label="Upload dependency file (e.g., requirements.txt, package.json, Gemfile)"),
|
| 155 |
gr.Textbox(label="GitHub File URL (optional)"),
|
| 156 |
+
gr.Textbox(label="SSH Private Key (required if using GitHub URL)", type="password"),
|
| 157 |
gr.Textbox(label="Gemini API Key", type="password"),
|
| 158 |
],
|
| 159 |
outputs=gr.Textbox(label="License Information and Analysis"),
|