Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,60 +2,80 @@ 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:
|
|
|
|
| 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 |
messages = [{"role": "system", "content": system_message}]
|
| 31 |
|
| 32 |
for val in history:
|
| 33 |
-
if val[0]:
|
| 34 |
messages.append({"role": "user", "content": val[0]})
|
| 35 |
-
if val[1]:
|
| 36 |
messages.append({"role": "assistant", "content": val[1]})
|
| 37 |
|
| 38 |
messages.append({"role": "user", "content": message})
|
| 39 |
|
| 40 |
response = ""
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
"""
|
| 58 |
-
For information on how to customize the ChatInterface, peruse the gradio docs:
|
|
|
|
| 59 |
"""
|
| 60 |
demo = gr.ChatInterface(
|
| 61 |
respond,
|
|
@@ -75,6 +95,5 @@ demo = gr.ChatInterface(
|
|
| 75 |
],
|
| 76 |
)
|
| 77 |
|
| 78 |
-
|
| 79 |
if __name__ == "__main__":
|
| 80 |
-
demo.launch()
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
"""
|
| 5 |
+
For more information on `huggingface_hub` Inference API support, please check the docs:
|
| 6 |
+
https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 7 |
"""
|
| 8 |
|
| 9 |
+
def respond(message, history, token, model, system_message, max_tokens, temperature, top_p):
|
| 10 |
+
"""
|
| 11 |
+
Handle chat responses using the Hugging Face Inference API.
|
| 12 |
+
|
| 13 |
+
Parameters:
|
| 14 |
+
- message: The user's current message.
|
| 15 |
+
- history: List of previous user-assistant message pairs.
|
| 16 |
+
- token: HF API token for authentication.
|
| 17 |
+
- model: Model name to use for inference.
|
| 18 |
+
- system_message: System prompt to initialize the chat.
|
| 19 |
+
- max_tokens: Maximum number of tokens to generate.
|
| 20 |
+
- temperature: Sampling temperature.
|
| 21 |
+
- top_p: Top-p (nucleus) sampling parameter.
|
| 22 |
+
|
| 23 |
+
Yields:
|
| 24 |
+
- Incremental responses for streaming in the chat interface.
|
| 25 |
+
"""
|
| 26 |
+
# Check for missing token
|
| 27 |
+
if not token:
|
| 28 |
+
yield "Please provide an HF API Token."
|
| 29 |
+
return
|
| 30 |
+
|
| 31 |
+
# Use default model if none provided
|
| 32 |
+
if not model:
|
| 33 |
+
model = "meta-llama/Llama-3.1-8B-Instruct"
|
| 34 |
+
|
| 35 |
+
# Initialize the InferenceClient
|
| 36 |
+
try:
|
| 37 |
+
client = InferenceClient(model=model, token=token)
|
| 38 |
+
except Exception as e:
|
| 39 |
+
yield f"Error initializing client: {str(e)}"
|
| 40 |
+
return
|
| 41 |
+
|
| 42 |
+
# Build the message history, starting with the system message
|
| 43 |
messages = [{"role": "system", "content": system_message}]
|
| 44 |
|
| 45 |
for val in history:
|
| 46 |
+
if val[0]: # User message
|
| 47 |
messages.append({"role": "user", "content": val[0]})
|
| 48 |
+
if val[1]: # Assistant message
|
| 49 |
messages.append({"role": "assistant", "content": val[1]})
|
| 50 |
|
| 51 |
messages.append({"role": "user", "content": message})
|
| 52 |
|
| 53 |
response = ""
|
| 54 |
|
| 55 |
+
# Make the API call with streaming
|
| 56 |
+
try:
|
| 57 |
+
for message in client.chat_completion(
|
| 58 |
+
messages,
|
| 59 |
+
max_tokens=max_tokens,
|
| 60 |
+
stream=True,
|
| 61 |
+
temperature=temperature,
|
| 62 |
+
top_p=top_p,
|
| 63 |
+
):
|
| 64 |
+
# Check for non-empty content in the delta
|
| 65 |
+
if message.choices and message.choices[0].delta.content is not None:
|
| 66 |
+
token = message.choices[0].delta.content
|
| 67 |
+
response += token
|
| 68 |
+
yield response
|
| 69 |
+
except Exception as e:
|
| 70 |
+
yield f"Error during API call: {str(e)}"
|
| 71 |
+
|
| 72 |
+
# Define input components
|
| 73 |
+
token_input = gr.Textbox(type="password", label="HF API Token")
|
| 74 |
+
model_input = gr.Textbox(label="Model Name", value="HuggingFaceH4/zephyr-7b-beta")
|
| 75 |
|
| 76 |
"""
|
| 77 |
+
For information on how to customize the ChatInterface, peruse the gradio docs:
|
| 78 |
+
https://www.gradio.app/docs/chatinterface
|
| 79 |
"""
|
| 80 |
demo = gr.ChatInterface(
|
| 81 |
respond,
|
|
|
|
| 95 |
],
|
| 96 |
)
|
| 97 |
|
|
|
|
| 98 |
if __name__ == "__main__":
|
| 99 |
+
demo.launch()
|