Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,35 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
inputs = tokenizer(input_text, return_tensors="pt").to("cpu")
|
| 15 |
-
output = model.generate(**inputs, max_new_tokens=200, do_sample=True, temperature=0.7)
|
| 16 |
-
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 17 |
-
return response.split("user")[-1].strip()
|
| 18 |
|
| 19 |
-
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
model_name = "microsoft/Phi-4-mini-instruct"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float32)
|
| 8 |
+
|
| 9 |
+
def chat(user_input, history=[]):
|
| 10 |
+
messages = [{"role": "system", "content": "Ты полезный ассистент."}]
|
| 11 |
+
for i, (q, a) in enumerate(history):
|
| 12 |
+
messages.append({"role": "user", "content": q})
|
| 13 |
+
messages.append({"role": "assistant", "content": a})
|
| 14 |
+
messages.append({"role": "user", "content": user_input})
|
| 15 |
+
|
| 16 |
+
inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
|
| 17 |
+
outputs = model.generate(inputs, max_new_tokens=256, do_sample=True, temperature=0.7)
|
| 18 |
+
reply = tokenizer.decode(outputs[0], skip_special_tokens=True).split("assistant")[-1].strip()
|
| 19 |
+
|
| 20 |
+
history.append((user_input, reply))
|
| 21 |
+
return reply, history
|
| 22 |
|
| 23 |
+
with gr.Blocks() as demo:
|
| 24 |
+
gr.Markdown("# 🤖 Флэри чат (на Phi-4-mini)")
|
| 25 |
+
chatbot = gr.Chatbot()
|
| 26 |
+
msg = gr.Textbox(label="Ваш вопрос")
|
| 27 |
+
state = gr.State([])
|
| 28 |
|
| 29 |
+
def respond(message, history):
|
| 30 |
+
reply, history = chat(message, history)
|
| 31 |
+
return chatbot.update(value=history), history
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
msg.submit(respond, [msg, state], [chatbot, state])
|
| 34 |
|
| 35 |
+
demo.launch()
|