bluenevus commited on
Commit
ec4142d
·
verified ·
1 Parent(s): bb7a708

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -38
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, Auth
6
  import google.generativeai as genai
 
 
 
7
 
8
- def extract_licenses(file_content, github_url, github_token, gemini_api_key):
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, github_token):
92
  try:
93
- # Create an authentication object
94
- auth = Auth.Token(github_token)
95
-
96
- # Create a Github instance with authentication
97
- g = Github(auth=auth)
98
-
99
- # Extract the repository name and file path from the URL
100
- _, _, _, owner, repo, _, *path_parts = github_url.split('/')
101
- repo_name = f"{owner}/{repo}"
102
- file_path = '/'.join(path_parts)
103
-
104
- print(f"Attempting to access file: {file_path} in repository: {repo_name}")
105
-
106
- # Get the repository
107
- repo = g.get_repo(repo_name)
108
-
109
- # Get the file contents
110
- contents = repo.get_contents(file_path)
111
- file_content = contents.decoded_content.decode('utf-8')
112
- print(f"Successfully retrieved {file_path}")
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"Unexpected error: {str(e)}"
122
 
123
- def process_input(file, github_url, github_token, gemini_api_key):
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 github_token:
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 github_token.strip():
133
- return "Error: GitHub Personal Access Token is empty. Please provide a valid token."
134
- file_content = fetch_github_info(github_url, github_token)
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 access token."
139
 
140
  try:
141
- return extract_licenses(file_content, github_url, github_token, gemini_api_key)
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="GitHub Personal Access Token (required if using GitHub URL)", type="password"),
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 [email protected]:{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"),