FlameF0X commited on
Commit
13f0e11
·
verified ·
1 Parent(s): 0d4961f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -24,8 +24,7 @@ def load_model(model_key):
24
  device = "cuda" if torch.cuda.is_available() else "cpu"
25
  model = AutoModelForCausalLM.from_pretrained(
26
  model_name,
27
- torch_dtype=torch.float16 if device == "cuda" else torch.float32,
28
- device_map=None, # Disable meta/offload shenanigans
29
  ).to(device)
30
 
31
  model_cache[model_key] = (tokenizer, model)
@@ -36,13 +35,12 @@ def chat_with_model(message, history, model_choice):
36
  tokenizer, model = load_model(model_choice)
37
  device = model.device
38
 
39
- # Convert the Gradio message history into a string prompt
40
  prompt = ""
41
  for msg in history:
42
- if msg["role"] == "user":
43
- prompt += f"User: {msg['content']}\n"
44
- elif msg["role"] == "assistant":
45
- prompt += f"Assistant: {msg['content']}\n"
46
  prompt += f"User: {message}\nAssistant:"
47
 
48
  streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
@@ -60,15 +58,19 @@ def chat_with_model(message, history, model_choice):
60
  thread = Thread(target=model.generate, kwargs=generation_kwargs)
61
  thread.start()
62
 
63
- partial = ""
64
  for new_text in streamer:
65
- partial += new_text
66
- yield partial
 
 
 
 
67
 
68
 
69
  def create_demo():
70
  with gr.Blocks(title="LiquidAI Chat Playground") as demo:
71
- gr.Markdown("## 💧 LiquidAI Chat Interface")
72
 
73
  model_choice = gr.Dropdown(
74
  label="Select Model",
 
24
  device = "cuda" if torch.cuda.is_available() else "cpu"
25
  model = AutoModelForCausalLM.from_pretrained(
26
  model_name,
27
+ dtype=torch.float16 if device == "cuda" else torch.float32,
 
28
  ).to(device)
29
 
30
  model_cache[model_key] = (tokenizer, model)
 
35
  tokenizer, model = load_model(model_choice)
36
  device = model.device
37
 
38
+ # Build the prompt from previous conversation
39
  prompt = ""
40
  for msg in history:
41
+ role = msg["role"]
42
+ content = msg["content"]
43
+ prompt += f"{role.capitalize()}: {content}\n"
 
44
  prompt += f"User: {message}\nAssistant:"
45
 
46
  streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
 
58
  thread = Thread(target=model.generate, kwargs=generation_kwargs)
59
  thread.start()
60
 
61
+ partial_text = ""
62
  for new_text in streamer:
63
+ partial_text += new_text
64
+ # Yield full chat including this updated assistant message
65
+ yield history + [
66
+ {"role": "user", "content": message},
67
+ {"role": "assistant", "content": partial_text},
68
+ ]
69
 
70
 
71
  def create_demo():
72
  with gr.Blocks(title="LiquidAI Chat Playground") as demo:
73
+ gr.Markdown("## 💧 LiquidAI Chat Playground")
74
 
75
  model_choice = gr.Dropdown(
76
  label="Select Model",