File size: 939 Bytes
4f01e40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
Gradio Space: Python docstring generation.
HF runs the app; do not call demo.launch() here.
"""
import gradio as gr

def summarize_code(code: str) -> str:
    if not code or not code.strip():
        return "Paste a Python code snippet above."
    try:
        from inference import generate_docstring
        return generate_docstring(code, model_name="t5-small", max_length=128, num_beams=4)
    except Exception as e:
        return f"Error: {str(e)}. (Model may be loading; try again.)"

demo = gr.Interface(
    fn=summarize_code,
    inputs=gr.Textbox(
        label="Python code",
        placeholder="def add(a, b):\n    return a + b",
        lines=8,
    ),
    outputs=gr.Textbox(label="Generated docstring"),
    title="Python Docstring Generator",
    description="Paste a Python function or snippet to get a short docstring summary.",
)
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)