Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import requests
|
|
@@ -72,13 +73,11 @@ def get_weather(location: str) -> dict:
|
|
| 72 |
# Tool execution function
|
| 73 |
def execute_tool(tool_name: str, parameters: dict) -> str:
|
| 74 |
if tool_name == "get_weather":
|
| 75 |
-
# The 'London' fallback will now only be used if Gemini genuinely doesn't provide a location
|
| 76 |
result = get_weather(parameters.get("location", "London"))
|
| 77 |
if "error" in result:
|
| 78 |
return result["error"]
|
| 79 |
|
| 80 |
try:
|
| 81 |
-
# Correct parsing for OpenWeatherMap API
|
| 82 |
location_name = result.get("name", "Unknown City")
|
| 83 |
country = result.get("sys", {}).get("country", "Unknown Country")
|
| 84 |
temp_c = result.get("main", {}).get("temp")
|
|
@@ -91,7 +90,6 @@ def execute_tool(tool_name: str, parameters: dict) -> str:
|
|
| 91 |
if None in [temp_c, humidity, wind_speed_ms, feels_like_c]:
|
| 92 |
return "Incomplete weather data received from API."
|
| 93 |
|
| 94 |
-
# Convert wind speed from m/s to km/h
|
| 95 |
wind_kph = wind_speed_ms * 3.6
|
| 96 |
|
| 97 |
return (
|
|
@@ -116,7 +114,6 @@ def process_with_tools(query: str) -> str:
|
|
| 116 |
try:
|
| 117 |
response = model.generate_content(query)
|
| 118 |
|
| 119 |
-
# Check if Gemini wants to call a function
|
| 120 |
if response.candidates and response.candidates[0].content.parts:
|
| 121 |
for part in response.candidates[0].content.parts:
|
| 122 |
if part.function_call:
|
|
@@ -124,7 +121,6 @@ def process_with_tools(query: str) -> str:
|
|
| 124 |
|
| 125 |
function_args = {}
|
| 126 |
if hasattr(part.function_call, 'args'):
|
| 127 |
-
# Directly convert MapComposite or dict-like objects to dict
|
| 128 |
if hasattr(part.function_call.args, 'items'):
|
| 129 |
function_args = dict(part.function_call.args)
|
| 130 |
elif isinstance(part.function_call.args, str):
|
|
@@ -145,15 +141,14 @@ def process_with_tools(query: str) -> str:
|
|
| 145 |
tool_result = execute_tool("get_weather", function_args)
|
| 146 |
print(f"Tool execution result: {tool_result}")
|
| 147 |
|
| 148 |
-
# Send the tool result back to Gemini for a final, natural language response
|
| 149 |
chat_session = model.start_chat()
|
| 150 |
-
chat_session.send_message(query)
|
| 151 |
|
| 152 |
response_with_tool_output = chat_session.send_message(
|
| 153 |
genai.protos.Part(
|
| 154 |
function_response=genai.protos.FunctionResponse(
|
| 155 |
name="get_weather",
|
| 156 |
-
response={"result": tool_result}
|
| 157 |
)
|
| 158 |
)
|
| 159 |
)
|
|
@@ -161,7 +156,6 @@ def process_with_tools(query: str) -> str:
|
|
| 161 |
else:
|
| 162 |
return f"Unknown tool requested by AI: {function_name}"
|
| 163 |
elif part.text:
|
| 164 |
-
# Gemini provided a direct text response (no tool needed or tool not applicable)
|
| 165 |
return part.text
|
| 166 |
return "No coherent response from AI (neither text nor function call)."
|
| 167 |
|
|
@@ -194,10 +188,17 @@ gradio_app = gr.Interface(
|
|
| 194 |
# Mount Gradio on FastAPI
|
| 195 |
app = gr.mount_gradio_app(app, gradio_app, path="/ui")
|
| 196 |
|
| 197 |
-
#
|
|
|
|
| 198 |
@app.get("/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
def health_check():
|
| 200 |
return {"status": "active", "components": ["fastapi", "gemini", "weather-api", "gradio"]}
|
|
|
|
| 201 |
|
| 202 |
if __name__ == "__main__":
|
| 203 |
import uvicorn
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.responses import RedirectResponse # Import RedirectResponse
|
| 3 |
import google.generativeai as genai
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
import requests
|
|
|
|
| 73 |
# Tool execution function
|
| 74 |
def execute_tool(tool_name: str, parameters: dict) -> str:
|
| 75 |
if tool_name == "get_weather":
|
|
|
|
| 76 |
result = get_weather(parameters.get("location", "London"))
|
| 77 |
if "error" in result:
|
| 78 |
return result["error"]
|
| 79 |
|
| 80 |
try:
|
|
|
|
| 81 |
location_name = result.get("name", "Unknown City")
|
| 82 |
country = result.get("sys", {}).get("country", "Unknown Country")
|
| 83 |
temp_c = result.get("main", {}).get("temp")
|
|
|
|
| 90 |
if None in [temp_c, humidity, wind_speed_ms, feels_like_c]:
|
| 91 |
return "Incomplete weather data received from API."
|
| 92 |
|
|
|
|
| 93 |
wind_kph = wind_speed_ms * 3.6
|
| 94 |
|
| 95 |
return (
|
|
|
|
| 114 |
try:
|
| 115 |
response = model.generate_content(query)
|
| 116 |
|
|
|
|
| 117 |
if response.candidates and response.candidates[0].content.parts:
|
| 118 |
for part in response.candidates[0].content.parts:
|
| 119 |
if part.function_call:
|
|
|
|
| 121 |
|
| 122 |
function_args = {}
|
| 123 |
if hasattr(part.function_call, 'args'):
|
|
|
|
| 124 |
if hasattr(part.function_call.args, 'items'):
|
| 125 |
function_args = dict(part.function_call.args)
|
| 126 |
elif isinstance(part.function_call.args, str):
|
|
|
|
| 141 |
tool_result = execute_tool("get_weather", function_args)
|
| 142 |
print(f"Tool execution result: {tool_result}")
|
| 143 |
|
|
|
|
| 144 |
chat_session = model.start_chat()
|
| 145 |
+
chat_session.send_message(query)
|
| 146 |
|
| 147 |
response_with_tool_output = chat_session.send_message(
|
| 148 |
genai.protos.Part(
|
| 149 |
function_response=genai.protos.FunctionResponse(
|
| 150 |
name="get_weather",
|
| 151 |
+
response={"result": tool_result}
|
| 152 |
)
|
| 153 |
)
|
| 154 |
)
|
|
|
|
| 156 |
else:
|
| 157 |
return f"Unknown tool requested by AI: {function_name}"
|
| 158 |
elif part.text:
|
|
|
|
| 159 |
return part.text
|
| 160 |
return "No coherent response from AI (neither text nor function call)."
|
| 161 |
|
|
|
|
| 188 |
# Mount Gradio on FastAPI
|
| 189 |
app = gr.mount_gradio_app(app, gradio_app, path="/ui")
|
| 190 |
|
| 191 |
+
# === NEW CODE STARTS HERE ===
|
| 192 |
+
# Redirect the root path to the Gradio UI
|
| 193 |
@app.get("/")
|
| 194 |
+
async def redirect_to_gradio():
|
| 195 |
+
return RedirectResponse(url="/ui")
|
| 196 |
+
|
| 197 |
+
# Optional: Keep the health check at a different path if still desired
|
| 198 |
+
@app.get("/health_status")
|
| 199 |
def health_check():
|
| 200 |
return {"status": "active", "components": ["fastapi", "gemini", "weather-api", "gradio"]}
|
| 201 |
+
# === NEW CODE ENDS HERE ===
|
| 202 |
|
| 203 |
if __name__ == "__main__":
|
| 204 |
import uvicorn
|