Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,21 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
|
|
|
|
4 |
|
5 |
def generate(message, chat_history, model, system_prompt):
|
6 |
-
"""
|
7 |
-
|
|
|
8 |
|
9 |
-
#
|
|
|
|
|
|
|
|
|
10 |
chat_history.append((message, response))
|
11 |
-
|
12 |
return chat_history, ""
|
13 |
|
14 |
DEFAULT_SYSTEM_PROMPT = """
|
@@ -24,13 +31,13 @@ with gr.Blocks() as demo:
|
|
24 |
gr.Markdown("# Custom Chat Interface")
|
25 |
|
26 |
with gr.Row():
|
27 |
-
|
28 |
|
29 |
system_prompt = gr.Textbox(value=DEFAULT_SYSTEM_PROMPT, lines=5, label="System Prompt")
|
30 |
chatbot = gr.Chatbot(label="Chat")
|
31 |
msg = gr.Textbox(label="Type your message here...", placeholder="Enter your message...")
|
32 |
|
33 |
-
msg.submit(generate, inputs=[msg, chatbot,
|
34 |
gr.Button("Clear Chat").click(clear_chat, inputs=None, outputs=[chatbot, msg])
|
35 |
|
36 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
|
4 |
+
# Подгружаем модель с использованием transformers
|
5 |
+
model_name = "Mixtral-8x7B-Instruct-v0.1"
|
6 |
+
generator = pipeline('text-generation', model=model_name, tokenizer=model_name)
|
7 |
|
8 |
def generate(message, chat_history, model, system_prompt):
|
9 |
+
"""Generates a response using the model."""
|
10 |
+
# Объединяем системный промпт и сообщение пользователя для контекста
|
11 |
+
prompt = system_prompt + "\n" + message
|
12 |
|
13 |
+
# Генерация ответа моделью
|
14 |
+
responses = generator(prompt, max_length=150, num_return_sequences=1)
|
15 |
+
response = responses[0]['generated_text'].split(prompt)[1] # Извлечение только сгенерированного ответа
|
16 |
+
|
17 |
+
# Обновление истории чата
|
18 |
chat_history.append((message, response))
|
|
|
19 |
return chat_history, ""
|
20 |
|
21 |
DEFAULT_SYSTEM_PROMPT = """
|
|
|
31 |
gr.Markdown("# Custom Chat Interface")
|
32 |
|
33 |
with gr.Row():
|
34 |
+
model_dropdown = gr.Dropdown(choices=[model_name], label="Select Model", value=model_name)
|
35 |
|
36 |
system_prompt = gr.Textbox(value=DEFAULT_SYSTEM_PROMPT, lines=5, label="System Prompt")
|
37 |
chatbot = gr.Chatbot(label="Chat")
|
38 |
msg = gr.Textbox(label="Type your message here...", placeholder="Enter your message...")
|
39 |
|
40 |
+
msg.submit(generate, inputs=[msg, chatbot, model_dropdown, system_prompt], outputs=[chatbot, msg])
|
41 |
gr.Button("Clear Chat").click(clear_chat, inputs=None, outputs=[chatbot, msg])
|
42 |
|
43 |
demo.launch()
|