Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,146 +1,123 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# pip install gradio huggingface_hub
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
try:
|
| 16 |
-
client = InferenceClient(
|
| 17 |
-
"HuggingFaceH4/zephyr-7b-beta",
|
| 18 |
-
# token=HUGGING_FACE_HUB_TOKEN # Uncomment if you want to pass token explicitly
|
| 19 |
-
)
|
| 20 |
-
print("InferenceClient initialized successfully.")
|
| 21 |
except Exception as e:
|
| 22 |
print(f"Error initializing InferenceClient: {e}")
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
def respond(
|
| 29 |
message: str,
|
| 30 |
-
history: list[tuple[str, str]],
|
| 31 |
-
system_message: str
|
| 32 |
-
max_tokens: int
|
| 33 |
-
temperature: float
|
| 34 |
-
top_p: float
|
| 35 |
):
|
| 36 |
"""
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
"""
|
| 39 |
-
# --- Client Check ---
|
| 40 |
-
if client is None:
|
| 41 |
-
yield "Error: InferenceClient could not be initialized. Please check server logs."
|
| 42 |
-
return # Stop generation if client is not available
|
| 43 |
-
|
| 44 |
-
# --- Input Validation (Basic) ---
|
| 45 |
-
if not message:
|
| 46 |
-
yield "Error: Please enter a message."
|
| 47 |
-
return
|
| 48 |
-
if not system_message:
|
| 49 |
-
system_message = "You are a helpful assistant." # Fallback system message
|
| 50 |
-
|
| 51 |
messages = [{"role": "system", "content": system_message}]
|
| 52 |
|
| 53 |
-
|
|
|
|
| 54 |
if user_msg:
|
| 55 |
messages.append({"role": "user", "content": user_msg})
|
| 56 |
-
if
|
| 57 |
-
messages.append({"role": "assistant", "content":
|
| 58 |
|
|
|
|
| 59 |
messages.append({"role": "user", "content": message})
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
try:
|
| 64 |
-
#
|
| 65 |
-
for
|
| 66 |
messages=messages,
|
| 67 |
max_tokens=max_tokens,
|
| 68 |
stream=True,
|
| 69 |
temperature=temperature,
|
| 70 |
top_p=top_p,
|
| 71 |
):
|
| 72 |
-
# Check if
|
| 73 |
-
token =
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
| 80 |
except Exception as e:
|
| 81 |
-
print(f"
|
| 82 |
-
|
| 83 |
-
yield f"An error occurred while generating the response: {e}"
|
| 84 |
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
demo = gr.ChatInterface(
|
| 88 |
respond,
|
| 89 |
-
chatbot=gr.Chatbot(
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
),
|
| 95 |
-
title="🤖 Zephyr 7B Beta Chat",
|
| 96 |
-
description="Chat with the Zephyr 7B Beta model using the Hugging Face Inference API. \nEnter your message and adjust settings below.",
|
| 97 |
examples=[
|
| 98 |
-
["Hello
|
| 99 |
-
["
|
| 100 |
-
["
|
| 101 |
-
["Write a short poem about the rain."]
|
| 102 |
],
|
| 103 |
-
cache_examples=False, # Set to True to cache example results
|
|
|
|
|
|
|
|
|
|
| 104 |
additional_inputs=[
|
| 105 |
-
gr.Textbox(
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
info="The instruction given to the chatbot to guide its behavior.",
|
| 109 |
-
),
|
| 110 |
-
gr.Slider(
|
| 111 |
-
minimum=1,
|
| 112 |
-
maximum=2048,
|
| 113 |
-
value=512, # Default max tokens
|
| 114 |
-
step=1,
|
| 115 |
-
label="Max New Tokens",
|
| 116 |
-
info="Maximum number of tokens to generate."
|
| 117 |
-
),
|
| 118 |
-
gr.Slider(
|
| 119 |
-
minimum=0.1,
|
| 120 |
-
# Max temperature adjusted: values > 1.0 often degrade quality
|
| 121 |
-
maximum=1.0,
|
| 122 |
-
value=0.7, # Default temperature
|
| 123 |
-
step=0.1,
|
| 124 |
-
label="Temperature",
|
| 125 |
-
info="Controls randomness. Lower values make output more focused, higher values make it more diverse."
|
| 126 |
-
),
|
| 127 |
gr.Slider(
|
| 128 |
minimum=0.1,
|
| 129 |
maximum=1.0,
|
| 130 |
-
value=0.95,
|
| 131 |
step=0.05,
|
| 132 |
label="Top-p (nucleus sampling)",
|
| 133 |
-
info="Considers only the most probable tokens with cumulative probability p. Helps prevent low-probability tokens."
|
| 134 |
),
|
| 135 |
],
|
| 136 |
-
|
| 137 |
)
|
| 138 |
|
| 139 |
|
| 140 |
if __name__ == "__main__":
|
| 141 |
-
|
| 142 |
-
demo.launch(
|
| 143 |
-
# share=True # Uncomment to create a temporary public link (use with caution)
|
| 144 |
-
# server_name="0.0.0.0" # Uncomment to allow access from your local network
|
| 145 |
-
# auth=("user", "password") # Optional: Add basic authentication
|
| 146 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from huggingface_hub.inference_api import InferenceApiException
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
"""
|
| 7 |
+
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
|
|
|
|
| 8 |
|
| 9 |
+
**Note:** You might need to authenticate with Hugging Face for this to work reliably.
|
| 10 |
+
Run `huggingface-cli login` in your terminal or set the HUGGING_FACE_HUB_TOKEN environment variable.
|
| 11 |
+
Alternatively, pass your token directly: InferenceClient(token="hf_YOUR_TOKEN")
|
| 12 |
+
"""
|
| 13 |
+
# Initialize the Inference Client
|
| 14 |
+
# It will try to use HUGGING_FACE_HUB_TOKEN environment variable or cached login
|
| 15 |
try:
|
| 16 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
except Exception as e:
|
| 18 |
print(f"Error initializing InferenceClient: {e}")
|
| 19 |
+
# Optionally, provide a default token if needed and available
|
| 20 |
+
# token = os.getenv("HUGGING_FACE_HUB_TOKEN")
|
| 21 |
+
# if token:
|
| 22 |
+
# client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=token)
|
| 23 |
+
# else:
|
| 24 |
+
# raise ValueError("Could not initialize InferenceClient. Ensure you are logged in or provide a token.") from e
|
| 25 |
+
# For now, let's just raise it if initialization fails fundamentally
|
| 26 |
+
raise
|
| 27 |
|
| 28 |
def respond(
|
| 29 |
message: str,
|
| 30 |
+
history: list[tuple[str | None, str | None]],
|
| 31 |
+
system_message: str,
|
| 32 |
+
max_tokens: int,
|
| 33 |
+
temperature: float,
|
| 34 |
+
top_p: float,
|
| 35 |
):
|
| 36 |
"""
|
| 37 |
+
Generates a response using the Hugging Face Inference API.
|
| 38 |
+
|
| 39 |
+
Args:
|
| 40 |
+
message: The user's input message.
|
| 41 |
+
history: A list of tuples representing the conversation history.
|
| 42 |
+
Each tuple is (user_message, bot_message).
|
| 43 |
+
system_message: The system prompt to guide the model.
|
| 44 |
+
max_tokens: The maximum number of new tokens to generate.
|
| 45 |
+
temperature: Controls randomness (higher = more random).
|
| 46 |
+
top_p: Nucleus sampling parameter.
|
| 47 |
+
|
| 48 |
+
Yields:
|
| 49 |
+
The generated response incrementally.
|
| 50 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
messages = [{"role": "system", "content": system_message}]
|
| 52 |
|
| 53 |
+
# Add conversation history
|
| 54 |
+
for user_msg, bot_msg in history:
|
| 55 |
if user_msg:
|
| 56 |
messages.append({"role": "user", "content": user_msg})
|
| 57 |
+
if bot_msg:
|
| 58 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 59 |
|
| 60 |
+
# Add the latest user message
|
| 61 |
messages.append({"role": "user", "content": message})
|
| 62 |
|
| 63 |
+
response = ""
|
|
|
|
| 64 |
try:
|
| 65 |
+
# Start streaming the response
|
| 66 |
+
for msg_chunk in client.chat_completion(
|
| 67 |
messages=messages,
|
| 68 |
max_tokens=max_tokens,
|
| 69 |
stream=True,
|
| 70 |
temperature=temperature,
|
| 71 |
top_p=top_p,
|
| 72 |
):
|
| 73 |
+
# Check if there's content in the delta
|
| 74 |
+
token = msg_chunk.choices[0].delta.content
|
| 75 |
+
if token: # Add check for empty/None token
|
| 76 |
+
response += token
|
| 77 |
+
yield response # Yield the accumulated response so far
|
| 78 |
+
|
| 79 |
+
except InferenceApiException as e:
|
| 80 |
+
print(f"Inference API Error: {e}")
|
| 81 |
+
yield f"Sorry, I encountered an error: {e}"
|
| 82 |
except Exception as e:
|
| 83 |
+
print(f"An unexpected error occurred: {e}")
|
| 84 |
+
yield f"Sorry, an unexpected error occurred: {e}"
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
+
"""
|
| 88 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 89 |
+
"""
|
| 90 |
demo = gr.ChatInterface(
|
| 91 |
respond,
|
| 92 |
+
chatbot=gr.Chatbot(height=400), # Adjust chatbot height if desired
|
| 93 |
+
textbox=gr.Textbox(placeholder="Ask me anything...", container=False, scale=7),
|
| 94 |
+
title="Zephyr 7B Beta Chat",
|
| 95 |
+
description="Chat with the Zephyr 7B Beta model using the Hugging Face Inference API.",
|
| 96 |
+
theme="soft", # Optional: Apply a theme
|
|
|
|
|
|
|
|
|
|
| 97 |
examples=[
|
| 98 |
+
["Hello!"],
|
| 99 |
+
["Explain the concept of Large Language Models in simple terms."],
|
| 100 |
+
["Write a short poem about the moon."],
|
|
|
|
| 101 |
],
|
| 102 |
+
cache_examples=False, # Set to True to cache example results
|
| 103 |
+
retry_btn="Retry",
|
| 104 |
+
undo_btn="Undo",
|
| 105 |
+
clear_btn="Clear",
|
| 106 |
additional_inputs=[
|
| 107 |
+
gr.Textbox(value="You are a friendly and helpful chatbot.", label="System message"),
|
| 108 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 109 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"), # Note: Max temp often capped lower (e.g., 1.0 or 2.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
gr.Slider(
|
| 111 |
minimum=0.1,
|
| 112 |
maximum=1.0,
|
| 113 |
+
value=0.95,
|
| 114 |
step=0.05,
|
| 115 |
label="Top-p (nucleus sampling)",
|
|
|
|
| 116 |
),
|
| 117 |
],
|
| 118 |
+
additional_inputs_accordion=gr.Accordion(label="Advanced Options", open=False), # Group additional inputs
|
| 119 |
)
|
| 120 |
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
| 123 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|