Upload folder using huggingface_hub
Browse files- README.md +14 -12
- app.py +26 -0
- inference.py +23 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,12 +1,14 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: Python Docstring Generator
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
---
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Python Docstring Generator
|
| 3 |
+
emoji: 📝
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: indigo
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.0.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Python Docstring Generator
|
| 13 |
+
|
| 14 |
+
Paste a Python code snippet to get a short docstring summary (T5-based). By [syeedalireza](https://huggingface.co/syeedalireza).
|
app.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Gradio Space: Python docstring generation. Run: gradio app.py
|
| 3 |
+
"""
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from inference import generate_docstring
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def summarize_code(code: str) -> str:
|
| 9 |
+
if not code or not code.strip():
|
| 10 |
+
return "Paste a Python code snippet above."
|
| 11 |
+
return generate_docstring(code, model_name="t5-small", max_length=128, num_beams=4)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
demo = gr.Interface(
|
| 15 |
+
fn=summarize_code,
|
| 16 |
+
inputs=gr.Textbox(
|
| 17 |
+
label="Python code",
|
| 18 |
+
placeholder="def add(a, b):\n return a + b",
|
| 19 |
+
lines=8,
|
| 20 |
+
),
|
| 21 |
+
outputs=gr.Textbox(label="Generated docstring"),
|
| 22 |
+
title="Python Docstring Generator",
|
| 23 |
+
description="Paste a Python function or snippet to get a short docstring summary.",
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
demo.launch()
|
inference.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Inference for docstring generation. Uses T5.
|
| 3 |
+
"""
|
| 4 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def generate_docstring(
|
| 9 |
+
code: str,
|
| 10 |
+
model_name: str = "t5-small",
|
| 11 |
+
max_length: int = 128,
|
| 12 |
+
num_beams: int = 4,
|
| 13 |
+
device: str = None,
|
| 14 |
+
) -> str:
|
| 15 |
+
if device is None:
|
| 16 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 18 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device)
|
| 19 |
+
input_text = "summarize: " + code
|
| 20 |
+
inputs = tokenizer(input_text, return_tensors="pt", truncation=True, max_length=512).to(device)
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
out = model.generate(**inputs, max_length=max_length, num_beams=num_beams, early_stopping=True)
|
| 23 |
+
return tokenizer.decode(out[0], skip_special_tokens=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=2.0.0
|
| 2 |
+
transformers>=4.30.0
|
| 3 |
+
gradio>=4.0.0
|