Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,87 +1,42 @@
|
|
| 1 |
-
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
|
| 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 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 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 |
-
outputs=gr.Textbox(
|
| 81 |
-
label="13-Character Code",
|
| 82 |
-
placeholder="The generated 13-character code will appear here",
|
| 83 |
-
),
|
| 84 |
)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 1 |
+
# app.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
import torch
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
# Load the model and tokenizer
|
| 9 |
+
model_name = "huggingface/zephyr-7b-beta" # Replace this with the actual model path
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 12 |
+
|
| 13 |
+
def get_category_scores(text):
|
| 14 |
+
# Tokenize input text
|
| 15 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 16 |
+
|
| 17 |
+
# Generate model outputs
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
outputs = model(**inputs)
|
| 20 |
+
|
| 21 |
+
# Process model output to extract labels
|
| 22 |
+
# For demonstration, we'll mock the extraction with random values
|
| 23 |
+
# Replace this part with actual logic based on model output
|
| 24 |
+
|
| 25 |
+
# Example random values for demonstration (these should be extracted from model)
|
| 26 |
+
scores = np.random.randint(0, 10, size=13) # Replace with actual logic
|
| 27 |
+
|
| 28 |
+
# Convert scores to 13-digit code
|
| 29 |
+
result_code = ''.join(map(str, scores))
|
| 30 |
+
return result_code
|
| 31 |
+
|
| 32 |
+
# Define the Gradio interface
|
| 33 |
+
iface = gr.Interface(
|
| 34 |
+
fn=get_category_scores,
|
| 35 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|
| 36 |
+
outputs="text",
|
| 37 |
+
title="Text Labeling",
|
| 38 |
+
description="Label text data based on emotion temperature and categories. Returns a 13-digit code."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
+
# Launch the interface
|
| 42 |
+
iface.launch()
|