Spaces:
Runtime error
Runtime error
ahmadalfakeh
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,70 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
@spaces.GPU()
|
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 val in history:
|
21 |
if val[0]:
|
22 |
messages.append({"role": "user", "content": val[0]})
|
23 |
if val[1]:
|
24 |
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
|
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
response = ""
|
29 |
|
|
|
30 |
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
max_tokens=max_tokens,
|
33 |
stream=True,
|
34 |
temperature=temperature,
|
35 |
top_p=top_p,
|
36 |
):
|
37 |
token = message.choices[0].delta.content
|
38 |
-
|
39 |
response += token
|
40 |
yield response
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
""
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
gr.
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
|
|
|
|
|
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
|
4 |
+
# Initialize the Hugging Face Inference Client
|
5 |
+
client = InferenceClient(model="meta-llama/Meta-Llama-3.1-405B-FP8")
|
6 |
+
|
7 |
+
# Define the response generation function
|
8 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
messages = [{"role": "system", "content": system_message}]
|
10 |
|
11 |
+
# Add previous messages to the conversation
|
12 |
for val in history:
|
13 |
if val[0]:
|
14 |
messages.append({"role": "user", "content": val[0]})
|
15 |
if val[1]:
|
16 |
messages.append({"role": "assistant", "content": val[1]})
|
17 |
|
18 |
+
# Add the new user message
|
19 |
messages.append({"role": "user", "content": message})
|
20 |
|
21 |
response = ""
|
22 |
|
23 |
+
# Generate the response using the model
|
24 |
for message in client.chat_completion(
|
25 |
+
messages=messages,
|
26 |
max_tokens=max_tokens,
|
27 |
stream=True,
|
28 |
temperature=temperature,
|
29 |
top_p=top_p,
|
30 |
):
|
31 |
token = message.choices[0].delta.content
|
|
|
32 |
response += token
|
33 |
yield response
|
34 |
|
35 |
+
# Define the ChatGPT-like interface
|
36 |
+
with gr.Blocks(css=".gradio-container {max-width: 900px; margin: auto;}") as demo:
|
37 |
+
gr.Markdown("<h1 style='text-align: center;'>ChatGPT-like Interface</h1>")
|
38 |
+
|
39 |
+
chatbot = gr.Chatbot(height=500)
|
40 |
+
with gr.Row():
|
41 |
+
with gr.Column(scale=6):
|
42 |
+
msg = gr.Textbox(
|
43 |
+
show_label=False,
|
44 |
+
placeholder="Type your message here...",
|
45 |
+
container=False
|
46 |
+
).style(container=False)
|
47 |
+
with gr.Column(scale=1, min_width=100):
|
48 |
+
send_btn = gr.Button("Send").style(full_width=True)
|
49 |
+
|
50 |
+
with gr.Accordion("Settings", open=False):
|
51 |
+
system_message = gr.Textbox(value="You are a friendly Chatbot.", label="System message")
|
52 |
+
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
53 |
+
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
54 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
55 |
+
|
56 |
+
def user_interaction(user_message, history, system_message, max_tokens, temperature, top_p):
|
57 |
+
history = history or []
|
58 |
+
history.append((user_message, None))
|
59 |
+
bot_response = respond(user_message, history, system_message, max_tokens, temperature, top_p)
|
60 |
+
history[-1] = (user_message, next(bot_response))
|
61 |
+
for token in bot_response:
|
62 |
+
history[-1] = (user_message, token)
|
63 |
+
yield history
|
64 |
|
65 |
+
msg.submit(user_interaction, [msg, chatbot, system_message, max_tokens, temperature, top_p], chatbot)
|
66 |
+
send_btn.click(user_interaction, [msg, chatbot, system_message, max_tokens, temperature, top_p], chatbot)
|
67 |
+
send_btn.click(lambda: "", None, msg) # Clear the input box after sending
|
68 |
|
69 |
if __name__ == "__main__":
|
70 |
+
demo.launch()
|