Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,18 @@
|
|
1 |
-
from huggingface_hub import
|
|
|
2 |
import gradio as gr
|
3 |
|
4 |
-
|
5 |
-
"mistralai/Mistral-7B-Instruct-v0.1"
|
6 |
-
)
|
7 |
-
|
8 |
|
9 |
def format_prompt(message, history):
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
def generate(
|
18 |
-
prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
|
19 |
-
):
|
20 |
temperature = float(temperature)
|
21 |
if temperature < 1e-2:
|
22 |
temperature = 1e-2
|
@@ -32,59 +28,28 @@ def generate(
|
|
32 |
)
|
33 |
|
34 |
formatted_prompt = format_prompt(prompt, history)
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
step=64,
|
61 |
-
interactive=True,
|
62 |
-
info="The maximum numbers of new tokens",
|
63 |
-
),
|
64 |
-
gr.Slider(
|
65 |
-
label="Top-p (nucleus sampling)",
|
66 |
-
value=0.90,
|
67 |
-
minimum=0.0,
|
68 |
-
maximum=1,
|
69 |
-
step=0.05,
|
70 |
-
interactive=True,
|
71 |
-
info="Higher values sample more low-probability tokens",
|
72 |
-
),
|
73 |
-
gr.Slider(
|
74 |
-
label="Repetition penalty",
|
75 |
-
value=1.2,
|
76 |
-
minimum=1.0,
|
77 |
-
maximum=2.0,
|
78 |
-
step=0.05,
|
79 |
-
interactive=True,
|
80 |
-
info="Penalize repeated tokens",
|
81 |
-
)
|
82 |
-
]
|
83 |
-
|
84 |
-
|
85 |
-
gr.ChatInterface(
|
86 |
-
fn=generate,
|
87 |
-
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
88 |
-
additional_inputs=additional_inputs,
|
89 |
-
title="""Mistral 7B 01"""
|
90 |
-
).launch(show_api=True)
|
|
|
1 |
+
from huggingface_hub import InferenceAPI
|
2 |
+
from urllib.parse import urlparse, parse_qs
|
3 |
import gradio as gr
|
4 |
|
5 |
+
api = InferenceAPI("mistralai/Mistral-7B-Instruct-v0.1")
|
|
|
|
|
|
|
6 |
|
7 |
def format_prompt(message, history):
|
8 |
+
prompt = "<s>"
|
9 |
+
for user_prompt, bot_response in history:
|
10 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
11 |
+
prompt += f" {bot_response}</s> "
|
12 |
+
prompt += f"[INST] {message} [/INST]"
|
13 |
+
return prompt
|
14 |
+
|
15 |
+
def generate(prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
|
|
|
|
|
16 |
temperature = float(temperature)
|
17 |
if temperature < 1e-2:
|
18 |
temperature = 1e-2
|
|
|
28 |
)
|
29 |
|
30 |
formatted_prompt = format_prompt(prompt, history)
|
31 |
+
response = api.generate_text(formatted_prompt, **generate_kwargs)
|
32 |
+
return response
|
33 |
+
|
34 |
+
def chatbot(message):
|
35 |
+
history = []
|
36 |
+
response = generate(message, history)
|
37 |
+
history.append(("User", message))
|
38 |
+
history.append(("ChatBot", response))
|
39 |
+
return response
|
40 |
+
|
41 |
+
def get_message_from_url():
|
42 |
+
url = urlparse(gradio.Interface.get_share_url())
|
43 |
+
query_params = parse_qs(url.query)
|
44 |
+
if "message" in query_params:
|
45 |
+
return query_params["message"][0]
|
46 |
+
return ""
|
47 |
+
|
48 |
+
message = get_message_from_url()
|
49 |
+
inputs = gr.inputs.Textbox(lines=2, placeholder="Type your message here...", initial_message=message)
|
50 |
+
outputs = gr.outputs.Textbox()
|
51 |
+
|
52 |
+
title = "Mistral 7B Chatbot"
|
53 |
+
description = "Chat with Mistral 7B, a powerful language model!"
|
54 |
+
|
55 |
+
gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title=title, description=description).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|