Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,87 +1,66 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
-
from threading import Thread
|
| 5 |
|
| 6 |
-
MODEL_ID = "microsoft/phi-
|
| 7 |
|
| 8 |
-
# Load model + tokenizer
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 10 |
-
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
MODEL_ID,
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
def chat(message, history):
|
| 19 |
-
# Format conversation
|
| 20 |
-
messages = []
|
| 21 |
-
|
| 22 |
-
for user, bot in history:
|
| 23 |
-
messages.append({"role": "user", "content": user})
|
| 24 |
-
messages.append({"role": "assistant", "content": bot})
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
prompt = tokenizer.apply_chat_template(
|
| 30 |
-
messages,
|
| 31 |
-
tokenize=False,
|
| 32 |
-
add_generation_prompt=True
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
tokenizer,
|
| 39 |
-
skip_prompt=True,
|
| 40 |
-
skip_special_tokens=True
|
| 41 |
-
)
|
| 42 |
|
| 43 |
-
|
| 44 |
**inputs,
|
| 45 |
-
|
| 46 |
-
max_new_tokens=150, # keep small for speed
|
| 47 |
temperature=0.7,
|
| 48 |
do_sample=True
|
| 49 |
)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
-
for new_token in streamer:
|
| 56 |
-
partial_text += new_token
|
| 57 |
-
yield partial_text
|
| 58 |
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
msg = gr.Textbox(placeholder="Type your message...")
|
| 66 |
-
clear = gr.Button("Clear")
|
| 67 |
|
| 68 |
-
|
| 69 |
-
return "", history + [[user_message, ""]]
|
| 70 |
|
| 71 |
-
|
| 72 |
-
user_message = history[-1][0]
|
| 73 |
|
| 74 |
-
|
| 75 |
-
for chunk in chat(user_message, history[:-1]):
|
| 76 |
-
bot_reply = chunk
|
| 77 |
-
history[-1][1] = bot_reply
|
| 78 |
-
yield history
|
| 79 |
|
| 80 |
-
msg.submit(user_input, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 81 |
-
bot_response, chatbot, chatbot
|
| 82 |
-
)
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
demo.queue()
|
| 87 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 4 |
|
| 5 |
+
MODEL_ID = "microsoft/phi-2" # lighter → IMPORTANT
|
| 6 |
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
|
|
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
MODEL_ID,
|
| 10 |
+
device_map="cpu",
|
| 11 |
+
torch_dtype=torch.float32,
|
| 12 |
+
low_cpu_mem_usage=True
|
| 13 |
)
|
| 14 |
|
| 15 |
+
torch.set_num_threads(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
def generate_reply(message, history):
|
| 18 |
+
# Convert history to prompt
|
| 19 |
+
prompt = ""
|
| 20 |
+
for msg in history:
|
| 21 |
+
if msg["role"] == "user":
|
| 22 |
+
prompt += f"User: {msg['content']}\n"
|
| 23 |
+
else:
|
| 24 |
+
prompt += f"Assistant: {msg['content']}\n"
|
| 25 |
|
| 26 |
+
prompt += f"User: {message}\nAssistant:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
outputs = model.generate(
|
| 31 |
**inputs,
|
| 32 |
+
max_new_tokens=80,
|
|
|
|
| 33 |
temperature=0.7,
|
| 34 |
do_sample=True
|
| 35 |
)
|
| 36 |
|
| 37 |
+
reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 38 |
+
reply = reply.split("Assistant:")[-1].strip()
|
| 39 |
|
| 40 |
+
return reply
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
+
def chat(message, history):
|
| 44 |
+
if history is None:
|
| 45 |
+
history = []
|
| 46 |
|
| 47 |
+
history.append({"role": "user", "content": message})
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
reply = generate_reply(message, history)
|
|
|
|
| 50 |
|
| 51 |
+
history.append({"role": "assistant", "content": reply})
|
|
|
|
| 52 |
|
| 53 |
+
return history
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
gr.Markdown("## ⚡ Fast Phi-2 Chatbot (HF Free Tier)")
|
| 58 |
+
|
| 59 |
+
chatbot = gr.Chatbot(type="messages")
|
| 60 |
+
msg = gr.Textbox(placeholder="Type something...")
|
| 61 |
+
clear = gr.Button("Clear")
|
| 62 |
+
|
| 63 |
+
msg.submit(chat, [msg, chatbot], chatbot)
|
| 64 |
+
clear.click(lambda: [], None, chatbot)
|
| 65 |
|
|
|
|
| 66 |
demo.launch()
|