Spaces:
Sleeping
Sleeping
import gradio as gr | |
from pathlib import Path | |
import logging | |
from code_summarizer import ( | |
clone_repo, | |
summarize_repo, | |
upload_summary_to_firebase, | |
is_firestore_available | |
) | |
# Import device/model status separately | |
from code_summarizer.summarizer import device as summarizer_device, MODEL_LOADED as SUMMARIZER_LOADED | |
log = logging.getLogger(__name__) | |
REPO_CLONE_DIR = "cloned_repo_gradio" | |
def format_summaries_for_display(summaries: list) -> str: | |
if not summaries: return "No summaries generated." | |
limit = 5 | |
output = f"β Found {len(summaries)} functions.\n" | |
output += f"Firestore available: {'Yes' if is_firestore_available() else 'No'}\n---\n" | |
for i, summary in enumerate(summaries[:limit]): | |
output += f"File: {summary.get('file_path', '?')}\nLang: {summary.get('language', '?')}\n" | |
output += f"Summary: {summary.get('summary', '?')}\n" | |
output += f"Embedding: {'Yes' if 'embedding' in summary else 'No'}\n---\n" | |
if len(summaries) > limit: | |
output += f"... and {len(summaries) - limit} more." | |
return output | |
def summarize_from_url(repo_url: str): | |
if not repo_url or not repo_url.startswith("https"): | |
yield "β Invalid HTTPS GitHub URL." | |
return | |
if not SUMMARIZER_LOADED: | |
yield "β Summarizer Model Not Loaded. Cannot proceed." | |
return | |
yield "β³ Cloning repository..." | |
clone_dir_path = Path(REPO_CLONE_DIR) | |
if not clone_repo(repo_url, str(clone_dir_path)): | |
yield "β Failed to clone repo." | |
return | |
yield f"β³ Summarizing code (using {summarizer_device})..." | |
summaries = summarize_repo(clone_dir_path, repo_url) | |
if not summaries: | |
yield "β οΈ Repo cloned, but no functions found." | |
return | |
status = f"β Summarized {len(summaries)} functions." | |
yield status + " Uploading to Firebase..." | |
upload_count = 0 | |
if is_firestore_available(): | |
for summary in summaries: | |
try: | |
upload_summary_to_firebase(summary) | |
upload_count += 1 | |
except Exception as e: | |
log.error(f"Gradio UI: Firebase upload error: {e}") | |
status += f" Uploaded {upload_count} to Firebase." | |
yield status + "\n---\n" + format_summaries_for_display(summaries) | |
else: | |
status += " Firebase unavailable, skipping upload." | |
yield status + "\n---\n" + format_summaries_for_display(summaries) | |
def perform_web_search(query: str): | |
# Placeholder - Replace with actual search implementation | |
return f"π Web search (placeholder) for: '{query}'" | |
def launch_interface(): | |
with gr.Blocks(title="Code Summarizer", theme=gr.themes.Soft()) as demo: | |
gr.Markdown("# π Code Summarizer & Search") | |
with gr.Tab("Repo Summarizer"): | |
repo_url_input = gr.Textbox(label="GitHub Repo URL", placeholder="https://github.com/user/repo") | |
summarize_button = gr.Button("Summarize & Upload", variant="primary") | |
status_output = gr.Textbox(label="Status / Output", lines=10, interactive=False) | |
summarize_button.click(fn=summarize_from_url, inputs=repo_url_input, outputs=status_output) | |
with gr.Tab("Web Code Search (Placeholder)"): | |
search_query_input = gr.Textbox(label="Search Query", placeholder="e.g., binary search tree cpp") | |
search_button = gr.Button("Search Web", variant="secondary") | |
search_output_display = gr.Textbox(label="Web Search Results", lines=5, interactive=False) | |
search_button.click(fn=perform_web_search, inputs=search_query_input, outputs=search_output_display) | |
log.info("Launching Gradio interface...") | |
demo.launch() | |
if __name__ == "__main__": | |
# Basic logging setup for the interface if run directly | |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - [Interface] %(message)s') | |
if not SUMMARIZER_LOADED: | |
log.error("Summarizer model failed to load. Interface functionality will be limited.") | |
# Add this check for Firebase as well, since the interface relies on it | |
if not is_firestore_available(): | |
log.warning("Firebase is not available. Upload/check functionality will be disabled.") | |
launch_interface() |