Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,52 @@
|
|
1 |
-
import
|
2 |
import gradio as gr
|
3 |
-
import torch
|
4 |
-
import logging
|
5 |
-
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
6 |
-
from dotenv import load_dotenv
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
if not HF_TOKEN:
|
19 |
-
logger.error("HF_TOKEN не задан. Пожалуйста, укажите токен доступа Hugging Face в файле .env.")
|
20 |
-
raise EnvironmentError("Отсутствует токен доступа Hugging Face.")
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
logger.info(f"Попытка загрузить модель: {MODEL_NAME}")
|
25 |
-
pipe = pipeline(
|
26 |
-
"text-generation",
|
27 |
-
model=MODEL_NAME,
|
28 |
-
use_auth_token=HF_TOKEN,
|
29 |
-
device=0 if torch.cuda.is_available() else -1
|
30 |
-
)
|
31 |
-
logger.info("Модель успешно загружена.")
|
32 |
-
except Exception as e:
|
33 |
-
logger.error(f"Ошибка при загрузке модели: {e}")
|
34 |
-
raise
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
"""
|
41 |
-
try:
|
42 |
-
# Форматирование инструкции согласно требованиям модели
|
43 |
-
formatted_prompt = f"<s>[INST] {prompt} [/INST]</s>"
|
44 |
-
logger.debug(f"Сформированный запрос: {formatted_prompt}")
|
45 |
-
response = pipe(formatted_prompt, max_length=150, num_return_sequences=1)
|
46 |
-
logger.debug(f"Полученный ответ: {response}")
|
47 |
-
return response[0]['generated_text'].replace(formatted_prompt, "").strip()
|
48 |
-
except Exception as e:
|
49 |
-
logger.error(f"Ошибка при генерации ответа: {e}")
|
50 |
-
return "Произошла ошибка при генерации ответа. Пожалуйста, попробуйте еще раз."
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
gr.Markdown("# Mixtral-8x7B Chat Interface")
|
56 |
-
with gr.Row():
|
57 |
-
with gr.Column():
|
58 |
-
prompt = gr.Textbox(label="Введите ваш запрос", placeholder="Введите текст сюда...")
|
59 |
-
submit_btn = gr.Button("Сгенерировать ответ")
|
60 |
-
with gr.Column():
|
61 |
-
response = gr.Textbox(label="Ответ модели")
|
62 |
-
submit_btn.click(fn=generate_response, inputs=prompt, outputs=response)
|
63 |
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
main()
|
|
|
1 |
+
from huggingface_hub import InferenceClient
|
2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
5 |
+
|
6 |
+
def format_prompt(message, history):
|
7 |
+
prompt = "<s>"
|
8 |
+
for user_prompt, bot_response in history:
|
9 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
10 |
+
prompt += f" {bot_response}</s> "
|
11 |
+
prompt += f"[INST] {message} [/INST]"
|
12 |
+
return prompt
|
13 |
+
|
14 |
+
def generate(
|
15 |
+
prompt, history, temperature=0.2, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
|
16 |
+
):
|
17 |
+
temperature = float(temperature)
|
18 |
+
if temperature < 1e-2:
|
19 |
+
temperature = 1e-2
|
20 |
+
top_p = float(top_p)
|
21 |
+
|
22 |
+
generate_kwargs = dict(
|
23 |
+
temperature=temperature,
|
24 |
+
max_new_tokens=max_new_tokens,
|
25 |
+
top_p=top_p,
|
26 |
+
repetition_penalty=repetition_penalty,
|
27 |
+
do_sample=True,
|
28 |
+
seed=42,
|
29 |
+
)
|
30 |
|
31 |
+
formatted_prompt = format_prompt(prompt, history)
|
|
|
|
|
|
|
32 |
|
33 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
34 |
+
output = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
for response in stream:
|
37 |
+
output += response.token.text
|
38 |
+
yield output
|
39 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
|
42 |
+
mychatbot = gr.Chatbot(
|
43 |
+
avatar_images=["./user.png", "./botm.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
demo = gr.ChatInterface(fn=generate,
|
46 |
+
chatbot=mychatbot,
|
47 |
+
#title="WebpyGPT",
|
48 |
+
retry_btn=None,
|
49 |
+
undo_btn=None
|
50 |
+
)
|
51 |
|
52 |
+
demo.queue().launch(show_api=False)
|
|