Spaces:
Paused
Paused
Fix Space runtime: update SDK/deps and harden startup
Browse files- README.md +13 -12
- app.py +45 -71
- requirements.txt +2 -2
README.md
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: AgeBot
|
| 3 |
-
emoji: 💬
|
| 4 |
-
colorFrom: yellow
|
| 5 |
-
colorTo: purple
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 4.
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: AgeBot
|
| 3 |
+
emoji: 💬
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.1
|
| 8 |
+
python_version: 3.10
|
| 9 |
+
app_file: app.py
|
| 10 |
+
pinned: false
|
| 11 |
+
license: mit
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
app.py
CHANGED
|
@@ -1,71 +1,45 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
|
| 4 |
-
""
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
response += token
|
| 48 |
-
yield response
|
| 49 |
-
|
| 50 |
-
"""
|
| 51 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 52 |
-
"""
|
| 53 |
-
demo = gr.ChatInterface(
|
| 54 |
-
respond,
|
| 55 |
-
additional_inputs=[
|
| 56 |
-
gr.Textbox(value="You are a friendly Chatbot. Enter the age you want the bot to be as n = _ and choose sex as male or female. Example n = 17 and sex = male", label="System message"),
|
| 57 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 58 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 59 |
-
gr.Slider(
|
| 60 |
-
minimum=0.1,
|
| 61 |
-
maximum=1.0,
|
| 62 |
-
value=0.95,
|
| 63 |
-
step=0.05,
|
| 64 |
-
label="Top-p (nucleus sampling)",
|
| 65 |
-
),
|
| 66 |
-
],
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
if __name__ == "__main__":
|
| 71 |
-
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
|
| 4 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 5 |
+
|
| 6 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 7 |
+
messages = [{"role": "system", "content": system_message}]
|
| 8 |
+
for user_msg, assistant_msg in history:
|
| 9 |
+
if user_msg:
|
| 10 |
+
messages.append({"role": "user", "content": user_msg})
|
| 11 |
+
if assistant_msg:
|
| 12 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 13 |
+
messages.append({"role": "user", "content": message})
|
| 14 |
+
|
| 15 |
+
response = ""
|
| 16 |
+
try:
|
| 17 |
+
for chunk in client.chat_completion(
|
| 18 |
+
messages,
|
| 19 |
+
max_tokens=int(max_tokens),
|
| 20 |
+
stream=True,
|
| 21 |
+
temperature=temperature,
|
| 22 |
+
top_p=top_p,
|
| 23 |
+
):
|
| 24 |
+
token = chunk.choices[0].delta.content
|
| 25 |
+
if token:
|
| 26 |
+
response += token
|
| 27 |
+
yield response
|
| 28 |
+
except Exception as exc:
|
| 29 |
+
yield f"Model call failed: {exc}. Add HF_TOKEN in Space secrets if needed."
|
| 30 |
+
|
| 31 |
+
demo = gr.ChatInterface(
|
| 32 |
+
respond,
|
| 33 |
+
additional_inputs=[
|
| 34 |
+
gr.Textbox(
|
| 35 |
+
value="You are a friendly chatbot. Speak as if you are age n and a given sex. Example: n = 17 and sex = male",
|
| 36 |
+
label="System message",
|
| 37 |
+
),
|
| 38 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 39 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
| 40 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
| 41 |
+
],
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
| 1 |
+
gradio==4.44.1
|
| 2 |
+
huggingface_hub>=0.26.0
|