CoolFace
Apppublic

cs23-namitha/text-mini

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
app.py86 linesDownload Raw Back to root
1import gradio as gr
2from transformers import pipeline
3import docx
4import PyPDF2
5
6summarizer = pipeline("summarization", model="t5-small", tokenizer="t5-small")
7
8def read_file(file_obj):
9    if file_obj.name.endswith(".txt"):
10        return file_obj.read().decode("utf-8")
11    elif file_obj.name.endswith(".docx"):
12        doc = docx.Document(file_obj)
13        return "\n".join([para.text for para in doc.paragraphs])
14    elif file_obj.name.endswith(".pdf"):
15        reader = PyPDF2.PdfReader(file_obj)
16        text = "\n".join(page.extract_text() for page in reader.pages if page.extract_text())
17        return text
18    else:
19        return "Unsupported file format. Please upload .txt, .docx, or .pdf"
20
21def summarize(text, clean):
22    if clean:
23        text = ''.join(c for c in text if c.isalnum() or c.isspace())
24    result = summarizer(text, max_length=130, min_length=30, do_sample=False)
25    summary = result[0]['summary_text']
26    return summary, f"Words: {len(text.split())}, Characters: {len(text)}"
27
28def app_interface(text_input, file_input, clean):
29    final_text = text_input
30    if file_input is not None:
31        file_text = read_file(file_input)
32        final_text = file_text if not text_input else text_input + "\n" + file_text
33
34    if not final_text.strip():
35        return "Please provide text input or upload a file.", ""
36
37    return summarize(final_text, clean)
38
39# ๐ŸŸฃ Vibrant Custom Theme (No unsupported args)
40custom_theme = gr.themes.Base(
41    primary_hue=gr.themes.Color(
42        c50="#f0f9ff", c100="#e0f2fe", c200="#bae6fd", c300="#7dd3fc", c400="#38bdf8",
43        c500="#0ea5e9", c600="#0284c7", c700="#0369a1", c800="#075985", c900="#0c4a6e", c950="#082f49"
44    ),
45    secondary_hue=gr.themes.Color(
46        c50="#fff1f2", c100="#ffe4e6", c200="#fecdd3", c300="#fda4af", c400="#fb7185",
47        c500="#f43f5e", c600="#e11d48", c700="#be123c", c800="#9f1239", c900="#881337", c950="#4c0519"
48    ),
49    neutral_hue=gr.themes.Color(
50        c50="#f8fafc", c100="#f1f5f9", c200="#e2e8f0", c300="#cbd5e1", c400="#94a3b8",
51        c500="#64748b", c600="#475569", c700="#334155", c800="#1e293b", c900="#0f172a", c950="#020617"
52    )
53).set(
54    body_background_fill="linear-gradient(to right, #0f2027, #203a43, #2c5364)",
55    body_text_color="#ffffff",
56    block_background_fill="#1e293b",
57    block_title_text_color="#38bdf8",
58    block_border_color="#334155",
59    button_primary_background_fill="linear-gradient(to right, #fb7185, #f43f5e)",
60    button_primary_text_color="white",
61    input_background_fill="#0f172a",
62    input_text_color="#ffffff",
63    input_border_color="#334155"
64)
65
66description = "<div style='font-size: 16px; color: #ccc;'>๐Ÿ“„ Upload or paste your content and get an <b>AI-generated summary</b> instantly.</div>"
67
68iface = gr.Interface(
69    fn=app_interface,
70    inputs=[
71        gr.Textbox(label="๐Ÿ“ Input Text", lines=10, placeholder="Paste or type your text here..."),
72        gr.File(label="๐Ÿ“ Upload File (.txt, .docx, .pdf)"),
73        gr.Checkbox(label="โœจ Clean extra symbols", value=True),
74    ],
75    outputs=[
76        gr.Textbox(label="๐Ÿง  Summary Output"),
77        gr.Textbox(label="๐Ÿ“Š Text Info (Words & Characters)"),
78    ],
79    title="๐ŸŒˆ Creative AI Text Summarizer",
80    description=description,
81    allow_flagging="never",
82    theme=custom_theme
83)
84
85iface.launch()
86