Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import torch
|
2 |
-
import gradio as gr
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
4 |
|
5 |
model_id = "cody82/unitrip"
|
6 |
|
@@ -11,28 +11,37 @@ model.to(device)
|
|
11 |
|
12 |
system_message = "Ты — умный помощник по Университету Иннополис."
|
13 |
|
14 |
-
def respond(
|
15 |
if history is None:
|
16 |
history = []
|
17 |
-
|
18 |
-
prompt =
|
|
|
|
|
|
|
19 |
|
20 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
21 |
|
22 |
with torch.no_grad():
|
23 |
outputs = model.generate(
|
24 |
**inputs,
|
25 |
-
max_new_tokens=
|
26 |
-
do_sample=False,
|
27 |
pad_token_id=tokenizer.eos_token_id,
|
28 |
eos_token_id=tokenizer.eos_token_id,
|
29 |
-
|
30 |
)
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
history.append((
|
35 |
return history, history
|
36 |
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
|
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import gradio as gr
|
4 |
|
5 |
model_id = "cody82/unitrip"
|
6 |
|
|
|
11 |
|
12 |
system_message = "Ты — умный помощник по Университету Иннополис."
|
13 |
|
14 |
+
def respond(user_message, history):
|
15 |
if history is None:
|
16 |
history = []
|
17 |
+
# Формируем полный контекст (если нужно)
|
18 |
+
prompt = system_message + "\n"
|
19 |
+
for user_text, bot_text in history:
|
20 |
+
prompt += f"User: {user_text}\nAssistant: {bot_text}\n"
|
21 |
+
prompt += f"User: {user_message}\nAssistant:"
|
22 |
|
23 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
24 |
|
25 |
with torch.no_grad():
|
26 |
outputs = model.generate(
|
27 |
**inputs,
|
28 |
+
max_new_tokens=150,
|
|
|
29 |
pad_token_id=tokenizer.eos_token_id,
|
30 |
eos_token_id=tokenizer.eos_token_id,
|
31 |
+
do_sample=False,
|
32 |
)
|
33 |
|
34 |
+
generated_text = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True).strip()
|
35 |
+
|
36 |
+
history.append((user_message, generated_text))
|
37 |
return history, history
|
38 |
|
39 |
+
with gr.Blocks() as demo:
|
40 |
+
chatbot = gr.Chatbot()
|
41 |
+
message = gr.Textbox(placeholder="Введите вопрос...")
|
42 |
+
state = gr.State([]) # История сообщений
|
43 |
+
|
44 |
+
message.submit(respond, inputs=[message, state], outputs=[chatbot, state])
|
45 |
+
message.submit(lambda: "", None, message) # Очистить поле ввода после отправки
|
46 |
+
|
47 |
+
demo.launch(share=True)
|