Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,99 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from google import genai
|
| 3 |
-
|
| 4 |
-
# Initialize Gemini AI client
|
| 5 |
-
GENAI_API_KEY = "GOOGLE_API_KEY" # Replace with your API key
|
| 6 |
-
client = genai.Client(api_key=GENAI_API_KEY)
|
| 7 |
-
MODEL_ID = "gemini-2.0-flash-exp"
|
| 8 |
-
|
| 9 |
-
# Define a system instruction for structured responses
|
| 10 |
-
GENAI_INSTRUCTION = """
|
| 11 |
-
You are a Google Maps assistant. Respond to location-related queries with structured data.
|
| 12 |
-
Format your response as:
|
| 13 |
-
{
|
| 14 |
-
"action": "search/navigate/mark",
|
| 15 |
-
"location": "Place Name or Coordinates",
|
| 16 |
-
"origin": "Origin Location (for navigation only)",
|
| 17 |
-
"destination": "Destination Location (for navigation only)"
|
| 18 |
-
}
|
| 19 |
-
"""
|
| 20 |
-
|
| 21 |
-
# Function to generate Google Maps URLs
|
| 22 |
-
def generate_google_maps_url(action, location=None, origin=None, destination=None):
|
| 23 |
-
"""
|
| 24 |
-
Generate Google Maps URLs based on user action.
|
| 25 |
-
"""
|
| 26 |
-
base_url = "https://www.google.com/maps"
|
| 27 |
-
|
| 28 |
-
if action == "search":
|
| 29 |
-
# Search for a location
|
| 30 |
-
return f"{base_url}/search/?api=1&query={location.replace(' ', '+')}"
|
| 31 |
-
|
| 32 |
-
elif action == "navigate":
|
| 33 |
-
# Get directions between two locations
|
| 34 |
-
return f"{base_url}/dir/?api=1&origin={origin.replace(' ', '+')}&destination={destination.replace(' ', '+')}"
|
| 35 |
-
|
| 36 |
-
elif action == "mark":
|
| 37 |
-
# Mark a specific location by coordinates
|
| 38 |
-
latitude, longitude = location.split(",")
|
| 39 |
-
return f"{base_url}/?q={latitude},{longitude}"
|
| 40 |
-
|
| 41 |
-
else:
|
| 42 |
-
return f"{base_url}"
|
| 43 |
-
|
| 44 |
-
# Function to process queries with Gemini AI
|
| 45 |
-
def process_query_with_ai(question):
|
| 46 |
-
try:
|
| 47 |
-
# Send the user query to Gemini
|
| 48 |
-
response = client.models.generate_content(
|
| 49 |
-
model=MODEL_ID,
|
| 50 |
-
contents=question,
|
| 51 |
-
config={"system_instruction": GENAI_INSTRUCTION}
|
| 52 |
-
)
|
| 53 |
-
structured_response = eval(response.text) # Convert text response to dictionary
|
| 54 |
-
|
| 55 |
-
# Parse the AI response
|
| 56 |
-
action = structured_response.get("action")
|
| 57 |
-
location = structured_response.get("location")
|
| 58 |
-
origin = structured_response.get("origin")
|
| 59 |
-
destination = structured_response.get("destination")
|
| 60 |
-
|
| 61 |
-
# Generate a Google Maps URL
|
| 62 |
-
map_url = generate_google_maps_url(action, location, origin, destination)
|
| 63 |
-
|
| 64 |
-
# Format output for Gradio visualization
|
| 65 |
-
if action == "search":
|
| 66 |
-
output = f"Search results for: **{location}**"
|
| 67 |
-
elif action == "navigate":
|
| 68 |
-
output = f"Directions from **{origin}** to **{destination}**"
|
| 69 |
-
elif action == "mark":
|
| 70 |
-
output = f"Location marked: **{location}**"
|
| 71 |
-
else:
|
| 72 |
-
output = "Unknown action."
|
| 73 |
-
|
| 74 |
-
return output, map_url
|
| 75 |
-
except Exception as e:
|
| 76 |
-
return f"Error: {str(e)}", ""
|
| 77 |
-
|
| 78 |
-
# Gradio interface
|
| 79 |
-
def handle_query(question):
|
| 80 |
-
output, map_url = process_query_with_ai(question)
|
| 81 |
-
return output, map_url
|
| 82 |
-
|
| 83 |
-
with gr.Blocks() as app:
|
| 84 |
-
gr.Markdown("# 🌍 AI-Powered Google Maps Navigator")
|
| 85 |
-
with gr.Row():
|
| 86 |
-
with gr.Column():
|
| 87 |
-
question_input = gr.Textbox(label="Ask a map-related question!", lines=2)
|
| 88 |
-
submit_button = gr.Button("Submit")
|
| 89 |
-
with gr.Column():
|
| 90 |
-
output_text = gr.Textbox(label="Response")
|
| 91 |
-
map_link = gr.Markdown(label="Map Link")
|
| 92 |
-
|
| 93 |
-
submit_button.click(
|
| 94 |
-
fn=handle_query,
|
| 95 |
-
inputs=[question_input],
|
| 96 |
-
outputs=[output_text, map_link]
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|