IshmamF's picture
Update app.py
ad44272 verified
import os
import requests
import json
from geopy.geocoders import GoogleV3
import streamlit as st
API_KEY = os.getenv('WEATHER_API')
GOOGLE_API = os.getenv('GOOGLE_API')
geolocator = GoogleV3(api_key=GOOGLE_API)
STABLE_API = os.getenv("STABLE_API")
def generate_image(prompt):
url = "https://stablediffusionapi.com/api/v3/text2img"
payload = json.dumps({
"key": STABLE_API,
"prompt": prompt,
"negative_prompt": None,
"width": "512",
"height": "512",
"samples": "1",
"num_inference_steps": "20",
"seed": None,
"guidance_scale": 7.5,
"safety_checker": "yes",
"multi_lingual": "no",
"panorama": "no",
"self_attention": "no",
"upscale": "no",
"embeddings_model": None,
"webhook": None,
"track_id": None
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json()["output"][0]
st.title("Weather Image")
user_input = st.text_input('Provide Landmark Below')
additional = st.text_input('Optional Additional Input')
submit = st.button("Submit", type="primary")
# Use for demonstration
# CITY = 'London'
# url = f"https://api.openweathermap.org/data/2.5/weather?q={CITY}&appid={API_KEY}"
if user_input and submit:
location = geolocator.geocode(user_input)
url = f"https://api.openweathermap.org/data/2.5/weather?lat={location.latitude}&lon={location.longitude}&appid={API_KEY}"
response = requests.get(url).json()
city = response['name']
weather = response["weather"][0]["main"]
country = response["sys"]["country"]
description = response["weather"][0]["description"]
if additional:
prompt = f"{additional} at {user_input} in {location},{country} with the weather consisting of {weather} and {description}"
else:
prompt = f"{user_input} in {location},{country} with the weather consisting of {weather} and {description}"
image_url = generate_image(prompt)
st.image(image_url, caption=f"{weather} at {user_input} in {location},{country}")