Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +14 -0
- app.py +40 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
WORKDIR /code
|
| 3 |
+
COPY . /requirements.txt/code/requiremnts.txt
|
| 4 |
+
RUN pip install --no-cache-dir--upgrade -r /code/requirements.txt
|
| 5 |
+
RUN useradd user
|
| 6 |
+
USER user
|
| 7 |
+
|
| 8 |
+
ENV HOME=/home/user \
|
| 9 |
+
PATH=/home/user/.local/bin:$PATH
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
WORKDIR $HOME/app
|
| 13 |
+
COPY --chown=user. $HOME/ app
|
| 14 |
+
CMD ["uvicorn","app:app","--host","0.0.0.0","--port","7860"]
|
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from fastapi import FastAPI, Query
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import requests
|
| 5 |
+
from datetime import date
|
| 6 |
+
import uvicorn
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
class WeatherRequest(BaseModel):
|
| 9 |
+
location: str
|
| 10 |
+
API_KEY = "d66b99384fc0495b9bb43946242607"
|
| 11 |
+
BASE_URL = "http://api.weatherapi.com/v1/current.json"
|
| 12 |
+
def get_weather(location):
|
| 13 |
+
params = {
|
| 14 |
+
"key": API_KEY,
|
| 15 |
+
"q": location,
|
| 16 |
+
"dt": date.today().isoformat()
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
response = requests.get(BASE_URL, params=params)
|
| 20 |
+
|
| 21 |
+
if response.status_code == 200:
|
| 22 |
+
data = response.json()
|
| 23 |
+
current = data['current']
|
| 24 |
+
return f"Temperature: {current['temp_c']}°C, Condition: {current['condition']['text']}"
|
| 25 |
+
else:
|
| 26 |
+
return "Unable to fetch weather data"
|
| 27 |
+
@app.get("/")
|
| 28 |
+
def home():
|
| 29 |
+
return {"message": "Welcome to the Weather Finder API"}
|
| 30 |
+
@app.get("/weather")
|
| 31 |
+
def get_weather_endpoint(location: str = Query(..., description="Location to get weather for")):
|
| 32 |
+
weather_info = get_weather(location)
|
| 33 |
+
return {"location": location, "weather_info": weather_info}
|
| 34 |
+
@app.post("/weather")
|
| 35 |
+
def post_weather_endpoint(request: WeatherRequest):
|
| 36 |
+
weather_info = get_weather(request.location)
|
| 37 |
+
return {"location": request.location, "weather_info": weather_info}
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
google-generativeai
|
| 4 |
+
python-dotenv
|