|
import gradio as gr |
|
import google.generativeai as genai |
|
import requests |
|
import base64 |
|
import json |
|
from tenacity import retry, stop_after_attempt, wait_fixed |
|
|
|
def fetch_github_files(github_url, personal_access_token): |
|
|
|
|
|
def process_chunk_with_gemini(chunk, gemini_api_key): |
|
genai.configure(api_key=gemini_api_key) |
|
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25') |
|
|
|
prompt = f""" |
|
Analyze the following file content for open-source license information: |
|
|
|
{chunk} |
|
|
|
Please provide: |
|
1. A numbered list with the name dependency and version as the title |
|
2. 1st bullet under title has a brief summary of what the dependency does |
|
3. 2nd bullet under title has the license name |
|
4. 3rd bullet under title has a hyperlink to the license file |
|
5. Provide no other information such as greeting or summary as the purpose is to catalog and document all open source licenses used. |
|
""" |
|
|
|
response = model.generate_content(prompt) |
|
return response.text |
|
|
|
@retry(stop=stop_after_attempt(3), wait=wait_fixed(2)) |
|
def process_with_gemini(file_content, gemini_api_key): |
|
|
|
chunk_size = 4000 |
|
chunks = [file_content[i:i+chunk_size] for i in range(0, len(file_content), chunk_size)] |
|
|
|
results = [] |
|
for chunk in chunks: |
|
result = process_chunk_with_gemini(chunk, gemini_api_key) |
|
results.append(result) |
|
|
|
|
|
combined_result = "\n\n".join(results) |
|
return combined_result |
|
|
|
def process_input(github_url, personal_access_token, gemini_api_key): |
|
if not github_url.startswith("https://github.com/"): |
|
return "Error: Invalid GitHub URL. Please use the format: https://github.com/username/repository.git" |
|
if not personal_access_token.strip(): |
|
return "Error: Personal Access Token is empty. Please provide a valid token." |
|
|
|
file_content = fetch_github_files(github_url, personal_access_token) |
|
if file_content.startswith("Error:"): |
|
return file_content |
|
|
|
try: |
|
|
|
analysis = process_with_gemini(file_content, gemini_api_key) |
|
return analysis |
|
except Exception as e: |
|
return f"Error processing the files: {str(e)}" |
|
|
|
iface = gr.Interface( |
|
fn=process_input, |
|
inputs=[ |
|
gr.Textbox(label="GitHub Repository URL (format: https://github.com/username/repository.git)"), |
|
gr.Textbox(label="GitHub Personal Access Token", type="password"), |
|
gr.Textbox(label="Gemini API Key", type="password"), |
|
], |
|
outputs=gr.Textbox(label="License Information and Analysis"), |
|
title="Open Source License Extractor", |
|
description="Provide a GitHub repository URL to analyze open-source licenses from dependency files.", |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |