bluenevus commited on
Commit
0c3d4b2
·
verified ·
1 Parent(s): ef736dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -60
app.py CHANGED
@@ -1,59 +1,38 @@
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:
8
  # Parse the GitHub URL
9
- parts = github_url.split(':')[1].split('/')
10
- owner = parts[0]
11
- repo = parts[1].split('.git')[0]
12
  branch = 'main' # You might want to make this configurable
13
- file_path = '' # You might want to make this configurable
14
 
15
- # Set up SSH client
16
- ssh = paramiko.SSHClient()
17
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
18
 
19
- # Clean and format the SSH key
20
- #ssh_private_key = '\n'.join(line.strip() for line in ssh_private_key.split('\n') if line.strip())
21
- #if not ssh_private_key.startswith("-----BEGIN"):
22
- # ssh_private_key = f"-----BEGIN OPENSSH PRIVATE KEY-----\n{ssh_private_key}\n-----END OPENSSH PRIVATE KEY-----"
 
23
 
24
- # Format the SSH key
25
- formatted_key = ssh_private_key.strip()
26
- if not formatted_key.startswith("-----BEGIN"):
27
- formatted_key = f"-----BEGIN OPENSSH PRIVATE KEY-----\n{formatted_key}\n-----END OPENSSH PRIVATE KEY-----"
28
 
29
-
30
- # Load the private key
31
- private_key = paramiko.RSAKey.from_private_key(io.StringIO(formatted_key))
32
-
33
- # Connect to GitHub using SSH
34
- ssh.connect('github.com', username='git', pkey=private_key)
35
-
36
- # Construct the git command to fetch the file content
37
- git_command = f"git archive --remote=git@github.com:{owner}/{repo}.git {branch} {file_path} | tar -xO"
38
-
39
- # Execute the command
40
- stdin, stdout, stderr = ssh.exec_command(git_command)
41
-
42
- # Read the file content
43
- file_content = stdout.read().decode('utf-8')
44
-
45
- # Close the SSH connection
46
- ssh.close()
47
 
48
  if not file_content:
49
  return "Error: File not found or empty."
50
 
51
  return file_content
52
- except paramiko.AuthenticationException:
53
- return "Error: Authentication failed. Please check your SSH key."
54
- except paramiko.SSHException as ssh_err:
55
- return f"SSH Error: {str(ssh_err)}"
56
- except Exception as e:
57
  return f"Error accessing GitHub: {str(e)}"
58
 
59
  def process_with_gemini(file_content, gemini_api_key):
@@ -76,22 +55,15 @@ def process_with_gemini(file_content, gemini_api_key):
76
  response = model.generate_content(prompt)
77
  return response.text
78
 
79
- def process_input(file, github_url, ssh_private_key, gemini_api_key):
80
- if file is not None and github_url:
81
- return "Error: Please either upload a file OR provide a GitHub URL, not both."
 
 
82
 
83
- if file is not None:
84
- file_content = file.decode('utf-8')
85
- elif github_url and ssh_private_key:
86
- if not github_url.startswith("git@github.com:"):
87
- return "Error: Invalid GitHub URL. Please use the format: git@github.com:username/repository.git"
88
- if not ssh_private_key.strip():
89
- return "Error: SSH Private Key is empty. Please provide a valid key."
90
- file_content = fetch_github_file(github_url, ssh_private_key)
91
- if file_content.startswith("Error:"):
92
- return file_content
93
- else:
94
- return "Error: Please either upload a file OR provide both GitHub URL and SSH Private Key."
95
 
96
  try:
97
  # Process the file content with Gemini
@@ -103,14 +75,13 @@ def process_input(file, github_url, ssh_private_key, gemini_api_key):
103
  iface = gr.Interface(
104
  fn=process_input,
105
  inputs=[
106
- gr.File(label="Upload dependency file (e.g., requirements.txt, package.json, Gemfile)"),
107
- gr.Textbox(label="GitHub File URL (optional)"),
108
- gr.TextArea(label="SSH Private Key (required if using GitHub URL)", lines=12),
109
  gr.Textbox(label="Gemini API Key", type="password"),
110
  ],
111
  outputs=gr.Textbox(label="License Information and Analysis"),
112
  title="Open Source License Extractor",
113
- description="Upload a dependency file or provide a GitHub repository URL to analyze open-source licenses.",
114
  )
115
 
116
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import google.generativeai as genai
3
+ import requests
4
+ import base64
5
 
6
+ def fetch_github_file(github_url, personal_access_token):
7
  try:
8
  # Parse the GitHub URL
9
+ parts = github_url.split('/')
10
+ owner = parts[3]
11
+ repo = parts[4].split('.git')[0]
12
  branch = 'main' # You might want to make this configurable
13
+ file_path = 'requirements.txt' # You might want to make this configurable
14
 
15
+ # Construct the API URL
16
+ api_url = f"https://api.github.com/repos/{owner}/{repo}/contents/{file_path}?ref={branch}"
 
17
 
18
+ # Set up headers with the personal access token
19
+ headers = {
20
+ "Authorization": f"token {personal_access_token}",
21
+ "Accept": "application/vnd.github.v3.raw"
22
+ }
23
 
24
+ # Make the API request
25
+ response = requests.get(api_url, headers=headers)
26
+ response.raise_for_status()
 
27
 
28
+ # Decode the content
29
+ file_content = base64.b64decode(response.json()['content']).decode('utf-8')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  if not file_content:
32
  return "Error: File not found or empty."
33
 
34
  return file_content
35
+ except requests.exceptions.RequestException as e:
 
 
 
 
36
  return f"Error accessing GitHub: {str(e)}"
37
 
38
  def process_with_gemini(file_content, gemini_api_key):
 
55
  response = model.generate_content(prompt)
56
  return response.text
57
 
58
+ def process_input(github_url, personal_access_token, gemini_api_key):
59
+ if not github_url.startswith("https://github.com/"):
60
+ return "Error: Invalid GitHub URL. Please use the format: https://github.com/username/repository.git"
61
+ if not personal_access_token.strip():
62
+ return "Error: Personal Access Token is empty. Please provide a valid token."
63
 
64
+ file_content = fetch_github_file(github_url, personal_access_token)
65
+ if file_content.startswith("Error:"):
66
+ return file_content
 
 
 
 
 
 
 
 
 
67
 
68
  try:
69
  # Process the file content with Gemini
 
75
  iface = gr.Interface(
76
  fn=process_input,
77
  inputs=[
78
+ gr.Textbox(label="GitHub Repository URL (format: https://github.com/username/repository.git)"),
79
+ gr.Textbox(label="GitHub Personal Access Token", type="password"),
 
80
  gr.Textbox(label="Gemini API Key", type="password"),
81
  ],
82
  outputs=gr.Textbox(label="License Information and Analysis"),
83
  title="Open Source License Extractor",
84
+ description="Provide a GitHub repository URL to analyze open-source licenses.",
85
  )
86
 
87
  if __name__ == "__main__":