Awaisccw11 commited on
Commit
b363eeb
·
verified ·
1 Parent(s): ed26950

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import tkinter as tk # Not used in Gradio app, but kept for context if user reverts
4
+ from tkinter import filedialog, messagebox # Not used in Gradio app
5
+ import requests # Still imported, but not used for model inference
6
+ import time
7
+
8
+ # --- Hugging Face Transformers Imports ---
9
+ # You will need to install this library: pip install transformers torch gradio
10
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
11
+
12
+ # --- Gradio Imports ---
13
+ import gradio as gr
14
+
15
+ # CHANGED: Switched to yeniguno/bert-uncased-intent-classification for intent-based classification
16
+ print("Loading local intent classification model (yeniguno/bert-uncased-intent-classification)...")
17
+ tokenizer_intent = AutoTokenizer.from_pretrained("yeniguno/bert-uncased-intent-classification")
18
+ model_intent = AutoModelForSequenceClassification.from_pretrained("yeniguno/bert-uncased-intent-classification")
19
+ # Use a text-classification pipeline as the model is for text classification
20
+ classification_pipeline = pipeline("text-classification", model=model_intent, tokenizer=tokenizer_intent)
21
+ print("Intent classification model loaded successfully.")
22
+
23
+
24
+ def classify_message_with_ai(message_content):
25
+ """
26
+ Classifies a message as 'buyer' or 'seller' using the loaded local intent classification model.
27
+
28
+ Args:
29
+ message_content (str): The text content of the message to classify.
30
+
31
+ Returns:
32
+ str: 'buyer' or 'seller' based on intent mapping, or 'unclassified'.
33
+ """
34
+ if not message_content:
35
+ return "Please provide some text to classify."
36
+
37
+ # Use the loaded intent classification pipeline
38
+ results = classification_pipeline(message_content)
39
+ # The result is a list of dictionaries, e.g., [{'label': 'product_inquiry', 'score': 0.99}]
40
+ intent = results[0]['label'].lower()
41
+ score = results[0]['score']
42
+
43
+ # --- Intent to Buyer/Seller Mapping Logic (CRITICAL: REVIEW AND ADJUST THIS) ---
44
+ # This is a **placeholder** mapping. You MUST define how intents map to buyer/seller roles
45
+ # based on the characteristics of your actual messages and the specific labels
46
+ # output by the 'yeniguno/bert-uncased-intent-classification' model.
47
+
48
+ # Example common intents and their likely roles (as defined in previous version):
49
+ buyer_intents = [
50
+ 'information_seeking', 'product_inquiry', 'complaint', 'order_status',
51
+ 'price_inquiry', 'availability_check', 'making_offer', 'general_query' # Added general_query as common buyer intent
52
+ ]
53
+ seller_intents = [
54
+ 'product_offering', 'promotion', 'transaction_confirmation',
55
+ 'providing_details', 'listing_prices', 'delivery_update', 'greeting' # Added greeting as common seller intent
56
+ ]
57
+
58
+ classification_result = "unclassified"
59
+ if intent in buyer_intents:
60
+ classification_result = 'buyer'
61
+ elif intent in seller_intents:
62
+ classification_result = 'seller'
63
+
64
+ # Return a more descriptive string for the Gradio interface
65
+ return f"Detected Intent: {intent.replace('_', ' ').title()} (Score: {score:.2f})\nClassification: {classification_result.upper()}"
66
+
67
+
68
+ # --- Gradio Interface ---
69
+ # Define the Gradio interface components
70
+ input_text = gr.Textbox(lines=5, label="Enter message text here:")
71
+ output_text = gr.Textbox(label="Classification Result")
72
+
73
+ # Create the Gradio interface
74
+ gr.Interface(
75
+ fn=classify_message_with_ai,
76
+ inputs=input_text,
77
+ outputs=output_text,
78
+ title="Buyer/Seller Message Classifier",
79
+ 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!"
80
+ ).launch()
81
+
82
+ # The `organize_messages_by_type` function is removed as it's for local file system operations.
83
+ # You would not typically run a Gradio app directly from __main__ like the old script.
84
+ # When deployed to Hugging Face Spaces, the `app.py` or `run.py` file would simply contain
85
+ # the Gradio interface definition and its `.launch()` call.