bluenevus commited on
Commit
d599929
·
verified ·
1 Parent(s): 4c72ac7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -24
app.py CHANGED
@@ -1,9 +1,6 @@
1
  import gradio as gr
2
- import requests
3
- import json
4
- import paramiko
5
- import os
6
  import google.generativeai as genai
 
7
 
8
  def fetch_github_file(github_url, ssh_private_key):
9
  try:
@@ -14,29 +11,32 @@ def fetch_github_file(github_url, ssh_private_key):
14
  branch = parts[6]
15
  file_path = '/'.join(parts[7:])
16
 
17
- # Set up SSH client
18
- ssh = paramiko.SSHClient()
19
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
20
 
21
- # Create a temporary file to store the SSH private key
22
- with open('temp_key', 'w') as key_file:
23
- key_file.write(ssh_private_key)
24
 
25
- # Connect to GitHub using SSH
26
- ssh.connect('github.com', username='git', key_filename='temp_key')
27
-
28
- # Execute git command to fetch file content
29
- stdin, stdout, stderr = ssh.exec_command(f"git archive [email protected]:{owner}/{repo}.git {branch} {file_path} | tar -xO")
30
- file_content = stdout.read().decode('utf-8')
31
-
32
- # Close SSH connection and remove temporary key file
33
- ssh.close()
34
- os.remove('temp_key')
35
 
36
  if not file_content:
37
- return "Error: File not found or empty."
38
 
39
  return file_content
 
 
 
 
 
 
 
40
  except Exception as e:
41
  return f"Error accessing GitHub: {str(e)}"
42
 
@@ -70,12 +70,12 @@ def process_input(file, github_url, ssh_private_key, gemini_api_key):
70
  if not github_url.startswith("https://github.com/"):
71
  return "Error: Invalid GitHub URL. Please use the format: https://github.com/username/repository/blob/branch/path/to/file"
72
  if not ssh_private_key.strip():
73
- return "Error: SSH Private Key is empty. Please provide a valid key."
74
  file_content = fetch_github_file(github_url, ssh_private_key)
75
  if file_content.startswith("Error:"):
76
  return file_content
77
  else:
78
- return "Error: Please either upload a file OR provide both GitHub URL and SSH Private Key."
79
 
80
  try:
81
  # Process the file content with Gemini
@@ -89,7 +89,7 @@ iface = gr.Interface(
89
  inputs=[
90
  gr.File(label="Upload dependency file (e.g., requirements.txt, package.json, Gemfile)"),
91
  gr.Textbox(label="GitHub File URL (optional)"),
92
- gr.Textbox(label="SSH Private Key (required if using GitHub URL)", type="password"),
93
  gr.Textbox(label="Gemini API Key", type="password"),
94
  ],
95
  outputs=gr.Textbox(label="License Information and Analysis"),
 
1
  import gradio as gr
 
 
 
 
2
  import google.generativeai as genai
3
+ from github import Github, GithubException
4
 
5
  def fetch_github_file(github_url, ssh_private_key):
6
  try:
 
11
  branch = parts[6]
12
  file_path = '/'.join(parts[7:])
13
 
14
+ # Create a Github instance using SSH key or Personal Access Token
15
+ g = Github(login_or_token=ssh_private_key)
 
16
 
17
+ # Get the repository
18
+ repository = g.get_repo(f"{owner}/{repo}")
 
19
 
20
+ try:
21
+ # Get the file content
22
+ file_content = repository.get_contents(file_path, ref=branch).decoded_content.decode('utf-8')
23
+ except GithubException as e:
24
+ if e.status == 404:
25
+ return f"Error: File not found. Please check the path: {file_path}"
26
+ else:
27
+ raise
 
 
28
 
29
  if not file_content:
30
+ return "Error: File is empty."
31
 
32
  return file_content
33
+ except GithubException as e:
34
+ if e.status == 401:
35
+ return "Error: Authentication failed. Please check your SSH key or token."
36
+ elif e.status == 404:
37
+ return f"Error: Repository not found: {owner}/{repo}"
38
+ else:
39
+ return f"GitHub Error: {str(e)}"
40
  except Exception as e:
41
  return f"Error accessing GitHub: {str(e)}"
42
 
 
70
  if not github_url.startswith("https://github.com/"):
71
  return "Error: Invalid GitHub URL. Please use the format: https://github.com/username/repository/blob/branch/path/to/file"
72
  if not ssh_private_key.strip():
73
+ return "Error: SSH Private Key or Personal Access Token is empty. Please provide a valid key or token."
74
  file_content = fetch_github_file(github_url, ssh_private_key)
75
  if file_content.startswith("Error:"):
76
  return file_content
77
  else:
78
+ return "Error: Please either upload a file OR provide both GitHub URL and SSH Private Key or Personal Access Token."
79
 
80
  try:
81
  # Process the file content with Gemini
 
89
  inputs=[
90
  gr.File(label="Upload dependency file (e.g., requirements.txt, package.json, Gemfile)"),
91
  gr.Textbox(label="GitHub File URL (optional)"),
92
+ gr.Textbox(label="SSH Private Key or Personal Access Token (required if using GitHub URL)", type="password"),
93
  gr.Textbox(label="Gemini API Key", type="password"),
94
  ],
95
  outputs=gr.Textbox(label="License Information and Analysis"),