bluenevus commited on
Commit
b8ccbf8
·
verified ·
1 Parent(s): af2579a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -100
app.py CHANGED
@@ -2,112 +2,35 @@ import gradio as gr
2
  import requests
3
  import json
4
  import re
5
- from github import Github
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
-
12
- # Fetch license information
13
- licenses = fetch_license_info(dependencies)
14
-
15
- # Determine probable packages
16
- probable_packages = determine_probable_packages(file_content, dependencies)
17
-
18
- # Enrich information using Gemini
19
- enriched_info = enrich_with_gemini(licenses, probable_packages, gemini_api_key)
20
-
21
- return enriched_info
22
-
23
- def parse_dependency_file(file_content):
24
- dependencies = []
25
- lines = file_content.split('\n')
26
- for line in lines:
27
- if '=' in line or '@' in line or ':' in line:
28
- parts = re.split(r'[=@:]', line)
29
- package = parts[0].strip()
30
- version = parts[1].strip() if len(parts) > 1 else "latest"
31
- dependencies.append((package, version))
32
- return dependencies
33
-
34
- def fetch_license_info(dependencies):
35
- licenses = []
36
- for package, version in dependencies:
37
- try:
38
- response = requests.get(f"https://pypi.org/pypi/{package}/{version}/json")
39
- data = response.json()
40
- license = data['info'].get('license', 'Unknown')
41
- description = data['info'].get('summary', 'No description available')
42
- licenses.append(f"Package: {package}\nVersion: {version}\nLicense: {license}\nDescription: {description}\n")
43
- except:
44
- licenses.append(f"Package: {package}\nVersion: {version}\nLicense: Unknown\nDescription: Unable to fetch information\n")
45
- return "\n".join(licenses)
46
-
47
- def determine_probable_packages(file_content, dependencies):
48
- probable_packages = []
49
- if "package.json" in file_content.lower():
50
- probable_packages.append("npm (Node Package Manager)")
51
- elif "gemfile" in file_content.lower():
52
- probable_packages.append("Bundler (Ruby)")
53
- elif "requirements.txt" in file_content.lower():
54
- probable_packages.append("pip (Python Package Installer)")
55
-
56
- # Add more probable packages based on common dependencies
57
- common_packages = {
58
- "react": "React (JavaScript library)",
59
- "django": "Django (Python web framework)",
60
- "rails": "Ruby on Rails (Web application framework)",
61
- }
62
-
63
- for package, _ in dependencies:
64
- if package.lower() in common_packages:
65
- probable_packages.append(common_packages[package.lower()])
66
-
67
- return "\n".join(probable_packages)
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:
75
-
76
- License Information:
77
- {licenses}
78
-
79
- Probable Packages:
80
- {probable_packages}
81
-
82
- Please provide a summary of the project based on these dependencies, including:
83
- 1. The likely type of project (e.g., web application, data science, etc.)
84
- 2. Any potential license conflicts or considerations
85
- 3. Suggestions for best practices in open-source license management for this project
86
- """
87
-
88
- response = model.generate_content(prompt)
89
- return response.text
90
 
91
  def fetch_github_info(github_url, github_token):
92
- g = Github(github_token)
93
- repo = g.get_repo(github_url.split('github.com/')[-1])
94
-
95
- file_content = ""
96
  try:
97
- contents = repo.get_contents("requirements.txt")
98
- file_content = contents.decoded_content.decode()
99
- except:
100
- try:
101
- contents = repo.get_contents("package.json")
102
- file_content = contents.decoded_content.decode()
103
- except:
104
  try:
105
- contents = repo.get_contents("Gemfile")
106
- file_content = contents.decoded_content.decode()
107
- except:
108
- return "Error: Could not find requirements.txt, package.json, or Gemfile in the repository."
109
-
110
- return file_content
 
 
 
 
 
 
 
 
 
111
 
112
  def process_input(file, github_url, github_token, gemini_api_key):
113
  if file is not None and github_url:
 
2
  import requests
3
  import json
4
  import re
5
+ from github import Github, GithubException
6
  import google.generativeai as genai
7
 
8
+ # ... (previous functions remain the same)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def fetch_github_info(github_url, github_token):
 
 
 
 
11
  try:
12
+ g = Github(github_token)
13
+ repo_name = github_url.split('github.com/')[-1]
14
+ repo = g.get_repo(repo_name)
15
+
16
+ file_content = ""
17
+ for file_name in ["requirements.txt", "package.json", "Gemfile"]:
 
18
  try:
19
+ contents = repo.get_contents(file_name)
20
+ file_content = contents.decoded_content.decode('utf-8')
21
+ break
22
+ except GithubException:
23
+ continue
24
+
25
+ if not file_content:
26
+ return "Error: Could not find requirements.txt, package.json, or Gemfile in the repository."
27
+
28
+ return file_content
29
+ except GithubException as e:
30
+ if e.status == 404:
31
+ return f"Error: Repository not found. Please check the URL and ensure you have the correct access permissions."
32
+ else:
33
+ return f"Error: {str(e)}"
34
 
35
  def process_input(file, github_url, github_token, gemini_api_key):
36
  if file is not None and github_url: