Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 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 |
def respond(
|
|
@@ -14,10 +12,8 @@ def respond(
|
|
| 14 |
temperature,
|
| 15 |
top_p,
|
| 16 |
):
|
| 17 |
-
# Set the default system message to the friendly Python chatbot message
|
| 18 |
system_message = system_message or "You are a friendly Python code writing Chatbot. Assist with Python programming tasks, debugging, and code optimization. Provide solutions for Python-related queries, help with libraries, algorithms, and best practices, and generate clean, efficient code for various applications."
|
| 19 |
|
| 20 |
-
# Prepare the conversation history and current message
|
| 21 |
messages = [{"role": "system", "content": system_message}]
|
| 22 |
for val in history:
|
| 23 |
if val[0]:
|
|
@@ -28,8 +24,6 @@ def respond(
|
|
| 28 |
messages.append({"role": "user", "content": message})
|
| 29 |
|
| 30 |
response = ""
|
| 31 |
-
|
| 32 |
-
# Request the response from the model
|
| 33 |
for message in client.chat_completion(
|
| 34 |
messages,
|
| 35 |
max_tokens=max_tokens,
|
|
@@ -42,25 +36,32 @@ def respond(
|
|
| 42 |
yield response
|
| 43 |
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
gr.Textbox(value="You are a friendly Python code writing Chatbot. Assist with Python programming tasks, debugging, and code optimization. Provide solutions for Python-related queries, help with libraries, algorithms, and best practices, and generate clean, efficient code for various applications.", label="System message"),
|
|
|
|
| 52 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 53 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 54 |
-
gr.Slider(
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
label="Top-p (nucleus sampling)",
|
| 60 |
-
),
|
| 61 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
)
|
| 63 |
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Initialize the Inference Client
|
|
|
|
|
|
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
def respond(
|
|
|
|
| 12 |
temperature,
|
| 13 |
top_p,
|
| 14 |
):
|
|
|
|
| 15 |
system_message = system_message or "You are a friendly Python code writing Chatbot. Assist with Python programming tasks, debugging, and code optimization. Provide solutions for Python-related queries, help with libraries, algorithms, and best practices, and generate clean, efficient code for various applications."
|
| 16 |
|
|
|
|
| 17 |
messages = [{"role": "system", "content": system_message}]
|
| 18 |
for val in history:
|
| 19 |
if val[0]:
|
|
|
|
| 24 |
messages.append({"role": "user", "content": message})
|
| 25 |
|
| 26 |
response = ""
|
|
|
|
|
|
|
| 27 |
for message in client.chat_completion(
|
| 28 |
messages,
|
| 29 |
max_tokens=max_tokens,
|
|
|
|
| 36 |
yield response
|
| 37 |
|
| 38 |
|
| 39 |
+
def clear_chat():
|
| 40 |
+
return [], ""
|
| 41 |
+
|
| 42 |
+
# Chatbot Interface
|
| 43 |
+
demo = gr.Interface(
|
| 44 |
+
fn=respond,
|
| 45 |
+
inputs=[
|
| 46 |
gr.Textbox(value="You are a friendly Python code writing Chatbot. Assist with Python programming tasks, debugging, and code optimization. Provide solutions for Python-related queries, help with libraries, algorithms, and best practices, and generate clean, efficient code for various applications.", label="System message"),
|
| 47 |
+
gr.Textbox(placeholder="Enter your message here...", label="User Input", lines=2),
|
| 48 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 49 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 50 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 51 |
+
],
|
| 52 |
+
outputs=[
|
| 53 |
+
gr.Chatbot(label="Chat History"),
|
| 54 |
+
gr.Textbox(label="Current Response", placeholder="Chatbot will reply here...", interactive=False)
|
|
|
|
|
|
|
| 55 |
],
|
| 56 |
+
live=True,
|
| 57 |
+
allow_flagging="never", # Disable flagging
|
| 58 |
+
layout="vertical",
|
| 59 |
+
theme="huggingface", # Optionally, set a different theme
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# Add additional features to the interface
|
| 63 |
+
with demo:
|
| 64 |
+
gr.Button("Clear Chat", elem_id="clear-chat").click(clear_chat, outputs=["chatbot", "textbox"])
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
demo.launch()
|