Mehak-Mazhar commited on
Commit
4a85a9d
Β·
verified Β·
1 Parent(s): 2e9a1ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -34
app.py CHANGED
@@ -1,62 +1,93 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- # Lightweight models that work within 16GB RAM
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
- # Content generation function
14
- def generate_content(topic):
15
- outputs = {}
16
- for name, model in models.items():
17
- try:
18
- if "text-generation" in str(model):
19
- result = model(topic, max_length=100, do_sample=False)[0]['generated_text']
20
- else:
21
- result = model(topic, max_length=100, do_sample=False)[0]['generated_text']
22
- outputs[name] = result
23
- except Exception as e:
24
- outputs[name] = f"Error: {str(e)}"
25
- return [outputs[name] for name in models.keys()]
26
-
27
- # Gradio UI
28
- gr.Theme.set("default")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  with gr.Blocks(css="""
30
  body {
31
- background-color: #fffde7;
32
  }
33
- #title {
34
  text-align: center;
35
- font-size: 34px;
36
  font-weight: bold;
37
  color: #8b4513;
38
- margin-bottom: 20px;
39
  }
40
  .output-box {
41
- border: 2px solid #d4af37;
42
- padding: 10px;
43
  border-radius: 10px;
44
- background-color: #fff8dc;
 
 
 
 
 
 
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
- topic = gr.Textbox(label="Enter your topic", placeholder="e.g. Climate Change Impact on Economy")
53
- generate_button = gr.Button("Generate Content ✨")
 
 
54
 
55
- with gr.Column():
56
- outputs = [gr.Textbox(label=model, lines=6, elem_classes=["output-box"]) for model in models.keys()]
 
57
 
58
- generate_button.click(fn=generate_content, inputs=[topic], outputs=outputs)
 
 
 
 
59
 
60
- gr.Markdown("<center>πŸ’‘ Compare outputs from 5 different lightweight LLMs. Fast, efficient, and creative!</center>")
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()