Spaces:
Sleeping
Sleeping
File size: 4,007 Bytes
b363eeb |
1 2 3 4 5 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 31 32 33 34 35 36 37 38 39 40 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 81 82 83 84 85 86 |
import os
import shutil
import tkinter as tk # Not used in Gradio app, but kept for context if user reverts
from tkinter import filedialog, messagebox # Not used in Gradio app
import requests # Still imported, but not used for model inference
import time
# --- Hugging Face Transformers Imports ---
# You will need to install this library: pip install transformers torch gradio
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
# --- Gradio Imports ---
import gradio as gr
# CHANGED: Switched to yeniguno/bert-uncased-intent-classification for intent-based classification
print("Loading local intent classification model (yeniguno/bert-uncased-intent-classification)...")
tokenizer_intent = AutoTokenizer.from_pretrained("yeniguno/bert-uncased-intent-classification")
model_intent = AutoModelForSequenceClassification.from_pretrained("yeniguno/bert-uncased-intent-classification")
# Use a text-classification pipeline as the model is for text classification
classification_pipeline = pipeline("text-classification", model=model_intent, tokenizer=tokenizer_intent)
print("Intent classification model loaded successfully.")
def classify_message_with_ai(message_content):
"""
Classifies a message as 'buyer' or 'seller' using the loaded local intent classification model.
Args:
message_content (str): The text content of the message to classify.
Returns:
str: 'buyer' or 'seller' based on intent mapping, or 'unclassified'.
"""
if not message_content:
return "Please provide some text to classify."
# Use the loaded intent classification pipeline
results = classification_pipeline(message_content)
# The result is a list of dictionaries, e.g., [{'label': 'product_inquiry', 'score': 0.99}]
intent = results[0]['label'].lower()
score = results[0]['score']
# --- Intent to Buyer/Seller Mapping Logic (CRITICAL: REVIEW AND ADJUST THIS) ---
# This is a **placeholder** mapping. You MUST define how intents map to buyer/seller roles
# based on the characteristics of your actual messages and the specific labels
# output by the 'yeniguno/bert-uncased-intent-classification' model.
# Example common intents and their likely roles (as defined in previous version):
buyer_intents = [
'information_seeking', 'product_inquiry', 'complaint', 'order_status',
'price_inquiry', 'availability_check', 'making_offer', 'general_query' # Added general_query as common buyer intent
]
seller_intents = [
'product_offering', 'promotion', 'transaction_confirmation',
'providing_details', 'listing_prices', 'delivery_update', 'greeting' # Added greeting as common seller intent
]
classification_result = "unclassified"
if intent in buyer_intents:
classification_result = 'buyer'
elif intent in seller_intents:
classification_result = 'seller'
# Return a more descriptive string for the Gradio interface
return f"Detected Intent: {intent.replace('_', ' ').title()} (Score: {score:.2f})\nClassification: {classification_result.upper()}"
# --- Gradio Interface ---
# Define the Gradio interface components
input_text = gr.Textbox(lines=5, label="Enter message text here:")
output_text = gr.Textbox(label="Classification Result")
# Create the Gradio interface
gr.Interface(
fn=classify_message_with_ai,
inputs=input_text,
outputs=output_text,
title="Buyer/Seller Message Classifier",
description="Enter a message to classify it as 'Buyer' or 'Seller' based on detected intent. Remember to adjust the mapping logic in the code for best results!"
).launch()
# The `organize_messages_by_type` function is removed as it's for local file system operations.
# You would not typically run a Gradio app directly from __main__ like the old script.
# When deployed to Hugging Face Spaces, the `app.py` or `run.py` file would simply contain
# the Gradio interface definition and its `.launch()` call.
|