bluenevus commited on
Commit
be514d7
·
verified ·
1 Parent(s): 196092b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -42
app.py CHANGED
@@ -4,21 +4,15 @@ 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,48 +24,41 @@ def fetch_github_file(github_url, ssh_private_key):
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:
58
- return f"SSH Error: {str(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
@@ -88,13 +75,13 @@ def process_input(file, github_url, ssh_private_key, gemini_api_key):
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."
100
 
@@ -109,7 +96,7 @@ iface = gr.Interface(
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
  ],
 
4
  import io
5
  import re
6
 
 
 
 
 
 
 
 
 
7
  def fetch_github_file(github_url, ssh_private_key):
8
  try:
9
+ # Parse the GitHub URL
10
+ parts = github_url.split('/')
11
+ owner = parts[3]
12
+ repo = parts[4]
13
+ branch = parts[6]
14
+ file_path = '/'.join(parts[7:])
15
+
16
  # Set up SSH client
17
  ssh = paramiko.SSHClient()
18
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 
24
  ssh.connect('github.com', username='git', pkey=private_key)
25
 
26
  # Construct the git command to fetch the file content
27
+ git_command = f"git archive --[email protected]:{owner}/{repo}.git {branch} {file_path} | tar -xO"
28
 
29
  # Execute the command
30
  stdin, stdout, stderr = ssh.exec_command(git_command)
31
 
32
+ # Read the file content
33
+ file_content = stdout.read().decode('utf-8')
 
 
 
 
 
 
34
 
35
  # Close the SSH connection
36
  ssh.close()
37
 
38
+ if not file_content:
39
+ return {"error": "Error: File not found or empty."}
40
+
41
+ return {file_path: file_content}
 
42
  except paramiko.AuthenticationException:
43
+ return {"error": "Error: Authentication failed. Please check your SSH key."}
44
  except paramiko.SSHException as ssh_err:
45
+ return {"error": f"SSH Error: {str(ssh_err)}"}
46
  except Exception as e:
47
+ return {"error": f"Error accessing GitHub: {str(e)}"}
48
 
49
+ def process_with_gemini(file_content, gemini_api_key):
50
  genai.configure(api_key=gemini_api_key)
51
+ model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
52
+
53
+ combined_content = "\n\n".join([f"File: {name}\n{content}" for name, content in file_content.items()])
54
+
55
  prompt = f"""
56
+ Analyze the following file content for open-source license information:
57
 
58
  {combined_content}
59
 
60
  Please provide:
61
+ 1. A list of dependencies and their versions
62
  2. The licenses associated with each dependency (if available)
63
  3. A summary of the project based on these dependencies
64
  4. Any potential license conflicts or considerations
 
75
  if file is not None:
76
  file_content = {file.name: file.read().decode('utf-8')}
77
  elif github_url and ssh_private_key:
78
+ if not github_url.startswith("https://github.com/"):
79
+ return "Error: Invalid GitHub URL. Please use the format: https://github.com/username/repository/blob/branch/path/to/file"
80
  if not ssh_private_key.strip():
81
  return "Error: SSH Private Key is empty. Please provide a valid key."
82
  file_content = fetch_github_file(github_url, ssh_private_key)
83
+ if isinstance(file_content, dict) and "error" in file_content:
84
+ return file_content["error"]
85
  else:
86
  return "Error: Please either upload a file OR provide both GitHub URL and SSH Private Key."
87
 
 
96
  fn=process_input,
97
  inputs=[
98
  gr.File(label="Upload dependency file (e.g., requirements.txt, package.json, Gemfile)"),
99
+ gr.Textbox(label="GitHub File URL (optional)"),
100
  gr.Textbox(label="SSH Private Key (required if using GitHub URL)", type="password"),
101
  gr.Textbox(label="Gemini API Key", type="password"),
102
  ],