Tobi196 commited on
Commit
ec4f8f3
·
verified ·
1 Parent(s): 8b588d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -18
app.py CHANGED
@@ -3,10 +3,10 @@ import gradio as gr
3
  import torch
4
  from transformers import pipeline, BitsAndBytesConfig
5
 
6
- # 1. Setup Auth
7
  token = os.environ.get("HF_TOKEN")
8
 
9
- # 2. Configuration for 4-bit Quantization (Saves RAM)
10
  bnb_config = BitsAndBytesConfig(
11
  load_in_4bit=True,
12
  bnb_4bit_compute_dtype=torch.bfloat16
@@ -14,32 +14,31 @@ bnb_config = BitsAndBytesConfig(
14
 
15
  print("Loading 3 models into RAM... please wait about 5 minutes.")
16
 
17
- # Load Gemma 3 1B (Uses ~1GB RAM)
18
  pipe_1b = pipeline("text-generation", model="google/gemma-3-1b-it",
19
  device_map="cpu", token=token, model_kwargs={"quantization_config": bnb_config})
20
 
21
- # Load Gemma 3 4B (Uses ~3.5GB RAM in 4-bit)
22
  pipe_4b = pipeline("text-generation", model="google/gemma-3-4b-it",
23
  device_map="cpu", token=token, model_kwargs={"quantization_config": bnb_config})
24
 
25
- # Load Phi-4 Mini (Uses ~2.5GB RAM in 4-bit)
26
  pipe_phi = pipeline("text-generation", model="microsoft/Phi-4-mini-instruct",
27
  device_map="cpu", token=token, model_kwargs={"quantization_config": bnb_config})
28
 
29
  def battle(message):
30
- results = []
31
- # Format message for Instruct models
32
- msgs = [{"role": "user", "content": message}]
33
-
34
- # Generate from all three
35
- out_1b = pipe_1b(msgs, max_new_tokens=128)[0]['generated_text'][-1]['content']
36
- out_4b = pipe_4b(msgs, max_new_tokens=128)[0]['generated_text'][-1]['content']
37
- out_phi = pipe_phi(msgs, max_new_tokens=128)[0]['generated_text'][-1]['content']
38
-
39
- return out_1b, out_4b, out_phi
 
40
 
41
- # 3. UI Layout
42
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
43
  gr.Markdown("# ⚔️ Triple Model Local Arena")
44
  gr.Markdown("Running locally on 16GB CPU RAM via 4-bit Quantization.")
45
 
@@ -61,4 +60,5 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
61
  btn.click(battle, inputs=txt, outputs=[res_1, res_2, res_3])
62
 
63
  if __name__ == "__main__":
64
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
3
  import torch
4
  from transformers import pipeline, BitsAndBytesConfig
5
 
6
+ # 1. Setup Auth (No changes)
7
  token = os.environ.get("HF_TOKEN")
8
 
9
+ # 2. Configuration (No changes)
10
  bnb_config = BitsAndBytesConfig(
11
  load_in_4bit=True,
12
  bnb_4bit_compute_dtype=torch.bfloat16
 
14
 
15
  print("Loading 3 models into RAM... please wait about 5 minutes.")
16
 
17
+ # Load models (No changes)
18
  pipe_1b = pipeline("text-generation", model="google/gemma-3-1b-it",
19
  device_map="cpu", token=token, model_kwargs={"quantization_config": bnb_config})
20
 
 
21
  pipe_4b = pipeline("text-generation", model="google/gemma-3-4b-it",
22
  device_map="cpu", token=token, model_kwargs={"quantization_config": bnb_config})
23
 
 
24
  pipe_phi = pipeline("text-generation", model="microsoft/Phi-4-mini-instruct",
25
  device_map="cpu", token=token, model_kwargs={"quantization_config": bnb_config})
26
 
27
  def battle(message):
28
+ try:
29
+ msgs = [{"role": "user", "content": message}]
30
+
31
+ # Defensive extraction of text
32
+ out_1b = pipe_1b(msgs, max_new_tokens=128)[0]['generated_text'][-1]['content']
33
+ out_4b = pipe_4b(msgs, max_new_tokens=128)[0]['generated_text'][-1]['content']
34
+ out_phi = pipe_phi(msgs, max_new_tokens=128)[0]['generated_text'][-1]['content']
35
+
36
+ return out_1b, out_4b, out_phi
37
+ except Exception as e:
38
+ return f"Error: {str(e)}", "Error", "Error"
39
 
40
+ # 3. UI Layout (UPDATED FOR GRADIO 6.0)
41
+ with gr.Blocks() as demo: # Removed theme=gr.themes.Soft() from here
42
  gr.Markdown("# ⚔️ Triple Model Local Arena")
43
  gr.Markdown("Running locally on 16GB CPU RAM via 4-bit Quantization.")
44
 
 
60
  btn.click(battle, inputs=txt, outputs=[res_1, res_2, res_3])
61
 
62
  if __name__ == "__main__":
63
+ # Theme is now passed here in Gradio 6.0
64
+ demo.launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft())