reset to basic
Browse files- app.py +57 -60
- requirements.txt +1 -6
app.py
CHANGED
@@ -1,72 +1,69 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
-
import uuid
|
5 |
-
import os
|
6 |
-
from datetime import datetime
|
7 |
-
import spaces
|
8 |
|
9 |
-
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
|
10 |
with open("system_prompt.txt", "r") as f:
|
11 |
SYSTEM_PROMPT = f.read()
|
12 |
-
LOG_DIR = "chat_logs"
|
13 |
-
os.makedirs(LOG_DIR, exist_ok=True)
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
22 |
-
with open(os.path.join(LOG_DIR, f"{session_id}.txt"), "a") as f:
|
23 |
-
f.write(f"[{timestamp}] User: {user_msg}\n")
|
24 |
-
f.write(f"[{timestamp}] Bot: {bot_msg}\n\n")
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
)
|
35 |
-
return tokenizer, model
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
for user_msg, bot_msg in history:
|
40 |
-
messages.append({"role": "user", "content": user_msg})
|
41 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
42 |
-
messages.append({"role": "user", "content": new_input})
|
43 |
-
return tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
44 |
|
45 |
-
@torch.no_grad()
|
46 |
-
def respond(message, history):
|
47 |
-
global tokenizer, model
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
prompt = format_chat_prompt(history, message)
|
54 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
55 |
-
output = model.generate(
|
56 |
-
**inputs,
|
57 |
-
max_new_tokens=512,
|
58 |
-
do_sample=True,
|
59 |
-
temperature=0.7,
|
60 |
-
top_p=0.95,
|
61 |
-
pad_token_id=tokenizer.eos_token_id
|
62 |
-
)
|
63 |
-
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
|
64 |
-
response = decoded.split(message)[-1].strip().split("\n")[0].strip()
|
65 |
-
log_chat(session_id, message, response)
|
66 |
-
return response
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
title="BoundrAI",
|
71 |
-
theme="soft"
|
72 |
-
).launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
3 |
|
|
|
4 |
with open("system_prompt.txt", "r") as f:
|
5 |
SYSTEM_PROMPT = f.read()
|
|
|
|
|
6 |
|
7 |
+
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
|
8 |
+
|
9 |
+
"""
|
10 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
11 |
+
"""
|
12 |
+
client = InferenceClient(MODEL_NAME)
|
13 |
+
|
14 |
+
|
15 |
+
def respond(
|
16 |
+
message,
|
17 |
+
history: list[tuple[str, str]],
|
18 |
+
system_message,
|
19 |
+
max_tokens,
|
20 |
+
temperature,
|
21 |
+
top_p,
|
22 |
+
):
|
23 |
+
messages = [{"role": "system", "content": system_message}]
|
24 |
+
|
25 |
+
for val in history:
|
26 |
+
if val[0]:
|
27 |
+
messages.append({"role": "user", "content": val[0]})
|
28 |
+
if val[1]:
|
29 |
+
messages.append({"role": "assistant", "content": val[1]})
|
30 |
+
|
31 |
+
messages.append({"role": "user", "content": message})
|
32 |
|
33 |
+
response = ""
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
for message in client.chat_completion(
|
36 |
+
messages,
|
37 |
+
max_tokens=max_tokens,
|
38 |
+
stream=True,
|
39 |
+
temperature=temperature,
|
40 |
+
top_p=top_p,
|
41 |
+
):
|
42 |
+
token = message.choices[0].delta.content
|
|
|
|
|
43 |
|
44 |
+
response += token
|
45 |
+
yield response
|
|
|
|
|
|
|
|
|
|
|
46 |
|
|
|
|
|
|
|
47 |
|
48 |
+
"""
|
49 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
50 |
+
"""
|
51 |
+
demo = gr.ChatInterface(
|
52 |
+
respond,
|
53 |
+
additional_inputs=[
|
54 |
+
gr.Textbox(value=SYSTEM_PROMPT, label="System message"),
|
55 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
56 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
57 |
+
gr.Slider(
|
58 |
+
minimum=0.1,
|
59 |
+
maximum=1.0,
|
60 |
+
value=0.95,
|
61 |
+
step=0.05,
|
62 |
+
label="Top-p (nucleus sampling)",
|
63 |
+
),
|
64 |
+
],
|
65 |
+
)
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
if __name__ == "__main__":
|
69 |
+
demo.launch()
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,6 +1 @@
|
|
1 |
-
huggingface_hub==0.25.2
|
2 |
-
gradio
|
3 |
-
transformers
|
4 |
-
torch
|
5 |
-
spaces
|
6 |
-
accelerate>=0.26.0
|
|
|
1 |
+
huggingface_hub==0.25.2
|
|
|
|
|
|
|
|
|
|