Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,79 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
|
4 |
-
""
|
5 |
-
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
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
9 |
|
10 |
-
def respond(
|
11 |
-
message
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
for
|
21 |
-
if
|
22 |
-
messages.append({"role": "user", "content":
|
23 |
-
if
|
24 |
-
messages.append({"role": "assistant", "content":
|
25 |
|
|
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
"""
|
43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
44 |
-
"""
|
45 |
-
demo = gr.ChatInterface(
|
46 |
-
respond,
|
47 |
-
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
-
)
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
from transformers import AutoTokenizer
|
4 |
|
5 |
+
client = InferenceClient(model="AriakimTaiyo/gpt2-chat")
|
|
|
|
|
|
|
6 |
|
7 |
+
# Load the tokenizer explicitly
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained("AriakimTaiyo/gpt2-chat")
|
9 |
|
10 |
+
def respond(message, history, system_message):
|
11 |
+
# Prepare messages, starting with the system message
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
messages = [{"role": "system", "content": system_message}]
|
13 |
|
14 |
+
for user_msg, assistant_msg in history:
|
15 |
+
if user_msg:
|
16 |
+
messages.append({"role": "user", "content": user_msg})
|
17 |
+
if assistant_msg:
|
18 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
19 |
|
20 |
+
# Add the latest user message
|
21 |
messages.append({"role": "user", "content": message})
|
22 |
|
23 |
response = ""
|
24 |
+
try:
|
25 |
+
# Generate responses
|
26 |
+
for message in client.chat_completion(
|
27 |
+
messages=messages,
|
28 |
+
max_tokens=256,
|
29 |
+
stream=True,
|
30 |
+
temperature=0.7,
|
31 |
+
top_p=0.95,
|
32 |
+
):
|
33 |
+
token = message.choices[0].delta.get('content', '')
|
34 |
+
response += token
|
35 |
+
yield response
|
36 |
+
except Exception as e:
|
37 |
+
# Return the error message in case of an exception
|
38 |
+
yield f"Hata: {e}"
|
39 |
|
40 |
+
# Create the Gradio interface
|
41 |
+
with gr.Blocks(theme=gr.Theme.from_hub('HaleyCH/HaleyCH_Theme')) as demo:
|
42 |
+
system_message = gr.HTML("""
|
43 |
+
<h1 style="color: #fff; text-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px #fff, 0 0 10px #0000ff, 0 0 15px #0000ff; text-align: center;">
|
44 |
+
SIMULACRA GPT-2
|
45 |
+
</h1>
|
46 |
+
<p>🤖 Welcome to Simulacra user! See our account for more information.</p>
|
47 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
chatbot = gr.Chatbot()
|
50 |
+
msg = gr.Textbox(label="Mesajınızı yazın")
|
51 |
+
|
52 |
+
# Place buttons side by side
|
53 |
+
with gr.Row():
|
54 |
+
clear = gr.Button("Temizle")
|
55 |
+
submit = gr.Button("Gönder")
|
56 |
|
57 |
+
def user_input(user_message, history):
|
58 |
+
return "", history + [[user_message, None]]
|
59 |
+
|
60 |
+
def bot_response(history):
|
61 |
+
last_message = history[-1][0]
|
62 |
+
response_gen = respond(
|
63 |
+
message=last_message,
|
64 |
+
history=history[:-1],
|
65 |
+
system_message=system_message.value,
|
66 |
+
)
|
67 |
+
for response in response_gen:
|
68 |
+
history[-1][1] = response
|
69 |
+
yield history
|
70 |
+
|
71 |
+
msg.submit(user_input, [msg, chatbot], [msg, chatbot], queue=False).then(
|
72 |
+
bot_response, chatbot, chatbot
|
73 |
+
)
|
74 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
75 |
+
submit.click(lambda: msg.submit(), None, chatbot, queue=False) # Send the message when the "Gönder" button is clicked
|
76 |
+
|
77 |
+
# Launch the app
|
78 |
if __name__ == "__main__":
|
79 |
+
demo.launch(share=True)
|