Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import spaces
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
import torch
|
6 |
+
from transformers import pipeline
|
7 |
+
|
8 |
+
pipe = pipeline("text-generation", model="BioMistral/BioMistral-7B", torch_dtype=torch.bfloat16, device_map="auto")
|
9 |
+
|
10 |
+
|
11 |
+
@spaces.GPU
|
12 |
+
def respond(
|
13 |
+
message,
|
14 |
+
history: list[tuple[str, str]],
|
15 |
+
system_message,
|
16 |
+
max_tokens,
|
17 |
+
temperature,
|
18 |
+
top_p,
|
19 |
+
hf_token,
|
20 |
+
):
|
21 |
+
|
22 |
+
messages = [{"role": "system", "content": system_message}]
|
23 |
+
|
24 |
+
for val in history:
|
25 |
+
if val[0]:
|
26 |
+
messages.append({"role": "user", "content": val[0]})
|
27 |
+
if val[1]:
|
28 |
+
messages.append({"role": "assistant", "content": val[1]})
|
29 |
+
|
30 |
+
messages.append({"role": "user", "content": message})
|
31 |
+
|
32 |
+
response = ""
|
33 |
+
|
34 |
+
prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
35 |
+
outputs = pipe(prompt, max_new_tokens=max_tokens, do_sample=True, temperature=temperature, top_k=50, top_p=top_p)
|
36 |
+
last_space_index = outputs[0]["generated_text"].rfind('[/INST]')
|
37 |
+
# Extract the substring after the last space character
|
38 |
+
substring_after_last_space = outputs[0]["generated_text"][last_space_index + 7:]
|
39 |
+
yield substring_after_last_space
|
40 |
+
|
41 |
+
|
42 |
+
demo = gr.ChatInterface(
|
43 |
+
respond,
|
44 |
+
|
45 |
+
additional_inputs=[
|
46 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
47 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
48 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
49 |
+
gr.Slider(
|
50 |
+
minimum=0.1,
|
51 |
+
maximum=1.0,
|
52 |
+
value=0.95,
|
53 |
+
step=0.05,
|
54 |
+
label="Top-p (nucleus sampling)",
|
55 |
+
|
56 |
+
),
|
57 |
+
gr.Textbox(label="Hugging Face Token", placeholder="Enter your Hugging Face token here"),
|
58 |
+
],
|
59 |
+
css="footer{display:none !important}",
|
60 |
+
)
|
61 |
+
|
62 |
+
|
63 |
+
if __name__ == "__main__":
|
64 |
+
demo.launch()
|