Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 4 |
+
from threading import Thread
|
| 5 |
+
|
| 6 |
+
MODEL_ID = "microsoft/phi-3-mini-4k-instruct"
|
| 7 |
+
|
| 8 |
+
# Load model + tokenizer
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 10 |
+
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
MODEL_ID,
|
| 13 |
+
torch_dtype=torch.float32, # safer for CPU
|
| 14 |
+
device_map="auto"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Chat function with streaming
|
| 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 |
+
messages.append({"role": "user", "content": message})
|
| 27 |
+
|
| 28 |
+
# Apply chat template
|
| 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 |
+
streamer = TextIteratorStreamer(
|
| 38 |
+
tokenizer,
|
| 39 |
+
skip_prompt=True,
|
| 40 |
+
skip_special_tokens=True
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
generation_kwargs = dict(
|
| 44 |
+
**inputs,
|
| 45 |
+
streamer=streamer,
|
| 46 |
+
max_new_tokens=150, # keep small for speed
|
| 47 |
+
temperature=0.7,
|
| 48 |
+
do_sample=True
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 52 |
+
thread.start()
|
| 53 |
+
|
| 54 |
+
partial_text = ""
|
| 55 |
+
for new_token in streamer:
|
| 56 |
+
partial_text += new_token
|
| 57 |
+
yield partial_text
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# Gradio UI
|
| 61 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 62 |
+
gr.Markdown("## ⚡ Phi-3 Mini Chatbot (Fast HF Space)")
|
| 63 |
+
|
| 64 |
+
chatbot = gr.Chatbot()
|
| 65 |
+
msg = gr.Textbox(placeholder="Type your message...")
|
| 66 |
+
clear = gr.Button("Clear")
|
| 67 |
+
|
| 68 |
+
def user_input(user_message, history):
|
| 69 |
+
return "", history + [[user_message, ""]]
|
| 70 |
+
|
| 71 |
+
def bot_response(history):
|
| 72 |
+
user_message = history[-1][0]
|
| 73 |
+
|
| 74 |
+
bot_reply = ""
|
| 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 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 85 |
+
|
| 86 |
+
demo.queue()
|
| 87 |
+
demo.launch()
|