Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,93 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
models = {
|
| 7 |
models = {
|
| 8 |
"π T5-Small": pipeline("text2text-generation", model="t5-small"),
|
| 9 |
"π DistilGPT2": pipeline("text-generation", model="distilgpt2"),
|
| 10 |
"π DistilBART": pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 11 |
}
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
with gr.Blocks(css="""
|
| 30 |
body {
|
| 31 |
-
background-color: #
|
| 32 |
}
|
| 33 |
-
#title {
|
| 34 |
text-align: center;
|
| 35 |
-
font-size:
|
| 36 |
font-weight: bold;
|
| 37 |
color: #8b4513;
|
| 38 |
-
margin-
|
| 39 |
}
|
| 40 |
.output-box {
|
| 41 |
-
border: 2px
|
| 42 |
-
padding:
|
| 43 |
border-radius: 10px;
|
| 44 |
-
background-color: #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
""") as demo:
|
| 47 |
|
| 48 |
-
gr.Markdown("<div id='title'>π LLM for Content Generation</div>")
|
| 49 |
|
| 50 |
with gr.Row():
|
| 51 |
-
with gr.Column():
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
with gr.Column():
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
gr.Markdown("<
|
| 61 |
|
| 62 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from rouge_score import rouge_scorer
|
| 4 |
|
| 5 |
+
# Load lightweight models
|
|
|
|
|
|
|
| 6 |
models = {
|
| 7 |
"π T5-Small": pipeline("text2text-generation", model="t5-small"),
|
| 8 |
"π DistilGPT2": pipeline("text-generation", model="distilgpt2"),
|
| 9 |
"π DistilBART": pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 10 |
}
|
| 11 |
|
| 12 |
+
# Summarization + Evaluation Function
|
| 13 |
+
def summarize_and_evaluate(original_text, reference_summary, model_choice):
|
| 14 |
+
if not original_text.strip():
|
| 15 |
+
return "β Please enter some text.", "", ""
|
| 16 |
+
|
| 17 |
+
summarizer = models[model_choice]
|
| 18 |
+
|
| 19 |
+
# Decide prompt & config
|
| 20 |
+
if model_choice == "π T5-Small":
|
| 21 |
+
prompt = "summarize: " + original_text
|
| 22 |
+
output = summarizer(prompt, max_length=100, min_length=30, do_sample=False)
|
| 23 |
+
summary = output[0]["generated_text"]
|
| 24 |
+
elif model_choice == "π DistilGPT2":
|
| 25 |
+
output = summarizer(original_text, max_length=100, num_return_sequences=1)
|
| 26 |
+
summary = output[0]["generated_text"]
|
| 27 |
+
else: # DistilBART
|
| 28 |
+
output = summarizer(original_text, max_length=100, min_length=30, do_sample=False)
|
| 29 |
+
summary = output[0]["summary_text"]
|
| 30 |
+
|
| 31 |
+
# ROUGE evaluation
|
| 32 |
+
if reference_summary.strip():
|
| 33 |
+
scorer = rouge_scorer.RougeScorer(['rouge1', 'rouge2', 'rougeL'], use_stemmer=True)
|
| 34 |
+
scores = scorer.score(reference_summary, summary)
|
| 35 |
+
rouge_str = f"""
|
| 36 |
+
π **ROUGE Scores vs Reference**:
|
| 37 |
+
- ROUGE-1: {scores['rouge1'].fmeasure:.2f}
|
| 38 |
+
- ROUGE-2: {scores['rouge2'].fmeasure:.2f}
|
| 39 |
+
- ROUGE-L: {scores['rougeL'].fmeasure:.2f}
|
| 40 |
+
"""
|
| 41 |
+
else:
|
| 42 |
+
rouge_str = "βΉοΈ No reference summary provided, so accuracy not calculated."
|
| 43 |
+
|
| 44 |
+
return summary, rouge_str, ""
|
| 45 |
+
|
| 46 |
+
# Gradio App UI
|
| 47 |
with gr.Blocks(css="""
|
| 48 |
body {
|
| 49 |
+
background-color: #fffdd0;
|
| 50 |
}
|
| 51 |
+
#main-title {
|
| 52 |
text-align: center;
|
| 53 |
+
font-size: 32px;
|
| 54 |
font-weight: bold;
|
| 55 |
color: #8b4513;
|
| 56 |
+
margin-top: 20px;
|
| 57 |
}
|
| 58 |
.output-box {
|
| 59 |
+
border: 2px dashed #caa74d;
|
| 60 |
+
padding: 12px;
|
| 61 |
border-radius: 10px;
|
| 62 |
+
background-color: #fffbe0;
|
| 63 |
+
}
|
| 64 |
+
#footer {
|
| 65 |
+
text-align: center;
|
| 66 |
+
font-size: 13px;
|
| 67 |
+
color: #777;
|
| 68 |
+
margin-top: 30px;
|
| 69 |
}
|
| 70 |
""") as demo:
|
| 71 |
|
| 72 |
+
gr.Markdown("<div id='main-title'>π LLM for Content Generation</div>")
|
| 73 |
|
| 74 |
with gr.Row():
|
| 75 |
+
with gr.Column(scale=1):
|
| 76 |
+
input_text = gr.Textbox(label="Original Text", lines=10, placeholder="Paste your content...")
|
| 77 |
+
reference_summary = gr.Textbox(label="Reference Summary (Optional)", lines=4, placeholder="For ROUGE score (optional)")
|
| 78 |
+
model_choice = gr.Radio(choices=list(models.keys()), value="π T5-Small", label="Select Model")
|
| 79 |
+
summarize_button = gr.Button("π Generate Content & Evaluate")
|
| 80 |
|
| 81 |
+
with gr.Column(scale=1):
|
| 82 |
+
output_summary = gr.Textbox(label="Generated Output", lines=10, elem_classes=["output-box"])
|
| 83 |
+
rouge_output = gr.Markdown()
|
| 84 |
|
| 85 |
+
summarize_button.click(
|
| 86 |
+
fn=summarize_and_evaluate,
|
| 87 |
+
inputs=[input_text, reference_summary, model_choice],
|
| 88 |
+
outputs=[output_summary, rouge_output, gr.Textbox(visible=False)]
|
| 89 |
+
)
|
| 90 |
|
| 91 |
+
gr.Markdown("<div id='footer'>π§ Powered by Transformers & Gradio | ROUGE auto-evaluation supported</div>")
|
| 92 |
|
| 93 |
demo.launch()
|