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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -24
app.py CHANGED
@@ -1,6 +1,7 @@
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,32 +12,36 @@ def fetch_github_file(github_url, ssh_private_key):
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,12 +75,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 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,7 +94,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 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"),
 
1
  import gradio as gr
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:
 
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)
30
+
31
+ # Read the file content
32
+ file_content = stdout.read().decode('utf-8')
33
+
34
+ # Close the SSH connection
35
+ ssh.close()
36
 
37
  if not file_content:
38
+ return "Error: File not found or empty."
39
 
40
  return file_content
41
+ except paramiko.AuthenticationException:
42
+ return "Error: Authentication failed. Please check your SSH key."
43
+ except paramiko.SSHException as ssh_err:
44
+ return f"SSH Error: {str(ssh_err)}"
 
 
 
45
  except Exception as e:
46
  return f"Error accessing GitHub: {str(e)}"
47
 
 
75
  if not github_url.startswith("https://github.com/"):
76
  return "Error: Invalid GitHub URL. Please use the format: https://github.com/username/repository/blob/branch/path/to/file"
77
  if not ssh_private_key.strip():
78
+ return "Error: SSH Private Key is empty. Please provide a valid key."
79
  file_content = fetch_github_file(github_url, ssh_private_key)
80
  if file_content.startswith("Error:"):
81
  return file_content
82
  else:
83
+ return "Error: Please either upload a file OR provide both GitHub URL and SSH Private Key."
84
 
85
  try:
86
  # Process the file content with Gemini
 
94
  inputs=[
95
  gr.File(label="Upload dependency file (e.g., requirements.txt, package.json, Gemfile)"),
96
  gr.Textbox(label="GitHub File URL (optional)"),
97
+ gr.Textbox(label="SSH Private Key (required if using GitHub URL)", type="password"),
98
  gr.Textbox(label="Gemini API Key", type="password"),
99
  ],
100
  outputs=gr.Textbox(label="License Information and Analysis"),