KrishanRao commited on
Commit
a642960
·
verified ·
1 Parent(s): 7a8a1f6

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ }
22
+ # Sending GET request with headers
23
+ response = requests.get(url, headers=headers)
24
+
25
+ # Check if the response is successful
26
+ response.raise_for_status() # Raise an error for bad status codes
27
+
28
+ # Parse HTML and extract text
29
+ soup = BeautifulSoup(response.text, "html.parser")
30
+ text = ' '.join(soup.stripped_strings)
31
+ return text
32
+ except requests.exceptions.RequestException as e:
33
+ return f"Error extracting text from URL: {str(e)}"
34
+
35
+ # Load Hugging Face model (for extracting named entities or QA)
36
+ try:
37
+ ner_model = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-english")
38
+ except Exception as e:
39
+ ner_model = None
40
+ print(f"Error loading model: {str(e)}")
41
+
42
+ # Function to extract information using Hugging Face model
43
+ def extract_info_with_model(text):
44
+ if not ner_model:
45
+ return {
46
+ "Keytags": "Model loading failed.",
47
+ "Amenities": "Model loading failed.",
48
+ "Facilities": "Model loading failed.",
49
+ "Seller Name": "Model loading failed.",
50
+ "Location Details": "Model loading failed."
51
+ }
52
+
53
+ try:
54
+ # Apply named entity recognition (NER) to extract entities from the text
55
+ ner_results = ner_model(text)
56
+
57
+ # Initialize variables
58
+ keytags = []
59
+ seller_name = ""
60
+ location_details = ""
61
+ amenities = ""
62
+ facilities = ""
63
+
64
+ # Search for relevant named entities
65
+ for entity in ner_results:
66
+ if entity['label'] == 'ORG':
67
+ keytags.append(entity['word']) # Example: Company or key term (this can be changed)
68
+ elif entity['label'] == 'PERSON':
69
+ seller_name = entity['word'] # If a person is mentioned, consider it the seller name
70
+ elif entity['label'] == 'GPE':
71
+ location_details = entity['word'] # Geopolitical entity as location
72
+
73
+ # For amenities and facilities, you can modify the logic or use additional models (e.g., question-answering models)
74
+ amenities = "No amenities found" # Placeholder for the amenities
75
+ facilities = "No facilities found" # Placeholder for the facilities
76
+
77
+ return {
78
+ "Keytags": ", ".join(keytags) if keytags else "No keytags found",
79
+ "Amenities": amenities,
80
+ "Facilities": facilities,
81
+ "Seller Name": seller_name if seller_name else "No seller name found",
82
+ "Location Details": location_details if location_details else "No location details found"
83
+ }
84
+ except Exception as e:
85
+ return {
86
+ "Keytags": f"Error processing text: {str(e)}",
87
+ "Amenities": f"Error processing text: {str(e)}",
88
+ "Facilities": f"Error processing text: {str(e)}",
89
+ "Seller Name": f"Error processing text: {str(e)}",
90
+ "Location Details": f"Error processing text: {str(e)}"
91
+ }
92
+
93
+ # Function to combine the extraction process (from URL + model processing)
94
+ def get_info(url):
95
+ text = extract_text(url)
96
+ if "Error" in text:
97
+ return text, text, text, text, text # Return the error message for all outputs
98
+
99
+ extracted_info = extract_info_with_model(text)
100
+
101
+ return (
102
+ extracted_info["Keytags"],
103
+ extracted_info["Amenities"],
104
+ extracted_info["Facilities"],
105
+ extracted_info["Seller Name"],
106
+ extracted_info["Location Details"]
107
+ )
108
+
109
+ # Gradio Interface to allow user input and display output
110
+ demo = gr.Interface(
111
+ fn=get_info,
112
+ inputs="text", # Input is a URL
113
+ outputs=["text", "text", "text", "text", "text"], # Outputs for each field (Keytags, Amenities, etc.)
114
+ title="Real Estate Info Extractor",
115
+ description="Extract Keytags, Amenities, Facilities, Seller Name, and Location Details from a real estate article URL."
116
+ )
117
+
118
+ if __name__ == "__main__":
119
+ demo.launch(show_api=False)
120
+