fix
Browse files
app.py
CHANGED
|
@@ -1,12 +1,15 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
import requests
|
| 4 |
import openai
|
| 5 |
|
| 6 |
# Set your OpenAI API key
|
| 7 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
|
| 9 |
def get_address(lat, lon):
|
|
|
|
|
|
|
|
|
|
| 10 |
headers = {"User-Agent": "gradio-gps-app"}
|
| 11 |
url = "https://nominatim.openstreetmap.org/reverse"
|
| 12 |
params = {
|
|
@@ -16,36 +19,48 @@ def get_address(lat, lon):
|
|
| 16 |
"addressdetails": 1,
|
| 17 |
}
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
data =
|
| 22 |
return data.get("display_name", "Address not found.")
|
| 23 |
except Exception as e:
|
| 24 |
-
return f"Error: {e}"
|
| 25 |
|
| 26 |
def get_street_info(street_name):
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
try:
|
| 29 |
-
response = openai.
|
| 30 |
-
|
| 31 |
-
|
| 32 |
max_tokens=150,
|
| 33 |
-
n=1,
|
| 34 |
-
stop=None,
|
| 35 |
temperature=0.7,
|
| 36 |
)
|
| 37 |
-
return response.choices[0].
|
| 38 |
except Exception as e:
|
| 39 |
-
return f"Error: {e}"
|
| 40 |
|
| 41 |
def process_location(coords):
|
| 42 |
-
|
| 43 |
-
address
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
|
|
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
coords_box = gr.Textbox(label="Your Coordinates (Lat, Lon)", visible=False)
|
| 51 |
address_output = gr.Textbox(label="Resolved Address")
|
|
@@ -53,19 +68,25 @@ with gr.Blocks() as demo:
|
|
| 53 |
|
| 54 |
get_location_btn = gr.Button("๐ Get My Location and Street Info")
|
| 55 |
|
|
|
|
| 56 |
get_location_btn.click(None, js="""
|
| 57 |
() => new Promise((resolve, reject) => {
|
| 58 |
-
navigator.geolocation
|
| 59 |
-
(
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
})
|
| 70 |
""", outputs=coords_box)
|
| 71 |
|
|
@@ -75,4 +96,5 @@ with gr.Blocks() as demo:
|
|
| 75 |
outputs=[address_output, street_info_output]
|
| 76 |
)
|
| 77 |
|
|
|
|
| 78 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import openai
|
| 5 |
|
| 6 |
# Set your OpenAI API key
|
| 7 |
+
openai.api_key = os.getenv("OPENAI_API_KEY", "your_openai_api_key")
|
| 8 |
|
| 9 |
def get_address(lat, lon):
|
| 10 |
+
"""
|
| 11 |
+
Reverse geocodes the given latitude and longitude to retrieve the address.
|
| 12 |
+
"""
|
| 13 |
headers = {"User-Agent": "gradio-gps-app"}
|
| 14 |
url = "https://nominatim.openstreetmap.org/reverse"
|
| 15 |
params = {
|
|
|
|
| 19 |
"addressdetails": 1,
|
| 20 |
}
|
| 21 |
try:
|
| 22 |
+
response = requests.get(url, headers=headers, params=params, timeout=5)
|
| 23 |
+
response.raise_for_status()
|
| 24 |
+
data = response.json()
|
| 25 |
return data.get("display_name", "Address not found.")
|
| 26 |
except Exception as e:
|
| 27 |
+
return f"Error retrieving address: {e}"
|
| 28 |
|
| 29 |
def get_street_info(street_name):
|
| 30 |
+
"""
|
| 31 |
+
Queries OpenAI's GPT model to obtain information about the specified street.
|
| 32 |
+
"""
|
| 33 |
+
messages = [
|
| 34 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 35 |
+
{"role": "user", "content": f"Tell me about {street_name}."}
|
| 36 |
+
]
|
| 37 |
try:
|
| 38 |
+
response = openai.ChatCompletion.create(
|
| 39 |
+
model="gpt-3.5-turbo",
|
| 40 |
+
messages=messages,
|
| 41 |
max_tokens=150,
|
|
|
|
|
|
|
| 42 |
temperature=0.7,
|
| 43 |
)
|
| 44 |
+
return response.choices[0].message['content'].strip()
|
| 45 |
except Exception as e:
|
| 46 |
+
return f"Error retrieving street information: {e}"
|
| 47 |
|
| 48 |
def process_location(coords):
|
| 49 |
+
"""
|
| 50 |
+
Processes the user's coordinates to retrieve the address and street information.
|
| 51 |
+
"""
|
| 52 |
+
try:
|
| 53 |
+
lat, lon = map(float, coords.split(","))
|
| 54 |
+
address = get_address(lat, lon)
|
| 55 |
+
if address.startswith("Error"):
|
| 56 |
+
return address, "Street information unavailable due to address retrieval error."
|
| 57 |
+
street_name = address.split(",")[0]
|
| 58 |
+
street_info = get_street_info(street_name)
|
| 59 |
+
return address, street_info
|
| 60 |
+
except ValueError:
|
| 61 |
+
return "Invalid coordinates provided.", "Unable to retrieve street information."
|
| 62 |
|
| 63 |
+
# Gradio Interface
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
coords_box = gr.Textbox(label="Your Coordinates (Lat, Lon)", visible=False)
|
| 66 |
address_output = gr.Textbox(label="Resolved Address")
|
|
|
|
| 68 |
|
| 69 |
get_location_btn = gr.Button("๐ Get My Location and Street Info")
|
| 70 |
|
| 71 |
+
# JavaScript to fetch geolocation and populate coords_box
|
| 72 |
get_location_btn.click(None, js="""
|
| 73 |
() => new Promise((resolve, reject) => {
|
| 74 |
+
if (navigator.geolocation) {
|
| 75 |
+
navigator.geolocation.getCurrentPosition(
|
| 76 |
+
(position) => {
|
| 77 |
+
const coords = `${position.coords.latitude},${position.coords.longitude}`;
|
| 78 |
+
resolve(coords);
|
| 79 |
+
},
|
| 80 |
+
(error) => {
|
| 81 |
+
alert("Geolocation error: " + error.message);
|
| 82 |
+
reject();
|
| 83 |
+
},
|
| 84 |
+
{ enableHighAccuracy: true }
|
| 85 |
+
);
|
| 86 |
+
} else {
|
| 87 |
+
alert("Geolocation is not supported by this browser.");
|
| 88 |
+
reject();
|
| 89 |
+
}
|
| 90 |
})
|
| 91 |
""", outputs=coords_box)
|
| 92 |
|
|
|
|
| 96 |
outputs=[address_output, street_info_output]
|
| 97 |
)
|
| 98 |
|
| 99 |
+
# Launch the Gradio application
|
| 100 |
demo.launch()
|