Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,106 +1,33 @@
|
|
1 |
-
import os
|
2 |
-
os.system('pip install dashscope')
|
3 |
import gradio as gr
|
4 |
-
from
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
History = List[Tuple[str, str]]
|
17 |
-
Messages = List[Dict[str, str]]
|
18 |
-
|
19 |
-
def clear_session() -> History:
|
20 |
-
return '', []
|
21 |
-
|
22 |
-
def modify_system_session(system: str) -> str:
|
23 |
-
if system is None or len(system) == 0:
|
24 |
-
system = default_system
|
25 |
-
return system, system, []
|
26 |
-
|
27 |
-
def history_to_messages(history: History, system: str) -> Messages:
|
28 |
-
messages = [{'role': Role.SYSTEM, 'content': system}]
|
29 |
-
for h in history:
|
30 |
-
messages.append({'role': Role.USER, 'content': h[0]})
|
31 |
-
messages.append({'role': Role.ASSISTANT, 'content': h[1]})
|
32 |
-
return messages
|
33 |
-
|
34 |
-
|
35 |
-
def messages_to_history(messages: Messages) -> Tuple[str, History]:
|
36 |
-
assert messages[0]['role'] == Role.SYSTEM
|
37 |
-
system = messages[0]['content']
|
38 |
-
history = []
|
39 |
-
for q, r in zip(messages[1::2], messages[2::2]):
|
40 |
-
history.append([q['content'], r['content']])
|
41 |
-
return system, history
|
42 |
-
|
43 |
-
|
44 |
-
def model_chat(query: Optional[str], history: Optional[History], system: str
|
45 |
-
) -> Tuple[str, str, History]:
|
46 |
-
if query is None:
|
47 |
-
query = ''
|
48 |
-
if history is None:
|
49 |
-
history = []
|
50 |
-
messages = history_to_messages(history, system)
|
51 |
-
messages.append({'role': Role.USER, 'content': query})
|
52 |
-
gen = Generation.call(
|
53 |
-
model='google/gemma-1.1-7b-it',
|
54 |
-
messages=messages,
|
55 |
-
result_format='message',
|
56 |
-
stream=True
|
57 |
-
)
|
58 |
-
for response in gen:
|
59 |
-
if response.status_code == HTTPStatus.OK:
|
60 |
-
role = response.output.choices[0].message.role
|
61 |
-
response = response.output.choices[0].message.content
|
62 |
-
system, history = messages_to_history(messages + [{'role': role, 'content': response}])
|
63 |
-
yield '', history, system
|
64 |
-
else:
|
65 |
-
raise ValueError('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
|
66 |
-
response.request_id, response.status_code,
|
67 |
-
response.code, response.message
|
68 |
-
))
|
69 |
-
|
70 |
-
|
71 |
-
with gr.Blocks() as demo:
|
72 |
-
gr.Markdown("""<center><font size=8>Qwen2-72B-instruct Chat👾</center>""")
|
73 |
-
|
74 |
-
with gr.Row():
|
75 |
-
with gr.Column(scale=3):
|
76 |
-
system_input = gr.Textbox(value=default_system, lines=1, label='System')
|
77 |
-
with gr.Column(scale=1):
|
78 |
-
modify_system = gr.Button("🛠️ Set system prompt and clear history", scale=2)
|
79 |
-
system_state = gr.Textbox(value=default_system, visible=False)
|
80 |
-
chatbot = gr.Chatbot(label='qwen2-72B-instruct')
|
81 |
-
textbox = gr.Textbox(lines=1, label='Input')
|
82 |
-
|
83 |
-
with gr.Row():
|
84 |
-
clear_history = gr.Button("🧹 Clear history")
|
85 |
-
sumbit = gr.Button("🚀 Send")
|
86 |
-
|
87 |
-
textbox.submit(model_chat,
|
88 |
-
inputs=[textbox, chatbot, system_state],
|
89 |
-
outputs=[textbox, chatbot, system_input],
|
90 |
-
concurrency_limit = 40)
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Загрузка модели и токенизатора
|
6 |
+
model_name = "google/gemma-1.1-7b-it"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# Функция для обработки сообщений
|
11 |
+
def chat_with_model(input_text):
|
12 |
+
# Токенизация входного текста
|
13 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Генерация ответа
|
16 |
+
with torch.no_grad():
|
17 |
+
outputs = model.generate(**inputs, max_length=512, pad_token_id=tokenizer.eos_token_id)
|
18 |
+
|
19 |
+
# Декодирование и возвращение ответа
|
20 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
21 |
+
return response
|
22 |
+
|
23 |
+
# Создание интерфейса Gradio
|
24 |
+
interface = gr.Interface(
|
25 |
+
fn=chat_with_model,
|
26 |
+
inputs="text",
|
27 |
+
outputs="text",
|
28 |
+
title="Gemma Chatbot",
|
29 |
+
description="Чат-бот на основе модели google/gemma-1.1-7b-it"
|
30 |
+
)
|
31 |
+
|
32 |
+
# Запуск интерфейса
|
33 |
+
interface.launch()
|