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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -25
app.py CHANGED
@@ -2,16 +2,23 @@ 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('/')
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())
@@ -23,21 +30,28 @@ def fetch_github_file(github_url, ssh_private_key):
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 --[email protected]:{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:
@@ -45,17 +59,19 @@ def fetch_github_file(github_url, ssh_private_key):
45
  except Exception as e:
46
  return f"Error accessing GitHub: {str(e)}"
47
 
48
- def process_with_gemini(file_content, gemini_api_key):
49
  genai.configure(api_key=gemini_api_key)
50
- model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
51
-
 
 
52
  prompt = f"""
53
- Analyze the following file content for open-source license information:
54
 
55
- {file_content}
56
 
57
  Please provide:
58
- 1. A list of dependencies and their versions
59
  2. The licenses associated with each dependency (if available)
60
  3. A summary of the project based on these dependencies
61
  4. Any potential license conflicts or considerations
@@ -70,14 +86,14 @@ def process_input(file, github_url, ssh_private_key, gemini_api_key):
70
  return "Error: Please either upload a file OR provide a GitHub URL, not both."
71
 
72
  if file is not None:
73
- file_content = file.decode('utf-8')
74
  elif github_url and ssh_private_key:
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."
@@ -93,13 +109,13 @@ iface = gr.Interface(
93
  fn=process_input,
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"),
101
  title="Open Source License Extractor",
102
- description="Upload a dependency file OR provide a GitHub file URL to extract and analyze open-source license information.",
103
  )
104
 
105
  if __name__ == "__main__":
 
2
  import google.generativeai as genai
3
  import paramiko
4
  import io
5
+ import re
6
+
7
+ dependency_file_patterns = "|".join([
8
+ "package\.json", "requirements\.txt", "Gemfile", "pom\.xml", "build\.gradle",
9
+ "composer\.json", "Cargo\.toml", "go\.mod", "project\.clj", "mix\.exs",
10
+ "build\.sbt", "Package\.swift", ".*\.csproj", "packages\.config", "yarn\.lock",
11
+ "package-lock\.json", "Pipfile", "poetry\.lock", "environment\.yml",
12
+ "pubspec\.yaml", "deps\.edn", "rebar\.config", "bower\.json", "Podfile", "Cartfile"
13
+ ])
14
 
15
  def fetch_github_file(github_url, ssh_private_key):
16
  try:
17
+ # Parse the GitHub SSH URL
18
+ parts = github_url.split(':')
19
+ owner_repo = parts[1].split('.git')[0]
20
+ owner, repo = owner_repo.split('/')
21
+
 
 
22
  # Set up SSH client
23
  ssh = paramiko.SSHClient()
24
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 
30
  ssh.connect('github.com', username='git', pkey=private_key)
31
 
32
  # Construct the git command to fetch the file content
33
+ git_command = f"git ls-tree -r --name-only HEAD | grep -E '({dependency_file_patterns})'"
34
 
35
  # Execute the command
36
  stdin, stdout, stderr = ssh.exec_command(git_command)
37
 
38
+ # Read the file list
39
+ file_list = stdout.read().decode('utf-8').splitlines()
40
+
41
+ file_contents = {}
42
+ for file_path in file_list:
43
+ cat_command = f"git show HEAD:{file_path}"
44
+ stdin, stdout, stderr = ssh.exec_command(cat_command)
45
+ file_contents[file_path] = stdout.read().decode('utf-8')
46
 
47
  # Close the SSH connection
48
  ssh.close()
49
 
50
+ if not file_contents:
51
+ return "Error: No dependency files found."
52
+
53
+ return file_contents
54
 
 
55
  except paramiko.AuthenticationException:
56
  return "Error: Authentication failed. Please check your SSH key."
57
  except paramiko.SSHException as ssh_err:
 
59
  except Exception as e:
60
  return f"Error accessing GitHub: {str(e)}"
61
 
62
+ def process_with_gemini(file_contents, gemini_api_key):
63
  genai.configure(api_key=gemini_api_key)
64
+ model = genai.GenerativeModel('gemini-pro')
65
+
66
+ combined_content = "\n\n".join([f"File: {name}\n{content}" for name, content in file_contents.items()])
67
+
68
  prompt = f"""
69
+ Analyze the following dependency files:
70
 
71
+ {combined_content}
72
 
73
  Please provide:
74
+ 1. A list of dependencies and their versions for each file
75
  2. The licenses associated with each dependency (if available)
76
  3. A summary of the project based on these dependencies
77
  4. Any potential license conflicts or considerations
 
86
  return "Error: Please either upload a file OR provide a GitHub URL, not both."
87
 
88
  if file is not None:
89
+ file_content = {file.name: file.read().decode('utf-8')}
90
  elif github_url and ssh_private_key:
91
+ if not re.match(r'^git@github\.com:.+/.+\.git$', github_url):
92
+ return "Error: Invalid GitHub SSH URL. Please use the format: git@github.com:username/repository.git"
93
  if not ssh_private_key.strip():
94
  return "Error: SSH Private Key is empty. Please provide a valid key."
95
  file_content = fetch_github_file(github_url, ssh_private_key)
96
+ if isinstance(file_content, str) and file_content.startswith("Error:"):
97
  return file_content
98
  else:
99
  return "Error: Please either upload a file OR provide both GitHub URL and SSH Private Key."
 
109
  fn=process_input,
110
  inputs=[
111
  gr.File(label="Upload dependency file (e.g., requirements.txt, package.json, Gemfile)"),
112
+ gr.Textbox(label="GitHub Repository URL (optional, use SSH format)"),
113
  gr.Textbox(label="SSH Private Key (required if using GitHub URL)", type="password"),
114
  gr.Textbox(label="Gemini API Key", type="password"),
115
  ],
116
  outputs=gr.Textbox(label="License Information and Analysis"),
117
  title="Open Source License Extractor",
118
+ description="Upload a dependency file or provide a GitHub repository URL to analyze open-source licenses.",
119
  )
120
 
121
  if __name__ == "__main__":