Rename app.py to src/app.py
Browse files- app.py → src/app.py +34 -34
app.py → src/app.py
RENAMED
|
@@ -1,20 +1,11 @@
|
|
| 1 |
-
import
|
| 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 val in history:
|
|
@@ -27,6 +18,7 @@ def respond(
|
|
| 27 |
|
| 28 |
response = ""
|
| 29 |
|
|
|
|
| 30 |
for message in client.chat_completion(
|
| 31 |
messages,
|
| 32 |
max_tokens=max_tokens,
|
|
@@ -35,30 +27,38 @@ def respond(
|
|
| 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 |
-
respond,
|
| 48 |
-
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Initialize the Inference client with the model
|
|
|
|
|
|
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
+
# Function to generate a response
|
| 8 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
messages = [{"role": "system", "content": system_message}]
|
| 10 |
|
| 11 |
for val in history:
|
|
|
|
| 18 |
|
| 19 |
response = ""
|
| 20 |
|
| 21 |
+
# Make the API call and stream the response
|
| 22 |
for message in client.chat_completion(
|
| 23 |
messages,
|
| 24 |
max_tokens=max_tokens,
|
|
|
|
| 27 |
top_p=top_p,
|
| 28 |
):
|
| 29 |
token = message.choices[0].delta.content
|
|
|
|
| 30 |
response += token
|
| 31 |
yield response
|
| 32 |
|
| 33 |
+
# Streamlit app layout
|
| 34 |
+
st.title("Zephyr Chatbot")
|
| 35 |
+
|
| 36 |
+
# Textbox for user input
|
| 37 |
+
user_message = st.text_input("Your message:")
|
| 38 |
+
|
| 39 |
+
# Text area for displaying chat history
|
| 40 |
+
history = st.session_state.get("history", [])
|
| 41 |
+
|
| 42 |
+
# System message (initialization)
|
| 43 |
+
system_message = st.text_area("System message", value="You are a friendly Chatbot.")
|
| 44 |
+
|
| 45 |
+
# Sliders for max tokens, temperature, and top-p
|
| 46 |
+
max_tokens = st.slider("Max new tokens", min_value=1, max_value=2048, value=512, step=1)
|
| 47 |
+
temperature = st.slider("Temperature", min_value=0.1, max_value=4.0, value=0.7, step=0.1)
|
| 48 |
+
top_p = st.slider("Top-p (nucleus sampling)", min_value=0.1, max_value=1.0, value=0.95, step=0.05)
|
| 49 |
|
| 50 |
+
# Button to send the message
|
| 51 |
+
if st.button("Send"):
|
| 52 |
+
# Get the response from the model
|
| 53 |
+
response_text = ""
|
| 54 |
+
for text in respond(user_message, history, system_message, max_tokens, temperature, top_p):
|
| 55 |
+
response_text = text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
# Update chat history in session state
|
| 58 |
+
history.append((user_message, response_text))
|
| 59 |
+
st.session_state["history"] = history
|
| 60 |
|
| 61 |
+
# Display chat history
|
| 62 |
+
for user_msg, assistant_msg in history:
|
| 63 |
+
st.write(f"**You:** {user_msg}")
|
| 64 |
+
st.write(f"**Bot:** {assistant_msg}")
|