Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,67 +2,53 @@ import gradio as gr
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
|
5 |
-
"""
|
6 |
-
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
|
7 |
-
"""
|
8 |
client = InferenceClient(
|
9 |
model="HuggingFaceH4/zephyr-7b-beta",
|
10 |
token=os.getenv('HF_TOKEN')
|
11 |
)
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
message,
|
16 |
-
history: list[tuple[str, str]],
|
17 |
-
system_message,
|
18 |
-
max_tokens,
|
19 |
-
temperature,
|
20 |
-
top_p,
|
21 |
-
):
|
22 |
messages = [{"role": "system", "content": system_message}]
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
30 |
messages.append({"role": "user", "content": message})
|
31 |
|
|
|
32 |
response = ""
|
33 |
-
|
34 |
-
|
35 |
-
messages,
|
36 |
-
max_tokens=max_tokens,
|
37 |
stream=True,
|
|
|
38 |
temperature=temperature,
|
39 |
top_p=top_p,
|
40 |
):
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
"""
|
50 |
-
|
51 |
-
|
52 |
-
additional_inputs=[
|
53 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
54 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
55 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
56 |
-
gr.Slider(
|
57 |
-
minimum=0.1,
|
58 |
-
maximum=1.0,
|
59 |
-
value=0.95,
|
60 |
-
step=0.05,
|
61 |
-
label="Top-p (nucleus sampling)",
|
62 |
-
),
|
63 |
],
|
|
|
|
|
|
|
|
|
64 |
)
|
65 |
|
66 |
-
|
67 |
if __name__ == "__main__":
|
68 |
demo.launch()
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
|
|
|
|
|
|
|
5 |
client = InferenceClient(
|
6 |
model="HuggingFaceH4/zephyr-7b-beta",
|
7 |
token=os.getenv('HF_TOKEN')
|
8 |
)
|
9 |
|
10 |
+
def chat_fn(message, system_message, history_str, max_tokens, temperature, top_p):
|
11 |
+
# Convert history string (optional) to message list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
messages = [{"role": "system", "content": system_message}]
|
13 |
+
|
14 |
+
if history_str:
|
15 |
+
# Format: user1||assistant1\nuser2||assistant2
|
16 |
+
for pair in history_str.split("\n"):
|
17 |
+
if "||" in pair:
|
18 |
+
user_msg, assistant_msg = pair.split("||", 1)
|
19 |
+
messages.append({"role": "user", "content": user_msg})
|
20 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
21 |
+
|
22 |
messages.append({"role": "user", "content": message})
|
23 |
|
24 |
+
# Get response from HF
|
25 |
response = ""
|
26 |
+
for chunk in client.chat_completion(
|
27 |
+
messages=messages,
|
|
|
|
|
28 |
stream=True,
|
29 |
+
max_tokens=max_tokens,
|
30 |
temperature=temperature,
|
31 |
top_p=top_p,
|
32 |
):
|
33 |
+
response += chunk.choices[0].delta.content or ""
|
34 |
+
|
35 |
+
return response
|
36 |
+
|
37 |
+
demo = gr.Interface(
|
38 |
+
fn=chat_fn,
|
39 |
+
inputs=[
|
40 |
+
gr.Textbox(lines=2, label="User Message"),
|
41 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System Prompt"),
|
42 |
+
gr.Textbox(lines=4, placeholder="user||bot\nuser2||bot2", label="Conversation History (optional)"),
|
43 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens"),
|
|
|
|
|
|
|
44 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
45 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
],
|
47 |
+
outputs="text",
|
48 |
+
allow_flagging="never",
|
49 |
+
title="LLM Budaya",
|
50 |
+
description="Chatbot menggunakan model HuggingFace Zephyr-7B"
|
51 |
)
|
52 |
|
|
|
53 |
if __name__ == "__main__":
|
54 |
demo.launch()
|