update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
| 4 |
client = InferenceClient(
|
| 5 |
"HuggingFaceH4/zephyr-7b-beta",
|
| 6 |
token=os.getenv("policy")
|
| 7 |
)
|
| 8 |
|
|
|
|
| 9 |
def respond(
|
| 10 |
message,
|
| 11 |
history: list[tuple[str, str]],
|
|
@@ -15,17 +18,14 @@ def respond(
|
|
| 15 |
top_p,
|
| 16 |
):
|
| 17 |
messages = [{"role": "system", "content": system_message}]
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 24 |
-
|
| 25 |
messages.append({"role": "user", "content": message})
|
| 26 |
|
| 27 |
response = ""
|
| 28 |
-
|
| 29 |
for message in client.chat_completion(
|
| 30 |
messages,
|
| 31 |
max_tokens=max_tokens,
|
|
@@ -33,31 +33,22 @@ def respond(
|
|
| 33 |
temperature=temperature,
|
| 34 |
top_p=top_p,
|
| 35 |
):
|
| 36 |
-
token = message.choices[0].delta.content
|
| 37 |
-
|
| 38 |
response += token
|
| 39 |
yield response
|
| 40 |
|
| 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
|
| 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 os
|
| 2 |
+
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# Create client with token from HF secret
|
| 6 |
client = InferenceClient(
|
| 7 |
"HuggingFaceH4/zephyr-7b-beta",
|
| 8 |
token=os.getenv("policy")
|
| 9 |
)
|
| 10 |
|
| 11 |
+
# Define chat logic
|
| 12 |
def respond(
|
| 13 |
message,
|
| 14 |
history: list[tuple[str, str]],
|
|
|
|
| 18 |
top_p,
|
| 19 |
):
|
| 20 |
messages = [{"role": "system", "content": system_message}]
|
| 21 |
+
for user_msg, bot_msg in history:
|
| 22 |
+
if user_msg:
|
| 23 |
+
messages.append({"role": "user", "content": user_msg})
|
| 24 |
+
if bot_msg:
|
| 25 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
|
|
|
|
|
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
response = ""
|
|
|
|
| 29 |
for message in client.chat_completion(
|
| 30 |
messages,
|
| 31 |
max_tokens=max_tokens,
|
|
|
|
| 33 |
temperature=temperature,
|
| 34 |
top_p=top_p,
|
| 35 |
):
|
| 36 |
+
token = message.choices[0].delta.content or ""
|
|
|
|
| 37 |
response += token
|
| 38 |
yield response
|
| 39 |
|
| 40 |
+
# Define Gradio interface
|
|
|
|
|
|
|
|
|
|
| 41 |
demo = gr.ChatInterface(
|
| 42 |
respond,
|
| 43 |
additional_inputs=[
|
| 44 |
+
gr.Textbox(value="You are a helpful assistant.", label="System message"),
|
| 45 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 46 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 47 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
],
|
| 49 |
+
title="AI Policy Chatbot"
|
| 50 |
)
|
| 51 |
|
| 52 |
+
# Launch the app
|
| 53 |
if __name__ == "__main__":
|
| 54 |
demo.launch()
|