bluenevus commited on
Commit
3021037
·
verified ·
1 Parent(s): d8afc39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -21
app.py CHANGED
@@ -68,7 +68,7 @@ def determine_probable_packages(file_content, dependencies):
68
 
69
  def enrich_with_gemini(licenses, probable_packages, api_key):
70
  genai.configure(api_key=api_key)
71
- model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
72
 
73
  prompt = f"""
74
  Analyze the following open-source license information and probable packages:
@@ -96,31 +96,25 @@ def fetch_github_info(github_url, github_token):
96
  # Create a Github instance with authentication
97
  g = Github(auth=auth)
98
 
99
- # Extract the repository name from the URL
100
- repo_name = github_url.split('github.com/')[-1]
101
- print(f"Attempting to access repository: {repo_name}")
 
 
 
102
 
103
  # Get the repository
104
  repo = g.get_repo(repo_name)
105
 
106
- file_content = ""
107
- for file_name in ["requirements.txt", "package.json", "Gemfile"]:
108
- try:
109
- contents = repo.get_contents(file_name)
110
- file_content = contents.decoded_content.decode('utf-8')
111
- print(f"Successfully retrieved {file_name}")
112
- break
113
- except GithubException as e:
114
- print(f"Failed to retrieve {file_name}: {str(e)}")
115
- continue
116
-
117
- if not file_content:
118
- return "Error: Could not find requirements.txt, package.json, or Gemfile in the repository."
119
 
120
  return file_content
121
  except GithubException as e:
122
  if e.status == 404:
123
- return f"Error: Repository not found or access denied. Please check the URL and ensure you have the correct access permissions. Details: {str(e)}"
124
  else:
125
  return f"Error accessing GitHub: {str(e)}"
126
  except Exception as e:
@@ -134,7 +128,7 @@ def process_input(file, github_url, github_token, gemini_api_key):
134
  file_content = file.decode('utf-8')
135
  elif github_url and github_token:
136
  if not github_url.startswith("https://github.com/"):
137
- return "Error: Invalid GitHub URL. Please use the format: https://github.com/username/repository"
138
  if not github_token.strip():
139
  return "Error: GitHub Personal Access Token is empty. Please provide a valid token."
140
  file_content = fetch_github_info(github_url, github_token)
@@ -152,13 +146,13 @@ iface = gr.Interface(
152
  fn=process_input,
153
  inputs=[
154
  gr.File(label="Upload dependency file (e.g., requirements.txt, package.json, Gemfile)"),
155
- gr.Textbox(label="GitHub Repository URL (optional)"),
156
  gr.Textbox(label="GitHub Personal Access Token (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"),
160
  title="Open Source License Extractor",
161
- description="Upload a dependency file OR provide a GitHub repository URL to extract and analyze open-source license information.",
162
  )
163
 
164
  if __name__ == "__main__":
 
68
 
69
  def enrich_with_gemini(licenses, probable_packages, api_key):
70
  genai.configure(api_key=api_key)
71
+ model = genai.GenerativeModel('gemini-pro')
72
 
73
  prompt = f"""
74
  Analyze the following open-source license information and probable packages:
 
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:
 
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)
 
146
  fn=process_input,
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"),
154
  title="Open Source License Extractor",
155
+ description="Upload a dependency file OR provide a GitHub file URL to extract and analyze open-source license information.",
156
  )
157
 
158
  if __name__ == "__main__":