KrishanRao commited on
Commit
320c368
·
verified ·
1 Parent(s): 70b0105

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[ ]:
5
+
6
+
7
+ import gradio as gr
8
+ import requests
9
+ from bs4 import BeautifulSoup
10
+ from transformers import pipeline
11
+ import os
12
+
13
+ # Function to extract text from the URL using requests
14
+ def extract_text(url):
15
+ try:
16
+ headers = {
17
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
18
+ 'Accept-Language': 'en-US,en;q=0.9',
19
+ 'Accept-Encoding': 'gzip, deflate, br',
20
+ 'Connection': 'keep-alive',
21
+ 'Referer': 'https://www.mansionglobal.com/' # Adding referer might help bypass restrictions
22
+ }
23
+
24
+ # Sending GET request with headers
25
+ response = requests.get(url, headers=headers)
26
+
27
+ # Check if the response is successful
28
+ response.raise_for_status() # Raise an error for bad status codes
29
+
30
+ # Parse HTML and extract text
31
+ soup = BeautifulSoup(response.text, "html.parser")
32
+ text = ' '.join(soup.stripped_strings)
33
+ return text
34
+ except requests.exceptions.RequestException as e:
35
+ return f"Error extracting text from URL: {str(e)}"
36
+
37
+ # Load Hugging Face model (for extracting named entities or QA)
38
+ try:
39
+ ner_model = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-english")
40
+ except Exception as e:
41
+ ner_model = None
42
+ print(f"Error loading model: {str(e)}")
43
+
44
+ # Function to extract information using Hugging Face model
45
+ def extract_info_with_model(text):
46
+ if not ner_model:
47
+ return {
48
+ "Keytags": "Model loading failed.",
49
+ "Amenities": "Model loading failed.",
50
+ "Facilities": "Model loading failed.",
51
+ "Seller Name": "Model loading failed.",
52
+ "Location Details": "Model loading failed."
53
+ }
54
+
55
+ try:
56
+ # Apply named entity recognition (NER) to extract entities from the text
57
+ ner_results = ner_model(text)
58
+
59
+ # Initialize variables
60
+ keytags = []
61
+ seller_name = ""
62
+ location_details = ""
63
+ amenities = ""
64
+ facilities = ""
65
+
66
+ # Search for relevant named entities
67
+ for entity in ner_results:
68
+ if entity['label'] == 'ORG':
69
+ keytags.append(entity['word']) # Example: Company or key term (this can be changed)
70
+ elif entity['label'] == 'PERSON':
71
+ seller_name = entity['word'] # If a person is mentioned, consider it the seller name
72
+ elif entity['label'] == 'GPE':
73
+ location_details = entity['word'] # Geopolitical entity as location
74
+
75
+ # For amenities and facilities, you can modify the logic or use additional models (e.g., question-answering models)
76
+ amenities = "No amenities found" # Placeholder for the amenities
77
+ facilities = "No facilities found" # Placeholder for the facilities
78
+
79
+ return {
80
+ "Keytags": ", ".join(keytags) if keytags else "No keytags found",
81
+ "Amenities": amenities,
82
+ "Facilities": facilities,
83
+ "Seller Name": seller_name if seller_name else "No seller name found",
84
+ "Location Details": location_details if location_details else "No location details found"
85
+ }
86
+ except Exception as e:
87
+ return {
88
+ "Keytags": f"Error processing text: {str(e)}",
89
+ "Amenities": f"Error processing text: {str(e)}",
90
+ "Facilities": f"Error processing text: {str(e)}",
91
+ "Seller Name": f"Error processing text: {str(e)}",
92
+ "Location Details": f"Error processing text: {str(e)}"
93
+ }
94
+
95
+ # Function to combine the extraction process (from URL + model processing)
96
+ def get_info(url):
97
+ text = extract_text(url)
98
+ if "Error" in text:
99
+ return text, text, text, text, text # Return the error message for all outputs
100
+
101
+ extracted_info = extract_info_with_model(text)
102
+
103
+ return (
104
+ extracted_info["Keytags"],
105
+ extracted_info["Amenities"],
106
+ extracted_info["Facilities"],
107
+ extracted_info["Seller Name"],
108
+ extracted_info["Location Details"]
109
+ )
110
+
111
+ # Gradio Interface to allow user input and display output
112
+ demo = gr.Interface(
113
+ fn=get_info,
114
+ inputs="text", # Input is a URL
115
+ outputs=["text", "text", "text", "text", "text"], # Outputs for each field (Keytags, Amenities, etc.)
116
+ title="Real Estate Info Extractor",
117
+ description="Extract Keytags, Amenities, Facilities, Seller Name, and Location Details from a real estate article URL."
118
+ )
119
+
120
+ if __name__ == "__main__":
121
+ demo.launch(show_api=False)
122
+