MuneebAbro commited on
Commit
4734d1a
Β·
verified Β·
1 Parent(s): f6186ca

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import re
4
+
5
+ # Load your fine-tuned model
6
+ try:
7
+ ner_pipeline = pipeline("ner",
8
+ model="MuneebAbro/ecommerce-ner-model",
9
+ aggregation_strategy="simple")
10
+ model_loaded = True
11
+ except:
12
+ # Fallback to a general NER model if yours isn't ready
13
+ ner_pipeline = pipeline("ner",
14
+ model="dbmdz/bert-large-cased-finetuned-conll03-english",
15
+ aggregation_strategy="simple")
16
+ model_loaded = False
17
+
18
+ def extract_product_info(text):
19
+ """Extract product information and format results"""
20
+ if not text.strip():
21
+ return "Please enter some text!"
22
+
23
+ try:
24
+ # Enhanced regex patterns for better extraction
25
+ result = {
26
+ "product_name": "",
27
+ "brand": "",
28
+ "price": "",
29
+ "quantities": []
30
+ }
31
+
32
+ # Extract brand
33
+ brand_patterns = [
34
+ r'\b(Samsung|Apple|iPhone|Google|OnePlus|Xiaomi|Huawei|Sony|LG|Dell|HP|Lenovo|Microsoft|Nintendo)\b'
35
+ ]
36
+ for pattern in brand_patterns:
37
+ match = re.search(pattern, text, re.IGNORECASE)
38
+ if match:
39
+ result["brand"] = match.group(1)
40
+ break
41
+
42
+ # Extract product name
43
+ product_patterns = [
44
+ r'(Galaxy\s+\w+(?:\s+\w+)?)',
45
+ r'(iPhone\s+\d+(?:\s+\w+)?)',
46
+ r'(Pixel\s+\d+(?:\s+\w+)?)',
47
+ r'(\w+\s+\d+(?:\s+\w+)?)'
48
+ ]
49
+ for pattern in product_patterns:
50
+ match = re.search(pattern, text)
51
+ if match:
52
+ result["product_name"] = match.group(1).strip()
53
+ break
54
+
55
+ # Extract price
56
+ price_patterns = [
57
+ r'Price\s*:?\s*\$(\d+(?:,\d{3})*(?:\.\d{2})?)',
58
+ r'\$(\d+(?:,\d{3})*(?:\.\d{2})?)'
59
+ ]
60
+ for pattern in price_patterns:
61
+ match = re.search(pattern, text, re.IGNORECASE)
62
+ if match:
63
+ result["price"] = f"${match.group(1)}"
64
+ break
65
+
66
+ # Extract quantities
67
+ quantity_matches = re.findall(r'(\d+(?:GB|TB|MB|RAM|Storage))', text, re.IGNORECASE)
68
+ result["quantities"] = [q for q in quantity_matches if q]
69
+
70
+ # Try NER model too
71
+ if model_loaded:
72
+ ner_results = ner_pipeline(text)
73
+ ner_info = "\nπŸ€– **NER Model Results:**\n"
74
+ for entity in ner_results:
75
+ if entity.get('score', 0) > 0.3:
76
+ ner_info += f"- {entity['word']} β†’ {entity['entity_group']} ({entity['score']:.2f})\n"
77
+ else:
78
+ ner_info = "\nπŸ€– **NER Model:** Using fallback model\n"
79
+
80
+ # Format output
81
+ output = f"""
82
+ 🏷️ **Brand:** {result['brand'] or 'Not found'}
83
+ πŸ“± **Product:** {result['product_name'] or 'Not found'}
84
+ πŸ’° **Price:** {result['price'] or 'Not found'}
85
+ πŸ“Š **Quantities:** {', '.join(result['quantities']) or 'Not found'}
86
+
87
+ {ner_info}
88
+ """
89
+
90
+ return output
91
+
92
+ except Exception as e:
93
+ return f"❌ Error: {str(e)}"
94
+
95
+ # Create interface
96
+ demo = gr.Interface(
97
+ fn=extract_product_info,
98
+ inputs=gr.Textbox(
99
+ label="Product Description",
100
+ placeholder="Enter product description here...",
101
+ lines=3,
102
+ value="Samsung Galaxy S23 Ultra, 12GB RAM, 256GB Storage, Price: $1199."
103
+ ),
104
+ outputs=gr.Textbox(
105
+ label="Extracted Information",
106
+ lines=12
107
+ ),
108
+ title="πŸ›’ E-commerce Product Information Extractor",
109
+ description="Extract product names, brands, prices, and quantities from product descriptions using fine-tuned NER model.",
110
+ examples=[
111
+ ["Samsung Galaxy S23 Ultra, 12GB RAM, 256GB Storage, Price: $1199."],
112
+ ["iPhone 14 Pro Max 256GB $1299 Apple"],
113
+ ["Google Pixel 7 Pro 12GB RAM $899"],
114
+ ["Sony WH-1000XM4 wireless headphones $349"],
115
+ ["Dell XPS 13 laptop 16GB DDR4 $1299"]
116
+ ],
117
+ theme="default",
118
+ allow_flagging="never"
119
+ )
120
+
121
+ if __name__ == "__main__":
122
+ demo.launch()