Update gradio_app.py
Browse files- gradio_app.py +86 -8
gradio_app.py
CHANGED
|
@@ -1,8 +1,86 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# gradio_demo.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import tempfile
|
| 4 |
+
import zipfile
|
| 5 |
+
import os
|
| 6 |
+
from summarizers.file_summarizers import summarize_file_with_graph
|
| 7 |
+
from summarizers.repo_summarizers import summarize_repo_with_graph
|
| 8 |
+
from models.models import func_summarizer
|
| 9 |
+
|
| 10 |
+
# ------------------------------------------------------------------
|
| 11 |
+
# 1. Function-level tab
|
| 12 |
+
# ------------------------------------------------------------------
|
| 13 |
+
def summarize_function(code: str) -> str:
|
| 14 |
+
if not code.strip():
|
| 15 |
+
return "⚠️ Please paste some code."
|
| 16 |
+
out = func_summarizer(code, max_length=64, do_sample=False, truncation=True)
|
| 17 |
+
return out[0]["generated_text"].strip()
|
| 18 |
+
|
| 19 |
+
# ------------------------------------------------------------------
|
| 20 |
+
# 2. File-level tab
|
| 21 |
+
# ------------------------------------------------------------------
|
| 22 |
+
def summarize_file(file_path) -> str:
|
| 23 |
+
if file_path is None:
|
| 24 |
+
return "⚠️ No file uploaded."
|
| 25 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
| 26 |
+
code = f.read()
|
| 27 |
+
return summarize_file_with_graph(code, top_k=5)
|
| 28 |
+
|
| 29 |
+
# ------------------------------------------------------------------
|
| 30 |
+
# 3. Repository-level tab
|
| 31 |
+
# ------------------------------------------------------------------
|
| 32 |
+
def summarize_repo(file_list):
|
| 33 |
+
file_contents = {}
|
| 34 |
+
|
| 35 |
+
for file_or_zip in file_list:
|
| 36 |
+
path = file_or_zip.name
|
| 37 |
+
# Case 1: single .py file
|
| 38 |
+
if path.endswith(".py"):
|
| 39 |
+
with open(path, "r", encoding="utf-8") as fh:
|
| 40 |
+
file_contents[os.path.basename(path)] = fh.read()
|
| 41 |
+
|
| 42 |
+
# Case 2: ZIP archive
|
| 43 |
+
elif path.endswith(".zip"):
|
| 44 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 45 |
+
with zipfile.ZipFile(path, "r") as zf:
|
| 46 |
+
zf.extractall(tmpdir)
|
| 47 |
+
for root, _, files in os.walk(tmpdir):
|
| 48 |
+
for f in files:
|
| 49 |
+
if f.endswith(".py"):
|
| 50 |
+
abs_path = os.path.join(root, f)
|
| 51 |
+
rel_path = os.path.relpath(abs_path, tmpdir)
|
| 52 |
+
with open(abs_path, "r", encoding="utf-8") as fh:
|
| 53 |
+
file_contents[rel_path] = fh.read()
|
| 54 |
+
|
| 55 |
+
if not file_contents:
|
| 56 |
+
return "⚠️ No Python files found."
|
| 57 |
+
|
| 58 |
+
return summarize_repo_with_graph(file_contents, top_files=5, top_k_funcs=5)
|
| 59 |
+
|
| 60 |
+
# ------------------------------------------------------------------
|
| 61 |
+
# Gradio UI
|
| 62 |
+
# ------------------------------------------------------------------
|
| 63 |
+
with gr.Blocks(title="Large-Code-Summarizer") as demo:
|
| 64 |
+
gr.Markdown("# 🧠 Large-Code-Summarizer\nGenerate summaries at **function**, **file**, or **repository** level.")
|
| 65 |
+
|
| 66 |
+
with gr.Tab("Function"):
|
| 67 |
+
func_code = gr.Code(language="python", label="Paste a Python function")
|
| 68 |
+
func_out = gr.Textbox(label="Summary", lines=2, interactive=False)
|
| 69 |
+
func_btn = gr.Button("Summarize")
|
| 70 |
+
func_btn.click(summarize_function, inputs=func_code, outputs=func_out)
|
| 71 |
+
|
| 72 |
+
with gr.Tab("File"):
|
| 73 |
+
file_in = gr.File(file_types=[".py"], label="Upload .py file")
|
| 74 |
+
file_out = gr.Textbox(label="Summary", lines=8, interactive=False)
|
| 75 |
+
file_btn = gr.Button("Summarize")
|
| 76 |
+
file_btn.click(summarize_file, inputs=file_in, outputs=file_out)
|
| 77 |
+
|
| 78 |
+
with gr.Tab("Repository"):
|
| 79 |
+
repo_in = gr.File(file_count="multiple", file_types=[".py", ".zip"], label="Upload .py files or zip")
|
| 80 |
+
repo_out = gr.Textbox(label="Summary", lines=10, interactive=False)
|
| 81 |
+
repo_btn = gr.Button("Summarize")
|
| 82 |
+
repo_btn.click(summarize_repo, inputs=repo_in, outputs=repo_out)
|
| 83 |
+
|
| 84 |
+
# Launch
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
demo.launch(share=False)
|