Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,55 +27,48 @@ def auto_update():
|
|
| 27 |
threading.Thread(target=auto_update, daemon=True).start()
|
| 28 |
|
| 29 |
# Chatbot response function using the InferenceClient
|
| 30 |
-
def respond(
|
| 31 |
-
message,
|
| 32 |
-
history: list[tuple[str, str]],
|
| 33 |
-
system_message,
|
| 34 |
-
max_tokens,
|
| 35 |
-
temperature,
|
| 36 |
-
top_p,
|
| 37 |
-
):
|
| 38 |
# Prepare the message history for the model
|
| 39 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 40 |
for val in history:
|
| 41 |
-
if val[0]:
|
| 42 |
messages.append({"role": "user", "content": val[0]})
|
| 43 |
-
if val[1]:
|
| 44 |
messages.append({"role": "assistant", "content": val[1]})
|
| 45 |
-
|
|
|
|
| 46 |
messages.append({"role": "user", "content": message})
|
| 47 |
|
| 48 |
response = ""
|
| 49 |
|
| 50 |
-
|
|
|
|
| 51 |
messages,
|
| 52 |
max_tokens=max_tokens,
|
| 53 |
stream=True,
|
| 54 |
temperature=temperature,
|
| 55 |
top_p=top_p,
|
| 56 |
):
|
| 57 |
-
token =
|
| 58 |
response += token
|
| 59 |
-
yield response
|
| 60 |
|
| 61 |
# Gradio interface
|
| 62 |
-
demo = gr.
|
| 63 |
-
respond,
|
| 64 |
-
|
| 65 |
-
gr.Textbox(
|
| 66 |
-
gr.
|
|
|
|
|
|
|
| 67 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 68 |
-
gr.Slider(
|
| 69 |
-
minimum=0.1,
|
| 70 |
-
maximum=1.0,
|
| 71 |
-
value=0.95,
|
| 72 |
-
step=0.05,
|
| 73 |
-
label="Top-p (nucleus sampling)",
|
| 74 |
-
),
|
| 75 |
],
|
|
|
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
# Launch the interface
|
| 79 |
if __name__ == "__main__":
|
| 80 |
demo.launch()
|
| 81 |
-
|
|
|
|
| 27 |
threading.Thread(target=auto_update, daemon=True).start()
|
| 28 |
|
| 29 |
# Chatbot response function using the InferenceClient
|
| 30 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# Prepare the message history for the model
|
| 32 |
messages = [{"role": "system", "content": system_message}]
|
| 33 |
+
|
| 34 |
for val in history:
|
| 35 |
+
if val[0]: # User's message
|
| 36 |
messages.append({"role": "user", "content": val[0]})
|
| 37 |
+
if val[1]: # Assistant's response
|
| 38 |
messages.append({"role": "assistant", "content": val[1]})
|
| 39 |
+
|
| 40 |
+
# Add the new user message
|
| 41 |
messages.append({"role": "user", "content": message})
|
| 42 |
|
| 43 |
response = ""
|
| 44 |
|
| 45 |
+
# Generate the response using the model
|
| 46 |
+
for msg in client.chat_completion(
|
| 47 |
messages,
|
| 48 |
max_tokens=max_tokens,
|
| 49 |
stream=True,
|
| 50 |
temperature=temperature,
|
| 51 |
top_p=top_p,
|
| 52 |
):
|
| 53 |
+
token = msg.choices[0].delta.content
|
| 54 |
response += token
|
| 55 |
+
yield response # Streaming response
|
| 56 |
|
| 57 |
# Gradio interface
|
| 58 |
+
demo = gr.Interface(
|
| 59 |
+
fn=respond,
|
| 60 |
+
inputs=[
|
| 61 |
+
gr.Textbox(label="User Message"),
|
| 62 |
+
gr.State(), # Gradio State to store the conversation history
|
| 63 |
+
gr.Textbox(value="You are a helpful chatbot.", label="System Message"),
|
| 64 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens"),
|
| 65 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 66 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
],
|
| 68 |
+
outputs=gr.Chatbot(), # Gradio chatbot output
|
| 69 |
+
live=True # Enable live responses
|
| 70 |
)
|
| 71 |
|
| 72 |
# Launch the interface
|
| 73 |
if __name__ == "__main__":
|
| 74 |
demo.launch()
|
|
|