Mehak-Mazhar commited on
Commit
aa7d2ac
Β·
verified Β·
1 Parent(s): 3f7cae5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -38
app.py CHANGED
@@ -1,64 +1,62 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load 5 reasonably sized models (avoid big ones like Falcon-7B)
5
  models = {
6
- "🧠 FLAN-T5 (large)": pipeline("text2text-generation", model="google/flan-t5-large"),
7
- "✍️ GPT-Neo (1.3B)": pipeline("text-generation", model="EleutherAI/gpt-neo-1.3B"),
8
- "πŸ“– BART": pipeline("text2text-generation", model="facebook/bart-large-cnn"),
9
- "πŸ“š Palmyra": pipeline("text-generation", model="Writer/palmyra-base"),
10
- "πŸ“ T5-Small": pipeline("text2text-generation", model="t5-small")
11
  }
12
 
13
- def generate_text(prompt, model_name):
14
- if not prompt.strip():
15
- return "⚠️ Please enter a topic or idea."
16
-
17
- generator = models[model_name]
18
- if "text2text" in generator.task:
19
- result = generator(prompt, max_length=200)[0]["generated_text"]
20
- else:
21
- result = generator(prompt, max_length=200, do_sample=True, temperature=0.7)[0]["generated_text"]
22
-
23
- return result
24
-
 
 
 
 
25
  with gr.Blocks(css="""
26
  body {
27
- background-color: #fffde7; /* Light lemon */
28
  }
29
  #title {
30
- color: #6b4f26; /* Brown */
31
  text-align: center;
32
- font-size: 40px;
33
  font-weight: bold;
34
- margin-top: 20px;
35
- }
36
- #subtitle {
37
- text-align: center;
38
- color: #8d6e63;
39
- font-size: 16px;
40
- margin-bottom: 30px;
41
  }
42
  .output-box {
43
- background-color: #f9fbe7;
44
- border: 2px dashed #c8b900;
45
- padding: 15px;
46
  border-radius: 10px;
 
47
  }
48
  """) as demo:
49
 
50
- gr.Markdown("<div id='title'>πŸͺ„ LLM for Content Generation</div>")
51
- gr.Markdown("<div id='subtitle'>Enter a topic and see how different LLMs generate creative content.</div>")
52
 
53
  with gr.Row():
54
  with gr.Column():
55
- user_input = gr.Textbox(label="Enter your topic or idea", lines=3, placeholder="e.g., The future of AI in education")
56
- model_choice = gr.Radio(choices=list(models.keys()), label="Select a model", value="🧠 FLAN-T5 (large)")
57
- gen_btn = gr.Button("✨ Generate Content")
58
 
59
  with gr.Column():
60
- output = gr.Textbox(label="Generated Content", lines=12, elem_classes=["output-box"])
 
 
61
 
62
- gen_btn.click(fn=generate_text, inputs=[user_input, model_choice], outputs=output)
63
 
64
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Lightweight models that work within 16GB RAM
5
  models = {
6
+ "πŸ“˜ T5-Small": pipeline("text2text-generation", model="t5-small"),
7
+ "πŸ“— DistilGPT2": pipeline("text-generation", model="distilgpt2"),
8
+ "πŸ“• DistilBART": pipeline("text2text-generation", model="sshleifer/distilbart-cnn-12-6"),
9
+ "πŸ“™ Pegasus-Tiny": pipeline("text2text-generation", model="google/pegasus-small"),
10
+ "πŸ“’ FLAN-T5-Base": pipeline("text2text-generation", model="google/flan-t5-base")
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()