ZENLLC commited on
Commit
4388810
·
verified ·
1 Parent(s): 9de1645

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -66
app.py CHANGED
@@ -2,101 +2,65 @@ import gradio as gr
2
  from transformers import pipeline
3
 
4
  # ===============================
5
- # Model dictionary (lazy loaded)
6
  # ===============================
7
- model_names = {
8
- "DistilGPT-2": "distilgpt2",
9
- "Bloom-560M": "bigscience/bloom-560m",
10
- "OPT-350M": "facebook/opt-350m",
11
- "Flan-T5-Base": "google/flan-t5-base",
12
- "Phi-2": "microsoft/phi-2"
 
 
13
  }
14
 
15
- loaded_models = {}
16
- summarizer = None # Flan-T5 for cleanup
17
-
18
- # ===============================
19
- # Lazy-load helper
20
- # ===============================
21
- def get_model(name):
22
- if name not in loaded_models:
23
- mdl = model_names[name]
24
- if "flan" in mdl.lower():
25
- loaded_models[name] = pipeline("text2text-generation", model=mdl)
26
- else:
27
- loaded_models[name] = pipeline("text-generation", model=mdl)
28
- return loaded_models[name]
29
-
30
- def get_summarizer():
31
- global summarizer
32
- if summarizer is None:
33
- summarizer = pipeline("text2text-generation", model="google/flan-t5-base")
34
- return summarizer
35
-
36
- # ===============================
37
- # Compare function
38
- # ===============================
39
- def compare_models(user_input, max_new_tokens=100, temperature=0.7):
40
- raw_outputs, clean_outputs = {}, {}
41
- for name in model_names.keys():
42
  try:
43
- generator = get_model(name)
44
- if generator.task == "text-generation":
45
- output = generator(
46
- user_input,
47
- max_new_tokens=max_new_tokens,
48
- temperature=temperature
49
- )[0]["generated_text"]
50
- else: # text2text-generation (Flan)
51
- output = generator(user_input, max_new_tokens=max_new_tokens)[0]["generated_text"]
52
-
53
- raw_outputs[name] = output
54
-
55
- # Summarize
56
- summary = get_summarizer()("Summarize this: " + output, max_new_tokens=60)[0]["generated_text"]
57
- clean_outputs[name] = summary
58
-
59
  except Exception as e:
60
- raw_outputs[name] = f"⚠️ Error: {str(e)}"
61
- clean_outputs[name] = "N/A"
62
-
63
- return [raw_outputs[m] for m in model_names.keys()], [clean_outputs[m] for m in model_names.keys()]
64
 
65
  # ===============================
66
  # Gradio UI
67
  # ===============================
68
  with gr.Blocks(css="style.css") as demo:
69
  gr.Markdown("## 🤖 Open-Source Model Comparator\n"
70
- "Compare outputs from open-source LLMs side by side.\n"
71
- "Raw output + a cleaned summary from Flan-T5.")
72
 
73
  with gr.Row():
74
- user_input = gr.Textbox(label="Your prompt", placeholder="Try: 'Explain quantum computing in simple terms'", lines=2)
75
  generate_btn = gr.Button("Generate", variant="primary")
76
 
77
  with gr.Row():
78
  max_tokens = gr.Slider(20, 200, value=100, step=10, label="Max new tokens")
79
  temp = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Creativity (temperature)")
 
80
 
81
- gr.Markdown("### 🔎 Raw Outputs")
82
- with gr.Row():
83
- raw_boxes = [gr.Textbox(label=name, elem_classes="output-box", interactive=False) for name in model_names.keys()]
84
-
85
- gr.Markdown("### ✨ Cleaned Summaries (Flan-T5)")
86
  with gr.Row():
87
- clean_boxes = [gr.Textbox(label=f"{name} (Summary)", elem_classes="output-box", interactive=False) for name in model_names.keys()]
88
 
89
  examples = [
90
  ["Explain quantum computing in simple terms."],
91
  ["Write a haiku about autumn leaves."],
92
  ["What are the pros and cons of nuclear energy?"],
93
  ["Describe a futuristic city in the year 2200."],
94
- ["Write a funny short story about a robot learning to cook."]
95
  ]
96
  gr.Examples(examples=examples, inputs=[user_input])
97
 
98
- generate_btn.click(compare_models, inputs=[user_input, max_tokens, temp], outputs=raw_boxes + clean_boxes)
99
- user_input.submit(compare_models, inputs=[user_input, max_tokens, temp], outputs=raw_boxes + clean_boxes)
100
 
101
  if __name__ == "__main__":
102
  demo.launch()
 
2
  from transformers import pipeline
3
 
4
  # ===============================
5
+ # Load only text-generation models (simpler, stable)
6
  # ===============================
7
+ models = {
8
+ "DistilGPT-2": pipeline("text-generation", model="distilgpt2"),
9
+ "GPT2 (Small)": pipeline("text-generation", model="gpt2"),
10
+ "DialoGPT-small": pipeline("text-generation", model="microsoft/DialoGPT-small"),
11
+ "OPT-350M": pipeline("text-generation", model="facebook/opt-350m"),
12
+ "Bloom-560M": pipeline("text-generation", model="bigscience/bloom-560m"),
13
+ "GPT-Neo-125M": pipeline("text-generation", model="EleutherAI/gpt-neo-125M"),
14
+ "Falcon-RW-1B": pipeline("text-generation", model="tiiuae/falcon-rw-1b"),
15
  }
16
 
17
+ def compare_models(user_input, max_new_tokens=100, temperature=0.7, top_p=0.95):
18
+ results = {}
19
+ for name, generator in models.items():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  try:
21
+ output = generator(
22
+ user_input,
23
+ max_new_tokens=max_new_tokens,
24
+ temperature=temperature,
25
+ top_p=top_p,
26
+ do_sample=True
27
+ )[0]["generated_text"]
28
+ results[name] = output
 
 
 
 
 
 
 
 
29
  except Exception as e:
30
+ results[name] = f"⚠️ Error: {str(e)}"
31
+ return [results[m] for m in models.keys()]
 
 
32
 
33
  # ===============================
34
  # Gradio UI
35
  # ===============================
36
  with gr.Blocks(css="style.css") as demo:
37
  gr.Markdown("## 🤖 Open-Source Model Comparator\n"
38
+ "Compare outputs from multiple open-source LLMs side by side.\n"
39
+ "These are raw, unfiltered outputs from Hugging Face models.")
40
 
41
  with gr.Row():
42
+ user_input = gr.Textbox(label="Your prompt", placeholder="Ask something like 'Write a short poem about the stars'...", lines=2)
43
  generate_btn = gr.Button("Generate", variant="primary")
44
 
45
  with gr.Row():
46
  max_tokens = gr.Slider(20, 200, value=100, step=10, label="Max new tokens")
47
  temp = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Creativity (temperature)")
48
+ topp = gr.Slider(0.5, 1.0, value=0.95, step=0.05, label="Nucleus sampling (top_p)")
49
 
 
 
 
 
 
50
  with gr.Row():
51
+ outputs = [gr.Textbox(label=name, elem_classes="output-box", interactive=False) for name in models.keys()]
52
 
53
  examples = [
54
  ["Explain quantum computing in simple terms."],
55
  ["Write a haiku about autumn leaves."],
56
  ["What are the pros and cons of nuclear energy?"],
57
  ["Describe a futuristic city in the year 2200."],
58
+ ["Write a funny short story about a robot learning to cook."],
59
  ]
60
  gr.Examples(examples=examples, inputs=[user_input])
61
 
62
+ generate_btn.click(compare_models, inputs=[user_input, max_tokens, temp, topp], outputs=outputs)
63
+ user_input.submit(compare_models, inputs=[user_input, max_tokens, temp, topp], outputs=outputs)
64
 
65
  if __name__ == "__main__":
66
  demo.launch()