Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
@@ -7,47 +62,30 @@ client = InferenceClient(
|
|
7 |
token=os.getenv('HF_TOKEN')
|
8 |
)
|
9 |
|
10 |
-
def
|
11 |
-
|
12 |
-
messages = [{"role": "system", "content": system_message}]
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
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 |
-
|
34 |
-
|
35 |
return response
|
36 |
|
37 |
demo = gr.Interface(
|
38 |
-
fn=
|
39 |
inputs=[
|
40 |
gr.Textbox(lines=2, label="User Message"),
|
41 |
-
gr.Textbox(value="You are a
|
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__":
|
|
|
1 |
+
# import gradio as gr
|
2 |
+
# from huggingface_hub import InferenceClient
|
3 |
+
# import os
|
4 |
+
|
5 |
+
# client = InferenceClient(
|
6 |
+
# model="mistralai/Mistral-Small-24B-Instruct-2501",
|
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()
|
55 |
+
|
56 |
import gradio as gr
|
57 |
from huggingface_hub import InferenceClient
|
58 |
import os
|
|
|
62 |
token=os.getenv('HF_TOKEN')
|
63 |
)
|
64 |
|
65 |
+
def simple_chat_fn(message, system_prompt, max_tokens, temperature, top_p):
|
66 |
+
prompt = f"{system_prompt}\n\nUser: {message}\nAssistant:"
|
|
|
67 |
|
68 |
+
response = client.text_generation(
|
69 |
+
prompt=prompt,
|
70 |
+
max_new_tokens=max_tokens,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
temperature=temperature,
|
72 |
top_p=top_p,
|
73 |
+
stream=False
|
74 |
+
)
|
|
|
75 |
return response
|
76 |
|
77 |
demo = gr.Interface(
|
78 |
+
fn=simple_chat_fn,
|
79 |
inputs=[
|
80 |
gr.Textbox(lines=2, label="User Message"),
|
81 |
+
gr.Textbox(value="You are a helpful and concise assistant.", label="System Prompt"),
|
|
|
82 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens"),
|
83 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
84 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
85 |
],
|
86 |
outputs="text",
|
87 |
+
title="Mistral 24B Chat (Single Turn)",
|
88 |
allow_flagging="never",
|
|
|
|
|
89 |
)
|
90 |
|
91 |
if __name__ == "__main__":
|