Update app.py
Browse files
app.py
CHANGED
@@ -2,64 +2,38 @@ import gradio as gr
|
|
2 |
import os
|
3 |
from huggingface_hub import InferenceClient
|
4 |
|
5 |
-
|
6 |
-
return InferenceClient(model_name)
|
7 |
|
8 |
-
|
9 |
-
zephyr_client = get_client("HuggingFaceH4/zephyr-7b-beta")
|
10 |
-
deepseek_client = get_client("deepseek-ai/DeepSeek-R1")
|
11 |
-
|
12 |
-
def generate_response(client, message, history, temperature, top_p):
|
13 |
system_rules = os.getenv("SYSTEM_RULES")
|
14 |
-
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
if user_msg:
|
18 |
-
messages.append({"role": "user", "content": user_msg})
|
19 |
-
if bot_reply:
|
20 |
-
messages.append({"role": "assistant", "content": bot_reply})
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
messages.append({"role": "user", "content": message})
|
23 |
-
|
24 |
response = ""
|
25 |
-
|
|
|
26 |
messages,
|
27 |
-
max_tokens=
|
28 |
stream=True,
|
29 |
temperature=temperature,
|
30 |
top_p=top_p,
|
31 |
):
|
32 |
-
token =
|
33 |
response += token
|
34 |
yield response
|
35 |
|
36 |
-
|
37 |
-
def respond(message, history: list[tuple[str, str]], model_choice="Best Match"):
|
38 |
-
temperature = 0.7
|
39 |
-
top_p = 0.9
|
40 |
-
|
41 |
-
|
42 |
-
zephyr_response = generate_response(zephyr_client, message, history, temperature, top_p)
|
43 |
-
deepseek_response = generate_response(deepseek_client, message, history, temperature, top_p)
|
44 |
-
|
45 |
-
|
46 |
-
if model_choice == "Zephyr Only":
|
47 |
-
return zephyr_response
|
48 |
-
elif model_choice == "DeepSeek Only":
|
49 |
-
return deepseek_response
|
50 |
-
else:
|
51 |
-
|
52 |
-
zephyr_text = "".join(zephyr_response)
|
53 |
-
deepseek_text = "".join(deepseek_response)
|
54 |
-
return zephyr_text if len(zephyr_text) > len(deepseek_text) else deepseek_text
|
55 |
-
|
56 |
-
|
57 |
-
demo = gr.ChatInterface(
|
58 |
-
respond,
|
59 |
-
description="A chatbot powered by two AI models: Zephyr-7B and DeepSeek-R1.",
|
60 |
-
additional_inputs=[gr.Radio(["Best Match", "Zephyr Only", "DeepSeek Only"], label="Model Selection", value="Best Match")],
|
61 |
-
theme="Yntec/HaleyCH_Theme_Orange"
|
62 |
-
)
|
63 |
|
64 |
if __name__ == "__main__":
|
65 |
-
demo.launch()
|
|
|
2 |
import os
|
3 |
from huggingface_hub import InferenceClient
|
4 |
|
5 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
6 |
|
7 |
+
def respond(message, history: list[tuple[str, str]]):
|
|
|
|
|
|
|
|
|
8 |
system_rules = os.getenv("SYSTEM_RULES")
|
9 |
+
max_tokens = 512
|
10 |
+
temperature = 0.75
|
11 |
+
top_p = 0.95
|
12 |
|
13 |
+
messages = [{"role": "system", "content": system_rules}]
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
for val in history:
|
16 |
+
if val[0]:
|
17 |
+
messages.append({"role": "user", "content": val[0]})
|
18 |
+
if val[1]:
|
19 |
+
messages.append({"role": "assistant", "content": val[1]})
|
20 |
+
|
21 |
messages.append({"role": "user", "content": message})
|
22 |
+
|
23 |
response = ""
|
24 |
+
|
25 |
+
for message in client.chat_completion(
|
26 |
messages,
|
27 |
+
max_tokens=4096,
|
28 |
stream=True,
|
29 |
temperature=temperature,
|
30 |
top_p=top_p,
|
31 |
):
|
32 |
+
token = message.choices[0].delta.content
|
33 |
response += token
|
34 |
yield response
|
35 |
|
36 |
+
demo = gr.ChatInterface(respond, description="This chatbot doesn't have restrictions. Use responsibly.", theme="Yntec/HaleyCH_Theme_Orange")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
if __name__ == "__main__":
|
39 |
+
demo.launch()
|