ar0551 commited on
Commit
45bdf16
·
verified ·
1 Parent(s): 9d4fcdd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -6
app.py CHANGED
@@ -5,18 +5,17 @@ import spaces
5
 
6
  model_id = "microsoft/phi-2"
7
 
8
- # 🌟 Auto-detect device (CPU/GPU)
9
- device = "cuda"
10
- precision = torch.float16
11
 
12
- # Load model and tokenizer (CPU + float32)
13
  tokenizer = AutoTokenizer.from_pretrained(model_id)
14
  model = AutoModelForCausalLM.from_pretrained(
15
  model_id,
16
  torch_dtype=precision
17
  ).to(device)
18
 
19
-
20
  # Chat function
21
  @spaces.GPU
22
  def chat_with_bot(message, history):
@@ -38,10 +37,11 @@ def chat_with_bot(message, history):
38
 
39
  decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
40
  response = decoded[len(prompt):].strip().split("\n")[0]
 
41
  history.append([message, response])
42
  return response, history
43
 
44
- # gr.ChatInterface expects fn(message, history) → (response, updated_history)
45
  gr.ChatInterface(
46
  fn=chat_with_bot,
47
  title="Phi-2 Chatbot (CPU-friendly)",
 
5
 
6
  model_id = "microsoft/phi-2"
7
 
8
+ # Auto-detect device (but since your space is ZeroGPU, this should default to CPU)
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ precision = torch.float16 if device == "cuda" else torch.float32
11
 
12
+ # Load model and tokenizer
13
  tokenizer = AutoTokenizer.from_pretrained(model_id)
14
  model = AutoModelForCausalLM.from_pretrained(
15
  model_id,
16
  torch_dtype=precision
17
  ).to(device)
18
 
 
19
  # Chat function
20
  @spaces.GPU
21
  def chat_with_bot(message, history):
 
37
 
38
  decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
39
  response = decoded[len(prompt):].strip().split("\n")[0]
40
+ # IMPORTANT: Append message pair as a list rather than a tuple.
41
  history.append([message, response])
42
  return response, history
43
 
44
+ # Gradio ChatInterface expects fn(message, history) → (response, updated_history)
45
  gr.ChatInterface(
46
  fn=chat_with_bot,
47
  title="Phi-2 Chatbot (CPU-friendly)",