Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,23 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 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 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
state = gr.State([])
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
|
|
| 3 |
|
| 4 |
+
# Загружаем модель
|
| 5 |
+
model_id = "microsoft/Phi-4-mini-instruct"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 8 |
|
| 9 |
+
# Создаем пайплайн
|
| 10 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Функция генерации текста
|
| 13 |
+
def chat(input_text):
|
| 14 |
+
result = pipe(input_text)[0]['generated_text']
|
| 15 |
+
return result
|
|
|
|
| 16 |
|
| 17 |
+
# Интерфейс Gradio
|
| 18 |
+
iface = gr.Interface(fn=chat,
|
| 19 |
+
inputs=gr.Textbox(lines=4, placeholder="Спроси меня что-нибудь..."),
|
| 20 |
+
outputs="text",
|
| 21 |
+
title="💬 Chat с Phi-4-mini-instruct")
|
| 22 |
|
| 23 |
+
iface.launch()
|
|
|
|
|
|