Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,26 +4,40 @@ from huggingface_hub import InferenceClient
|
|
| 4 |
# Initialize the inference client with the model
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
history: list[tuple[str, str]],
|
| 10 |
-
system_message,
|
| 11 |
max_tokens,
|
| 12 |
temperature,
|
| 13 |
-
top_p
|
| 14 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Prepare the messages, starting with the system message
|
| 16 |
messages = [{"role": "system", "content": system_message}]
|
| 17 |
|
| 18 |
-
# Add the
|
| 19 |
-
|
| 20 |
-
if user_message:
|
| 21 |
-
messages.append({"role": "user", "content": user_message})
|
| 22 |
-
if assistant_response:
|
| 23 |
-
messages.append({"role": "assistant", "content": assistant_response})
|
| 24 |
-
|
| 25 |
-
# Add the current user message
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
response = ""
|
| 29 |
|
|
@@ -44,43 +58,14 @@ def respond(
|
|
| 44 |
else:
|
| 45 |
return "Invalid response format"
|
| 46 |
|
| 47 |
-
# Instructions for the model
|
| 48 |
-
instructions = (
|
| 49 |
-
"You are tasked with labeling text data based on both emotion temperature and text type categories. "
|
| 50 |
-
"The final output must be a 13-character code that consists of the following structure:\n\n"
|
| 51 |
-
" 0 index : Emotion Temperature code\n"
|
| 52 |
-
" 1 index : Informative\n"
|
| 53 |
-
" 2 index : Hate\n"
|
| 54 |
-
" 3 index : Toxic\n"
|
| 55 |
-
" 4 index : Appreciation\n"
|
| 56 |
-
" 5 index : Constructive Criticism\n"
|
| 57 |
-
" 6 index : Genuine Questions\n"
|
| 58 |
-
" 7 index : Advice/Suggestions\n"
|
| 59 |
-
" 8 index : Requests\n"
|
| 60 |
-
" 9 index : Spam\n"
|
| 61 |
-
" 10 index : Off-Topic\n"
|
| 62 |
-
" 11 index : Engagement Boosters\n"
|
| 63 |
-
" 12 index : Neutral/General\n\n"
|
| 64 |
-
"Every index should have a number between 0-9. 0 means not applicable, 4 means normal, 9 means high. "
|
| 65 |
-
"Choose appropriate numbers to showcase how much each category is related to the text input.\n\n"
|
| 66 |
-
"Example:\n"
|
| 67 |
-
"For the text 'I love your videos but still something is missing':\n"
|
| 68 |
-
" - Emotion: Cold and Warm (CW)\n"
|
| 69 |
-
" - Types Detected: 2 (Appreciation), 3 (Constructive Criticism), 5 (Advice/Suggestions)\n"
|
| 70 |
-
" - Special Categories: Neutral/General (set the 10th digit to 1), no Hate\n\n"
|
| 71 |
-
"The output would be: CW02305000010\n\n"
|
| 72 |
-
"Output Format:\n"
|
| 73 |
-
"Always return a 13-character code following this structure."
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
# Create the Gradio interface
|
| 77 |
-
demo = gr.
|
| 78 |
-
|
| 79 |
-
|
| 80 |
gr.Textbox(
|
| 81 |
-
|
| 82 |
-
label="
|
| 83 |
-
|
| 84 |
),
|
| 85 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 86 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
|
@@ -92,6 +77,10 @@ demo = gr.ChatInterface(
|
|
| 92 |
label="Top-p (nucleus sampling)",
|
| 93 |
),
|
| 94 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
|
|
|
| 4 |
# Initialize the inference client with the model
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
+
def classify_text(
|
| 8 |
+
text,
|
|
|
|
|
|
|
| 9 |
max_tokens,
|
| 10 |
temperature,
|
| 11 |
+
top_p
|
| 12 |
):
|
| 13 |
+
# Define system message with instructions for the model
|
| 14 |
+
system_message = (
|
| 15 |
+
"You are tasked with labeling text data based on both emotion temperature and text type categories. "
|
| 16 |
+
"The final output must be a 13-character code that consists of the following structure:\n\n"
|
| 17 |
+
" 0 index : Emotion Temperature code\n"
|
| 18 |
+
" 1 index : Informative\n"
|
| 19 |
+
" 2 index : Hate\n"
|
| 20 |
+
" 3 index : Toxic\n"
|
| 21 |
+
" 4 index : Appreciation\n"
|
| 22 |
+
" 5 index : Constructive Criticism\n"
|
| 23 |
+
" 6 index : Genuine Questions\n"
|
| 24 |
+
" 7 index : Advice/Suggestions\n"
|
| 25 |
+
" 8 index : Requests\n"
|
| 26 |
+
" 9 index : Spam\n"
|
| 27 |
+
" 10 index : Off-Topic\n"
|
| 28 |
+
" 11 index : Engagement Boosters\n"
|
| 29 |
+
" 12 index : Neutral/General\n\n"
|
| 30 |
+
"Every index should have a number between 0-9. 0 means not applicable, 4 means normal, 9 means high. "
|
| 31 |
+
"Choose appropriate numbers to showcase how much each category is related to the text input.\n\n"
|
| 32 |
+
"Output Format:\n"
|
| 33 |
+
"Always return a 13-character code following this structure."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
# Prepare the messages, starting with the system message
|
| 37 |
messages = [{"role": "system", "content": system_message}]
|
| 38 |
|
| 39 |
+
# Add the user message
|
| 40 |
+
messages.append({"role": "user", "content": text})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
response = ""
|
| 43 |
|
|
|
|
| 58 |
else:
|
| 59 |
return "Invalid response format"
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# Create the Gradio interface
|
| 62 |
+
demo = gr.Interface(
|
| 63 |
+
fn=classify_text,
|
| 64 |
+
inputs=[
|
| 65 |
gr.Textbox(
|
| 66 |
+
lines=5,
|
| 67 |
+
label="Input Comment",
|
| 68 |
+
placeholder="Enter a comment to classify",
|
| 69 |
),
|
| 70 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 71 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
|
|
|
| 77 |
label="Top-p (nucleus sampling)",
|
| 78 |
),
|
| 79 |
],
|
| 80 |
+
outputs=gr.Textbox(
|
| 81 |
+
label="13-Character Code",
|
| 82 |
+
placeholder="The generated 13-character code will appear here",
|
| 83 |
+
),
|
| 84 |
)
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|