Leri777 commited on
Commit
1277c3a
·
verified ·
1 Parent(s): f582d39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -51
app.py CHANGED
@@ -1,52 +1,45 @@
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="Tomoniai's Mixtral 8x7b Chat",
48
- retry_btn=None,
49
- undo_btn=None
50
- )
51
-
52
- demo.queue().launch(show_api=False)
 
1
+ import os
2
  import gradio as gr
3
+ import torch
4
+ from transformers import pipeline
5
+ from dotenv import load_dotenv
6
+
7
+ # Загрузка переменных окружения
8
+ load_dotenv()
9
+ MODEL_NAME = os.getenv("MODEL_NAME", "Mixtral-8x7B-Instruct-v0-1")
10
+ HF_TOKEN = os.getenv("HF_TOKEN")
11
+
12
+ # Инициализация пайплайна для работы с моделью
13
+ pipe = pipeline(
14
+ "text-generation",
15
+ model=MODEL_NAME,
16
+ use_auth_token=HF_TOKEN,
17
+ device=0 if torch.cuda.is_available() else -1
18
+ )
19
+
20
+ def generate_response(prompt):
21
+ """
22
+ Функция для генерации ответа с использованием модели.
23
+ Форматирует запрос в соответствии с требованиями модели.
24
+ """
25
+ # Форматирование инструкции согласно требованиям модели
26
+ formatted_prompt = f"<s>[INST] {prompt} [/INST]</s>"
27
+ response = pipe(formatted_prompt, max_length=100, num_return_sequences=1)
28
+ return response[0]['generated_text']
29
+
30
+ # Интерфейс Gradio для взаимодействия с моделью
31
+ def main():
32
+ with gr.Blocks() as demo:
33
+ gr.Markdown("# Mixtral-8x7B Chat Interface")
34
+ with gr.Row():
35
+ with gr.Column():
36
+ prompt = gr.Textbox(label="Введите ваш запрос", placeholder="Введите текст сюда...")
37
+ submit_btn = gr.Button("Сгенерировать ответ")
38
+ with gr.Column():
39
+ response = gr.Textbox(label="Ответ модели")
40
+ submit_btn.click(fn=generate_response, inputs=prompt, outputs=response)
41
+
42
+ demo.launch(server_name="0.0.0.0", server_port=7860)
43
+
44
+ if __name__ == "__main__":
45
+ main()