Update app.py
Browse files
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 |
-
|
| 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 |
-
#
|
| 40 |
prompt = ""
|
| 41 |
for msg in history:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 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 |
-
|
| 64 |
for new_text in streamer:
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
def create_demo():
|
| 70 |
with gr.Blocks(title="LiquidAI Chat Playground") as demo:
|
| 71 |
-
gr.Markdown("## 💧 LiquidAI Chat
|
| 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",
|