Update app.py
Browse files
app.py
CHANGED
|
@@ -2,48 +2,28 @@ import gradio as gr
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import paramiko
|
| 4 |
import io
|
| 5 |
-
import re
|
| 6 |
-
|
| 7 |
-
def parse_github_url(github_url):
|
| 8 |
-
if github_url.startswith("git@github.com:"):
|
| 9 |
-
# SSH format
|
| 10 |
-
parts = github_url.split(':')[1].split('/')
|
| 11 |
-
owner = parts[0]
|
| 12 |
-
repo = parts[1].split('.git')[0]
|
| 13 |
-
return owner, repo, None, None
|
| 14 |
-
elif github_url.startswith("https://github.com/"):
|
| 15 |
-
# HTTPS format
|
| 16 |
-
parts = github_url.split('/')
|
| 17 |
-
owner = parts[3]
|
| 18 |
-
repo = parts[4]
|
| 19 |
-
branch = parts[6] if len(parts) > 6 else None
|
| 20 |
-
file_path = '/'.join(parts[7:]) if len(parts) > 7 else None
|
| 21 |
-
return owner, repo, branch, file_path
|
| 22 |
-
else:
|
| 23 |
-
raise ValueError("Invalid GitHub URL format")
|
| 24 |
|
| 25 |
def fetch_github_file(github_url, ssh_private_key):
|
| 26 |
try:
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Set up SSH client
|
| 30 |
ssh = paramiko.SSHClient()
|
| 31 |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
| 32 |
|
| 33 |
# Load the private key
|
| 34 |
-
|
| 35 |
-
private_key = paramiko.RSAKey.from_private_key(io.StringIO(ssh_private_key.strip()))
|
| 36 |
-
except paramiko.ssh_exception.SSHException as e:
|
| 37 |
-
return f"Error: Invalid SSH key - {str(e)}"
|
| 38 |
|
| 39 |
# Connect to GitHub using SSH
|
| 40 |
ssh.connect('github.com', username='git', pkey=private_key)
|
| 41 |
|
| 42 |
# Construct the git command to fetch the file content
|
| 43 |
-
|
| 44 |
-
git_command = f"git archive --remote=git@github.com:{owner}/{repo}.git {branch} {file_path} | tar -xO"
|
| 45 |
-
else:
|
| 46 |
-
git_command = f"git ls-tree -r --name-only HEAD | grep -E '(requirements\.txt|package\.json|Gemfile)'"
|
| 47 |
|
| 48 |
# Execute the command
|
| 49 |
stdin, stdout, stderr = ssh.exec_command(git_command)
|
|
@@ -56,8 +36,8 @@ def fetch_github_file(github_url, ssh_private_key):
|
|
| 56 |
|
| 57 |
if not file_content:
|
| 58 |
return {"error": "Error: File not found or empty."}
|
| 59 |
-
|
| 60 |
-
return {file_path
|
| 61 |
except paramiko.AuthenticationException:
|
| 62 |
return {"error": "Error: Authentication failed. Please check your SSH key."}
|
| 63 |
except paramiko.SSHException as ssh_err:
|
|
@@ -94,16 +74,13 @@ def process_input(file, github_url, ssh_private_key, gemini_api_key):
|
|
| 94 |
if file is not None:
|
| 95 |
file_content = {file.name: file.read().decode('utf-8')}
|
| 96 |
elif github_url and ssh_private_key:
|
|
|
|
|
|
|
| 97 |
if not ssh_private_key.strip():
|
| 98 |
return "Error: SSH Private Key is empty. Please provide a valid key."
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
file_content = fetch_github_file(github_url, ssh_private_key)
|
| 103 |
-
if "error" in file_content:
|
| 104 |
-
return file_content["error"]
|
| 105 |
-
except ValueError as e:
|
| 106 |
-
return str(e)
|
| 107 |
else:
|
| 108 |
return "Error: Please either upload a file OR provide both GitHub URL and SSH Private Key."
|
| 109 |
|
|
@@ -118,7 +95,7 @@ iface = gr.Interface(
|
|
| 118 |
fn=process_input,
|
| 119 |
inputs=[
|
| 120 |
gr.File(label="Upload dependency file (e.g., requirements.txt, package.json, Gemfile)"),
|
| 121 |
-
gr.Textbox(label="GitHub
|
| 122 |
gr.TextArea(label="SSH Private Key (required if using GitHub URL)", lines=10),
|
| 123 |
gr.Textbox(label="Gemini API Key", type="password"),
|
| 124 |
],
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import paramiko
|
| 4 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def fetch_github_file(github_url, ssh_private_key):
|
| 7 |
try:
|
| 8 |
+
# Parse the GitHub URL
|
| 9 |
+
parts = github_url.split('/')
|
| 10 |
+
owner = parts[3]
|
| 11 |
+
repo = parts[4]
|
| 12 |
+
branch = parts[6]
|
| 13 |
+
file_path = '/'.join(parts[7:])
|
| 14 |
|
| 15 |
# Set up SSH client
|
| 16 |
ssh = paramiko.SSHClient()
|
| 17 |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
| 18 |
|
| 19 |
# Load the private key
|
| 20 |
+
private_key = paramiko.RSAKey.from_private_key(io.StringIO(ssh_private_key))
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Connect to GitHub using SSH
|
| 23 |
ssh.connect('github.com', username='git', pkey=private_key)
|
| 24 |
|
| 25 |
# Construct the git command to fetch the file content
|
| 26 |
+
git_command = f"git archive --remote=git@github.com:{owner}/{repo}.git {branch} {file_path} | tar -xO"
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Execute the command
|
| 29 |
stdin, stdout, stderr = ssh.exec_command(git_command)
|
|
|
|
| 36 |
|
| 37 |
if not file_content:
|
| 38 |
return {"error": "Error: File not found or empty."}
|
| 39 |
+
|
| 40 |
+
return {file_path: file_content}
|
| 41 |
except paramiko.AuthenticationException:
|
| 42 |
return {"error": "Error: Authentication failed. Please check your SSH key."}
|
| 43 |
except paramiko.SSHException as ssh_err:
|
|
|
|
| 74 |
if file is not None:
|
| 75 |
file_content = {file.name: file.read().decode('utf-8')}
|
| 76 |
elif github_url and ssh_private_key:
|
| 77 |
+
if not github_url.startswith("https://github.com/"):
|
| 78 |
+
return "Error: Invalid GitHub URL. Please use the format: https://github.com/username/repository/blob/branch/path/to/file"
|
| 79 |
if not ssh_private_key.strip():
|
| 80 |
return "Error: SSH Private Key is empty. Please provide a valid key."
|
| 81 |
+
file_content = fetch_github_file(github_url, ssh_private_key)
|
| 82 |
+
if isinstance(file_content, dict) and "error" in file_content:
|
| 83 |
+
return file_content["error"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
else:
|
| 85 |
return "Error: Please either upload a file OR provide both GitHub URL and SSH Private Key."
|
| 86 |
|
|
|
|
| 95 |
fn=process_input,
|
| 96 |
inputs=[
|
| 97 |
gr.File(label="Upload dependency file (e.g., requirements.txt, package.json, Gemfile)"),
|
| 98 |
+
gr.Textbox(label="GitHub File URL (optional)"),
|
| 99 |
gr.TextArea(label="SSH Private Key (required if using GitHub URL)", lines=10),
|
| 100 |
gr.Textbox(label="Gemini API Key", type="password"),
|
| 101 |
],
|