Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -86,7 +86,6 @@ def handle_error(message, e=None):
|
|
| 86 |
def pinpoint_location(api_key, uploaded_image):
|
| 87 |
"""
|
| 88 |
Takes an API key and an uploaded image, returns location data.
|
| 89 |
-
The `gr.Image(type="pil")` component handles the upload, replacing the need for a custom upload function.
|
| 90 |
"""
|
| 91 |
if not api_key:
|
| 92 |
handle_error("Google AI API Key is required.")
|
|
@@ -95,25 +94,26 @@ def pinpoint_location(api_key, uploaded_image):
|
|
| 95 |
|
| 96 |
try:
|
| 97 |
rp = RainboltParody(title="Location Analysis", api_key=api_key)
|
| 98 |
-
# 'uploaded_image' is already a PIL Image object, thanks to `type="pil"`.
|
| 99 |
response, _, _ = rp.get_info(SYSTEM_PROMPT=SYSTEM_PROMPT, img=uploaded_image)
|
| 100 |
|
| 101 |
-
# Prepare outputs for Gradio
|
| 102 |
-
# Convert the dictionary to a list of lists for the DataFrame
|
| 103 |
info_data = [[key, value] for key, value in response.items()]
|
| 104 |
info_df = pd.DataFrame(info_data, columns=["Attribute", "Value"])
|
| 105 |
|
| 106 |
reasoning = response.get('reasoning', 'No reasoning provided.')
|
| 107 |
coords_str = response.get('coordinates', '0,0')
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
# Update visibility of subsequent sections
|
| 111 |
ui_updates = {
|
| 112 |
error_accordion: gr.Accordion(visible=True),
|
| 113 |
save_accordion: gr.Accordion(visible=True)
|
| 114 |
}
|
| 115 |
|
| 116 |
-
|
|
|
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
handle_error("Failed to process the image with the AI model", e)
|
|
@@ -160,7 +160,6 @@ def calculate_error_distance(prediction_state, url, manual_coords_str):
|
|
| 160 |
def save_and_download(prediction_state, error_km, direction, original_url, filename):
|
| 161 |
"""
|
| 162 |
Saves the combined results to a CSV file and provides it for download.
|
| 163 |
-
This function replaces the Colab-specific `prepare_csv`.
|
| 164 |
"""
|
| 165 |
if not prediction_state:
|
| 166 |
handle_error("No prediction data to save.")
|
|
@@ -170,24 +169,20 @@ def save_and_download(prediction_state, error_km, direction, original_url, filen
|
|
| 170 |
filename += '.csv'
|
| 171 |
|
| 172 |
try:
|
| 173 |
-
# Prepare the new row of data
|
| 174 |
data_to_add = prediction_state.copy()
|
| 175 |
data_to_add['error'] = f"{error_km:.2f}" if error_km is not None else 'N/A'
|
| 176 |
data_to_add['direction'] = direction if direction else 'N/A'
|
| 177 |
data_to_add['actual_location_url'] = original_url if original_url else 'N/A'
|
| 178 |
new_df = pd.DataFrame([data_to_add])
|
| 179 |
|
| 180 |
-
# Check if file exists to append or create new
|
| 181 |
if os.path.exists(filename):
|
| 182 |
existing_df = pd.read_csv(filename)
|
| 183 |
final_df = pd.concat([existing_df, new_df], ignore_index=True)
|
| 184 |
else:
|
| 185 |
final_df = new_df
|
| 186 |
|
| 187 |
-
# Save the final dataframe to the CSV file
|
| 188 |
final_df.to_csv(filename, index=False)
|
| 189 |
|
| 190 |
-
# Return the updated DataFrame for display and the file path for download
|
| 191 |
return gr.DataFrame(value=final_df, visible=True), gr.File(value=filename, label="Download CSV", visible=True)
|
| 192 |
|
| 193 |
except Exception as e:
|
|
@@ -198,7 +193,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="GeoGuessr AI") as app:
|
|
| 198 |
gr.Markdown("# GeoGuessr AI π")
|
| 199 |
gr.Markdown("Pinpoint the location of any Google Street View image.")
|
| 200 |
|
| 201 |
-
# State variables to hold data between steps
|
| 202 |
prediction_state = gr.State(None)
|
| 203 |
error_state = gr.State(None)
|
| 204 |
direction_state = gr.State(None)
|
|
@@ -217,7 +211,8 @@ with gr.Blocks(theme=gr.themes.Soft(), title="GeoGuessr AI") as app:
|
|
| 217 |
|
| 218 |
with gr.Column(scale=2):
|
| 219 |
gr.Markdown("### Map View")
|
| 220 |
-
|
|
|
|
| 221 |
map_output_comparison = gr.HTML(visible=False)
|
| 222 |
|
| 223 |
with gr.Accordion("2. Calculate Error (Optional)", open=False, visible=False) as error_accordion:
|
|
|
|
| 86 |
def pinpoint_location(api_key, uploaded_image):
|
| 87 |
"""
|
| 88 |
Takes an API key and an uploaded image, returns location data.
|
|
|
|
| 89 |
"""
|
| 90 |
if not api_key:
|
| 91 |
handle_error("Google AI API Key is required.")
|
|
|
|
| 94 |
|
| 95 |
try:
|
| 96 |
rp = RainboltParody(title="Location Analysis", api_key=api_key)
|
|
|
|
| 97 |
response, _, _ = rp.get_info(SYSTEM_PROMPT=SYSTEM_PROMPT, img=uploaded_image)
|
| 98 |
|
|
|
|
|
|
|
| 99 |
info_data = [[key, value] for key, value in response.items()]
|
| 100 |
info_df = pd.DataFrame(info_data, columns=["Attribute", "Value"])
|
| 101 |
|
| 102 |
reasoning = response.get('reasoning', 'No reasoning provided.')
|
| 103 |
coords_str = response.get('coordinates', '0,0')
|
| 104 |
+
|
| 105 |
+
# --- FIX: Generate map HTML for the single point ---
|
| 106 |
+
mgk = MapGenerator()
|
| 107 |
+
single_point_map = mgk.get_map(location1=coords_str)
|
| 108 |
+
map_html_output = single_point_map._repr_html_()
|
| 109 |
|
|
|
|
| 110 |
ui_updates = {
|
| 111 |
error_accordion: gr.Accordion(visible=True),
|
| 112 |
save_accordion: gr.Accordion(visible=True)
|
| 113 |
}
|
| 114 |
|
| 115 |
+
# Return the map HTML instead of coordinates
|
| 116 |
+
return info_df, reasoning, map_html_output, response, ui_updates
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
handle_error("Failed to process the image with the AI model", e)
|
|
|
|
| 160 |
def save_and_download(prediction_state, error_km, direction, original_url, filename):
|
| 161 |
"""
|
| 162 |
Saves the combined results to a CSV file and provides it for download.
|
|
|
|
| 163 |
"""
|
| 164 |
if not prediction_state:
|
| 165 |
handle_error("No prediction data to save.")
|
|
|
|
| 169 |
filename += '.csv'
|
| 170 |
|
| 171 |
try:
|
|
|
|
| 172 |
data_to_add = prediction_state.copy()
|
| 173 |
data_to_add['error'] = f"{error_km:.2f}" if error_km is not None else 'N/A'
|
| 174 |
data_to_add['direction'] = direction if direction else 'N/A'
|
| 175 |
data_to_add['actual_location_url'] = original_url if original_url else 'N/A'
|
| 176 |
new_df = pd.DataFrame([data_to_add])
|
| 177 |
|
|
|
|
| 178 |
if os.path.exists(filename):
|
| 179 |
existing_df = pd.read_csv(filename)
|
| 180 |
final_df = pd.concat([existing_df, new_df], ignore_index=True)
|
| 181 |
else:
|
| 182 |
final_df = new_df
|
| 183 |
|
|
|
|
| 184 |
final_df.to_csv(filename, index=False)
|
| 185 |
|
|
|
|
| 186 |
return gr.DataFrame(value=final_df, visible=True), gr.File(value=filename, label="Download CSV", visible=True)
|
| 187 |
|
| 188 |
except Exception as e:
|
|
|
|
| 193 |
gr.Markdown("# GeoGuessr AI π")
|
| 194 |
gr.Markdown("Pinpoint the location of any Google Street View image.")
|
| 195 |
|
|
|
|
| 196 |
prediction_state = gr.State(None)
|
| 197 |
error_state = gr.State(None)
|
| 198 |
direction_state = gr.State(None)
|
|
|
|
| 211 |
|
| 212 |
with gr.Column(scale=2):
|
| 213 |
gr.Markdown("### Map View")
|
| 214 |
+
# --- FIX: Changed gr.Map to gr.HTML ---
|
| 215 |
+
map_output_single = gr.HTML(label="Predicted Location")
|
| 216 |
map_output_comparison = gr.HTML(visible=False)
|
| 217 |
|
| 218 |
with gr.Accordion("2. Calculate Error (Optional)", open=False, visible=False) as error_accordion:
|