File size: 2,079 Bytes
56d1238
 
ad44272
56d1238
 
 
 
 
 
2cebe33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56d1238
 
 
2630f42
 
 
56d1238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2630f42
 
02a0a1b
2630f42
02a0a1b
 
2630f42
56d1238
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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}")