Update app.py
Browse files
app.py
CHANGED
|
@@ -3,17 +3,28 @@ import google.generativeai as genai
|
|
| 3 |
import requests
|
| 4 |
import base64
|
| 5 |
|
| 6 |
-
def
|
| 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 |
-
#
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Set up headers with the personal access token
|
| 19 |
headers = {
|
|
@@ -21,17 +32,22 @@ def fetch_github_file(github_url, personal_access_token):
|
|
| 21 |
"Accept": "application/vnd.github.v3.raw"
|
| 22 |
}
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
except requests.exceptions.RequestException as e:
|
| 36 |
return f"Error accessing GitHub: {str(e)}"
|
| 37 |
|
|
@@ -61,7 +77,7 @@ def process_input(github_url, personal_access_token, gemini_api_key):
|
|
| 61 |
if not personal_access_token.strip():
|
| 62 |
return "Error: Personal Access Token is empty. Please provide a valid token."
|
| 63 |
|
| 64 |
-
file_content =
|
| 65 |
if file_content.startswith("Error:"):
|
| 66 |
return file_content
|
| 67 |
|
|
@@ -70,7 +86,7 @@ def process_input(github_url, personal_access_token, gemini_api_key):
|
|
| 70 |
analysis = process_with_gemini(file_content, gemini_api_key)
|
| 71 |
return analysis
|
| 72 |
except Exception as e:
|
| 73 |
-
return f"Error processing the
|
| 74 |
|
| 75 |
iface = gr.Interface(
|
| 76 |
fn=process_input,
|
|
@@ -81,7 +97,7 @@ iface = gr.Interface(
|
|
| 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__":
|
|
|
|
| 3 |
import requests
|
| 4 |
import base64
|
| 5 |
|
| 6 |
+
def fetch_github_files(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 |
|
| 14 |
+
# List of common dependency files to look for
|
| 15 |
+
dependency_files = [
|
| 16 |
+
'requirements.txt',
|
| 17 |
+
'package.json',
|
| 18 |
+
'Gemfile',
|
| 19 |
+
'pom.xml',
|
| 20 |
+
'build.gradle',
|
| 21 |
+
'composer.json',
|
| 22 |
+
'Cargo.toml',
|
| 23 |
+
'go.mod',
|
| 24 |
+
'Pipfile'
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
all_content = ""
|
| 28 |
|
| 29 |
# Set up headers with the personal access token
|
| 30 |
headers = {
|
|
|
|
| 32 |
"Accept": "application/vnd.github.v3.raw"
|
| 33 |
}
|
| 34 |
|
| 35 |
+
for file_path in dependency_files:
|
| 36 |
+
# Construct the API URL
|
| 37 |
+
api_url = f"https://api.github.com/repos/{owner}/{repo}/contents/{file_path}?ref={branch}"
|
| 38 |
|
| 39 |
+
# Make the API request
|
| 40 |
+
response = requests.get(api_url, headers=headers)
|
| 41 |
|
| 42 |
+
if response.status_code == 200:
|
| 43 |
+
# Decode the content
|
| 44 |
+
file_content = base64.b64decode(response.json()['content']).decode('utf-8')
|
| 45 |
+
all_content += f"\n\n--- {file_path} ---\n{file_content}"
|
| 46 |
|
| 47 |
+
if not all_content:
|
| 48 |
+
return "Error: No dependency files found in the repository."
|
| 49 |
+
|
| 50 |
+
return all_content
|
| 51 |
except requests.exceptions.RequestException as e:
|
| 52 |
return f"Error accessing GitHub: {str(e)}"
|
| 53 |
|
|
|
|
| 77 |
if not personal_access_token.strip():
|
| 78 |
return "Error: Personal Access Token is empty. Please provide a valid token."
|
| 79 |
|
| 80 |
+
file_content = fetch_github_files(github_url, personal_access_token)
|
| 81 |
if file_content.startswith("Error:"):
|
| 82 |
return file_content
|
| 83 |
|
|
|
|
| 86 |
analysis = process_with_gemini(file_content, gemini_api_key)
|
| 87 |
return analysis
|
| 88 |
except Exception as e:
|
| 89 |
+
return f"Error processing the files: {str(e)}"
|
| 90 |
|
| 91 |
iface = gr.Interface(
|
| 92 |
fn=process_input,
|
|
|
|
| 97 |
],
|
| 98 |
outputs=gr.Textbox(label="License Information and Analysis"),
|
| 99 |
title="Open Source License Extractor",
|
| 100 |
+
description="Provide a GitHub repository URL to analyze open-source licenses from dependency files.",
|
| 101 |
)
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|