Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
import torch | |
# Load the model from Hugging Face | |
def load_model(): | |
"""Load the intent classification model""" | |
try: | |
classifier = pipeline( | |
"text-classification", | |
model="YosefA/adfluence-intent-model", | |
return_all_scores=True | |
) | |
return classifier | |
except Exception as e: | |
print(f"Error loading model: {e}") | |
return None | |
def classify_intent(comment): | |
""" | |
Classify the intent of a comment | |
Args: | |
comment (str): The input comment text | |
Returns: | |
dict: Classification results with labels and scores | |
""" | |
if not comment.strip(): | |
return "Please enter a comment to classify." | |
classifier = load_model() | |
if classifier is None: | |
return "Error: Could not load the model. Please try again later." | |
try: | |
# Get predictions | |
results = classifier(comment) | |
# Format results for display | |
formatted_results = [] | |
for result in results: | |
for item in result: | |
label = item['label'] | |
score = item['score'] | |
formatted_results.append(f"{label}: {score:.4f} ({score*100:.2f}%)") | |
return "\n".join(formatted_results) | |
except Exception as e: | |
return f"Error during classification: {str(e)}" | |
# Create the Gradio interface | |
with gr.Blocks(title="Ad Comments Intent Classifier") as demo: | |
gr.Markdown(""" | |
# π― Ad Comments Intent Classifier | |
This app classifies the intent of comments related to advertisements using the **YosefA/adfluence-intent-model**. | |
Simply enter a comment below and get the classification results with confidence scores. | |
""") | |
with gr.Row(): | |
with gr.Column(): | |
comment_input = gr.Textbox( | |
label="Comment Text", | |
placeholder="Enter your comment here...", | |
lines=3, | |
max_lines=10 | |
) | |
classify_btn = gr.Button("π Classify Intent", variant="primary") | |
with gr.Column(): | |
output = gr.Textbox( | |
label="Classification Results", | |
lines=5, | |
max_lines=10, | |
interactive=False | |
) | |
# Example inputs | |
gr.Examples( | |
examples=[ | |
["This product looks amazing! Where can I buy it?"], | |
["This is clearly a scam, don't trust it."], | |
["I love this brand, they make quality products."], | |
["The price seems too high for what you get."], | |
["Has anyone tried this? I'm curious about reviews."] | |
], | |
inputs=comment_input, | |
label="π Example Comments" | |
) | |
# Set up the event handlers | |
classify_btn.click( | |
fn=classify_intent, | |
inputs=comment_input, | |
outputs=output | |
) | |
comment_input.submit( | |
fn=classify_intent, | |
inputs=comment_input, | |
outputs=output | |
) | |
# Launch the app | |
if __name__ == "__main__": | |
demo.launch() |