Spaces:
Sleeping
Sleeping
added debugging script and changed the data html to pandas
Browse files- app.py +19 -12
- geolocation_apis.py +1 -66
- llm_api.py +13 -49
- location.py +66 -0
app.py
CHANGED
|
@@ -13,10 +13,12 @@ def return_html_file(files):
|
|
| 13 |
points = [loc.coords for loc in locations]
|
| 14 |
route = coordinates_to_route(points)
|
| 15 |
df = pd.DataFrame([loc.to_dict() for loc in locations])
|
|
|
|
|
|
|
| 16 |
df_html = df.to_html(index=False, escape=False)
|
| 17 |
map_output_path = r"map_output.html"
|
| 18 |
generate_route_map(route, map_output_path)
|
| 19 |
-
html_content =
|
| 20 |
data_output_path = './data_output.html'
|
| 21 |
with open(data_output_path, mode='w', encoding='utf-8') as html:
|
| 22 |
html.write(html_content)
|
|
@@ -24,15 +26,20 @@ def return_html_file(files):
|
|
| 24 |
return [data_output_path, map_output_path]
|
| 25 |
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
points = [loc.coords for loc in locations]
|
| 14 |
route = coordinates_to_route(points)
|
| 15 |
df = pd.DataFrame([loc.to_dict() for loc in locations])
|
| 16 |
+
llm_parsed = llm_parsed.rename(columns={'business_address': 'Address'})
|
| 17 |
+
df = df.join(llm_parsed.set_index('Address'), on='Address')
|
| 18 |
df_html = df.to_html(index=False, escape=False)
|
| 19 |
map_output_path = r"map_output.html"
|
| 20 |
generate_route_map(route, map_output_path)
|
| 21 |
+
html_content = df_html
|
| 22 |
data_output_path = './data_output.html'
|
| 23 |
with open(data_output_path, mode='w', encoding='utf-8') as html:
|
| 24 |
html.write(html_content)
|
|
|
|
| 26 |
return [data_output_path, map_output_path]
|
| 27 |
|
| 28 |
|
| 29 |
+
def main():
|
| 30 |
+
iface = gr.Interface(
|
| 31 |
+
fn=return_html_file,
|
| 32 |
+
inputs=gr.Files(file_types=[".pdf"]),
|
| 33 |
+
outputs=[
|
| 34 |
+
gr.File(label="📊 Business Data HTML"),
|
| 35 |
+
gr.File(label="🗺️ Route Map HTML")
|
| 36 |
+
],
|
| 37 |
+
title="Download HTML File",
|
| 38 |
+
description="Upload PDF files to trigger the download of a prepared HTML file."
|
| 39 |
+
)
|
| 40 |
|
| 41 |
+
iface.launch()
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
if __name__ == '__main__':
|
| 45 |
+
main()
|
geolocation_apis.py
CHANGED
|
@@ -1,79 +1,14 @@
|
|
| 1 |
from dotenv import load_dotenv
|
|
|
|
| 2 |
import urllib.parse
|
| 3 |
import pandas as pd
|
| 4 |
import requests
|
| 5 |
-
import re
|
| 6 |
import os
|
| 7 |
|
| 8 |
HOME_COORDS = [35.09126, 32.85195]
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
|
| 12 |
-
class Location:
|
| 13 |
-
def __init__(self, address):
|
| 14 |
-
self.address = address
|
| 15 |
-
self.city = None
|
| 16 |
-
self.street = None
|
| 17 |
-
self.building = None
|
| 18 |
-
self.coords = None
|
| 19 |
-
self.coords_level = 'address'
|
| 20 |
-
self.parse_address()
|
| 21 |
-
|
| 22 |
-
def __str__(self):
|
| 23 |
-
if self.coords:
|
| 24 |
-
return f"""
|
| 25 |
-
---------------------------------------
|
| 26 |
-
city: {self.city}
|
| 27 |
-
street: {self.street}
|
| 28 |
-
building: {self.building}
|
| 29 |
-
coords: {self.coords}
|
| 30 |
-
coords level: {self.coords_level}
|
| 31 |
-
"""
|
| 32 |
-
else:
|
| 33 |
-
return f"""
|
| 34 |
-
---------------------------------------
|
| 35 |
-
city: {self.city}
|
| 36 |
-
street: {self.street}
|
| 37 |
-
building: {self.building}
|
| 38 |
-
"""
|
| 39 |
-
|
| 40 |
-
def to_dict(self):
|
| 41 |
-
return {
|
| 42 |
-
"Address": self.address,
|
| 43 |
-
"City": self.city,
|
| 44 |
-
"Street": self.street,
|
| 45 |
-
"Building": self.building,
|
| 46 |
-
"Coordinates": f"{self.coords[0]}, {self.coords[1]}" if self.coords else None,
|
| 47 |
-
"Precision": self.coords_level,
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
def set_coords_level(self, level):
|
| 51 |
-
self.coords_level = level
|
| 52 |
-
|
| 53 |
-
def set_coords(self, lat, lon):
|
| 54 |
-
self.coords = (lat, lon)
|
| 55 |
-
|
| 56 |
-
def parse_address(self):
|
| 57 |
-
"""
|
| 58 |
-
Parses address of the form: "StreetName BuildingNumber, CityName"
|
| 59 |
-
Example: "גושן 1 ,קרית מוצקין"
|
| 60 |
-
"""
|
| 61 |
-
if not self.address or ',' not in self.address:
|
| 62 |
-
return
|
| 63 |
-
|
| 64 |
-
try:
|
| 65 |
-
before_comma, after_comma = self.address.split(',', 1)
|
| 66 |
-
self.city = after_comma.strip()
|
| 67 |
-
|
| 68 |
-
# Regex: match "רחוב [שם] מספר [מספר]" or just "[שם] [מספר]"
|
| 69 |
-
match = re.match(r'(.+?)\s+(\d+)', before_comma.strip())
|
| 70 |
-
if match:
|
| 71 |
-
self.street = match.group(1).strip()
|
| 72 |
-
self.building = match.group(2).strip()
|
| 73 |
-
except Exception as e:
|
| 74 |
-
print(f"Failed to parse address: {self.address}. Error: {e}")
|
| 75 |
-
|
| 76 |
-
|
| 77 |
def call_tomtom_api(location):
|
| 78 |
base_url = os.getenv("TOMTOM_URL")
|
| 79 |
api_key = os.getenv("TOMTOM_API_KEY")
|
|
|
|
| 1 |
from dotenv import load_dotenv
|
| 2 |
+
from location import Location
|
| 3 |
import urllib.parse
|
| 4 |
import pandas as pd
|
| 5 |
import requests
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
HOME_COORDS = [35.09126, 32.85195]
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def call_tomtom_api(location):
|
| 13 |
base_url = os.getenv("TOMTOM_URL")
|
| 14 |
api_key = os.getenv("TOMTOM_API_KEY")
|
llm_api.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 2 |
from langchain.output_parsers import PydanticOutputParser
|
| 3 |
from langchain_core.prompts import PromptTemplate
|
|
@@ -24,48 +27,9 @@ class InsuredDetails(BaseModel):
|
|
| 24 |
separator = "~" * 20
|
| 25 |
return "\n".join([content, separator])
|
| 26 |
|
| 27 |
-
def to_html(self):
|
| 28 |
-
return f"""
|
| 29 |
-
<tr>
|
| 30 |
-
<td>{self.business_name}</td>
|
| 31 |
-
<td>{self.business_address}</td>
|
| 32 |
-
<td>{self.contact_name}</td>
|
| 33 |
-
<td>{self.contact_phone}</td>
|
| 34 |
-
</tr>"""
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
def parse_to_html(results):
|
| 38 |
-
# HTML Header (table structure)
|
| 39 |
-
html_content = """
|
| 40 |
-
<html>
|
| 41 |
-
<head><title>Insured Business Details</title></head>
|
| 42 |
-
<body>
|
| 43 |
-
<h1>Insured Business Details</h1>
|
| 44 |
-
<table border="1">
|
| 45 |
-
<thead>
|
| 46 |
-
<tr>
|
| 47 |
-
<th>Business Name</th>
|
| 48 |
-
<th>Business Address</th>
|
| 49 |
-
<th>Contact Name</th>
|
| 50 |
-
<th>Contact Phone</th>
|
| 51 |
-
</tr>
|
| 52 |
-
</thead>
|
| 53 |
-
<tbody>
|
| 54 |
-
"""
|
| 55 |
-
|
| 56 |
-
# Adding each InsuredDetails as a table row
|
| 57 |
-
for item in results:
|
| 58 |
-
html_content += item.to_html()
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
</tbody>
|
| 63 |
-
</table>
|
| 64 |
-
</body>
|
| 65 |
-
</html>
|
| 66 |
-
"""
|
| 67 |
-
|
| 68 |
-
return html_content
|
| 69 |
|
| 70 |
|
| 71 |
load_dotenv() # Only needed locally
|
|
@@ -132,11 +96,12 @@ def llm_parser(docs, model="gemini-2.0-flash-001"):
|
|
| 132 |
for item in results:
|
| 133 |
print(item)
|
| 134 |
|
| 135 |
-
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
return results, html_results
|
| 138 |
|
| 139 |
-
# x =
|
| 140 |
# שומרה חברה לביטוח בע"מ
|
| 141 |
#
|
| 142 |
# לפנה"צ
|
|
@@ -426,8 +391,7 @@ def llm_parser(docs, model="gemini-2.0-flash-001"):
|
|
| 426 |
#
|
| 427 |
#
|
| 428 |
# """
|
| 429 |
-
#
|
| 430 |
-
#
|
| 431 |
-
# results =
|
| 432 |
-
|
| 433 |
-
# print(results)
|
|
|
|
| 1 |
+
from copy import copy
|
| 2 |
+
|
| 3 |
+
import pandas as pd
|
| 4 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 5 |
from langchain.output_parsers import PydanticOutputParser
|
| 6 |
from langchain_core.prompts import PromptTemplate
|
|
|
|
| 27 |
separator = "~" * 20
|
| 28 |
return "\n".join([content, separator])
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
def parse_to_df(results):
|
| 32 |
+
return pd.DataFrame([ins.model_dump() for ins in results])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
load_dotenv() # Only needed locally
|
|
|
|
| 96 |
for item in results:
|
| 97 |
print(item)
|
| 98 |
|
| 99 |
+
df_results = parse_to_df(results)
|
| 100 |
+
|
| 101 |
+
return results, df_results
|
| 102 |
|
|
|
|
| 103 |
|
| 104 |
+
# x = """
|
| 105 |
# שומרה חברה לביטוח בע"מ
|
| 106 |
#
|
| 107 |
# לפנה"צ
|
|
|
|
| 391 |
#
|
| 392 |
#
|
| 393 |
# """
|
| 394 |
+
# y = copy(x)
|
| 395 |
+
#
|
| 396 |
+
# results = llm_parser([x, y])
|
| 397 |
+
|
|
|
location.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class Location:
|
| 5 |
+
def __init__(self, address):
|
| 6 |
+
self.address = address
|
| 7 |
+
self.city = None
|
| 8 |
+
self.street = None
|
| 9 |
+
self.building = None
|
| 10 |
+
self.coords = None
|
| 11 |
+
self.coords_level = 'address'
|
| 12 |
+
self.parse_address()
|
| 13 |
+
|
| 14 |
+
def __str__(self):
|
| 15 |
+
if self.coords:
|
| 16 |
+
return f"""
|
| 17 |
+
---------------------------------------
|
| 18 |
+
city: {self.city}
|
| 19 |
+
street: {self.street}
|
| 20 |
+
building: {self.building}
|
| 21 |
+
coords: {self.coords}
|
| 22 |
+
coords level: {self.coords_level}
|
| 23 |
+
"""
|
| 24 |
+
else:
|
| 25 |
+
return f"""
|
| 26 |
+
---------------------------------------
|
| 27 |
+
city: {self.city}
|
| 28 |
+
street: {self.street}
|
| 29 |
+
building: {self.building}
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
def to_dict(self):
|
| 33 |
+
return {
|
| 34 |
+
"Address": self.address,
|
| 35 |
+
"City": self.city,
|
| 36 |
+
"Street": self.street,
|
| 37 |
+
"Building": self.building,
|
| 38 |
+
"Coordinates": f"{self.coords[0]}, {self.coords[1]}" if self.coords else None,
|
| 39 |
+
"Precision": self.coords_level,
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
def set_coords_level(self, level):
|
| 43 |
+
self.coords_level = level
|
| 44 |
+
|
| 45 |
+
def set_coords(self, lat, lon):
|
| 46 |
+
self.coords = (lat, lon)
|
| 47 |
+
|
| 48 |
+
def parse_address(self):
|
| 49 |
+
"""
|
| 50 |
+
Parses address of the form: "StreetName BuildingNumber, CityName"
|
| 51 |
+
Example: "גושן 1 ,קרית מוצקין"
|
| 52 |
+
"""
|
| 53 |
+
if not self.address or ',' not in self.address:
|
| 54 |
+
return
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
before_comma, after_comma = self.address.split(',', 1)
|
| 58 |
+
self.city = after_comma.strip()
|
| 59 |
+
|
| 60 |
+
# Regex: match "רחוב [שם] מספר [מספר]" or just "[שם] [מספר]"
|
| 61 |
+
match = re.match(r'(.+?)\s+(\d+)', before_comma.strip())
|
| 62 |
+
if match:
|
| 63 |
+
self.street = match.group(1).strip()
|
| 64 |
+
self.building = match.group(2).strip()
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"Failed to parse address: {self.address}. Error: {e}")
|