Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,42 @@
|
|
| 1 |
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: 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 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
|
|
|
| 28 |
response = ""
|
| 29 |
|
|
|
|
| 30 |
for message in client.chat_completion(
|
| 31 |
messages,
|
| 32 |
max_tokens=max_tokens,
|
|
@@ -34,15 +44,12 @@ def respond(
|
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
|
|
|
| 37 |
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
System Prompt Modification for NLPToolkit Agent
|
| 45 |
-
"""
|
| 46 |
default_system_message = (
|
| 47 |
"You are NLPToolkit Agent, an advanced natural language processing assistant. "
|
| 48 |
"You specialize in tasks such as text summarization, sentiment analysis, text classification, "
|
|
@@ -50,39 +57,49 @@ default_system_message = (
|
|
| 50 |
"Assist users with clear, concise, and actionable outputs."
|
| 51 |
)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
"""
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
# Run the
|
| 88 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Hugging Face client initialization
|
|
|
|
|
|
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
+
# Function to handle NLP responses and interaction with the model
|
| 8 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 9 |
+
"""
|
| 10 |
+
Function to handle user message and generate a response using the NLP model.
|
| 11 |
+
|
| 12 |
+
Parameters:
|
| 13 |
+
message (str): User's current message/input.
|
| 14 |
+
history (list): List of tuples representing conversation history (user's and assistant's messages).
|
| 15 |
+
system_message (str): System-level instructions to the assistant to guide its responses.
|
| 16 |
+
max_tokens (int): Maximum number of tokens to generate in the response.
|
| 17 |
+
temperature (float): Degree of randomness in the response generation.
|
| 18 |
+
top_p (float): Controls the diversity of the response using nucleus sampling.
|
| 19 |
+
|
| 20 |
+
Yields:
|
| 21 |
+
str: Streamed response as tokens are generated.
|
| 22 |
+
"""
|
| 23 |
+
# Prepare the message for the assistant, including system-level instructions and history.
|
| 24 |
messages = [{"role": "system", "content": system_message}]
|
| 25 |
+
|
| 26 |
+
# Loop through the history and add past conversation to the messages
|
| 27 |
+
for user_message, assistant_message in history:
|
| 28 |
+
if user_message:
|
| 29 |
+
messages.append({"role": "user", "content": user_message})
|
| 30 |
+
if assistant_message:
|
| 31 |
+
messages.append({"role": "assistant", "content": assistant_message})
|
| 32 |
+
|
| 33 |
+
# Append the current user message to the conversation
|
| 34 |
messages.append({"role": "user", "content": message})
|
| 35 |
|
| 36 |
+
# Initialize the response variable
|
| 37 |
response = ""
|
| 38 |
|
| 39 |
+
# Get the response stream from the Hugging Face model
|
| 40 |
for message in client.chat_completion(
|
| 41 |
messages,
|
| 42 |
max_tokens=max_tokens,
|
|
|
|
| 44 |
temperature=temperature,
|
| 45 |
top_p=top_p,
|
| 46 |
):
|
| 47 |
+
# Extract the token content and append it to the response
|
| 48 |
token = message.choices[0].delta.content
|
|
|
|
| 49 |
response += token
|
| 50 |
yield response
|
| 51 |
|
| 52 |
+
# System prompt to guide the assistant's behavior
|
|
|
|
|
|
|
|
|
|
| 53 |
default_system_message = (
|
| 54 |
"You are NLPToolkit Agent, an advanced natural language processing assistant. "
|
| 55 |
"You specialize in tasks such as text summarization, sentiment analysis, text classification, "
|
|
|
|
| 57 |
"Assist users with clear, concise, and actionable outputs."
|
| 58 |
)
|
| 59 |
|
| 60 |
+
# Create the Gradio interface for user interaction
|
| 61 |
+
def create_interface():
|
| 62 |
+
"""
|
| 63 |
+
Create and return a Gradio interface for the NLPToolkit Agent with customizable parameters.
|
| 64 |
+
|
| 65 |
+
Parameters:
|
| 66 |
+
None
|
| 67 |
+
|
| 68 |
+
Returns:
|
| 69 |
+
gr.Interface: The Gradio interface object.
|
| 70 |
+
"""
|
| 71 |
+
return gr.ChatInterface(
|
| 72 |
+
respond,
|
| 73 |
+
additional_inputs=[
|
| 74 |
+
gr.Textbox(
|
| 75 |
+
value=default_system_message,
|
| 76 |
+
label="System Message"
|
| 77 |
+
),
|
| 78 |
+
gr.Slider(
|
| 79 |
+
minimum=1,
|
| 80 |
+
maximum=2048,
|
| 81 |
+
value=512,
|
| 82 |
+
step=1,
|
| 83 |
+
label="Max New Tokens"
|
| 84 |
+
),
|
| 85 |
+
gr.Slider(
|
| 86 |
+
minimum=0.1,
|
| 87 |
+
maximum=4.0,
|
| 88 |
+
value=0.7,
|
| 89 |
+
step=0.1,
|
| 90 |
+
label="Temperature"
|
| 91 |
+
),
|
| 92 |
+
gr.Slider(
|
| 93 |
+
minimum=0.1,
|
| 94 |
+
maximum=1.0,
|
| 95 |
+
value=0.95,
|
| 96 |
+
step=0.05,
|
| 97 |
+
label="Top-p (Nucleus Sampling)"
|
| 98 |
+
),
|
| 99 |
+
],
|
| 100 |
+
)
|
| 101 |
|
| 102 |
+
# Run the Gradio interface
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
demo = create_interface()
|
| 105 |
+
demo.launch()
|