Spaces:
Runtime error
Runtime error
import os | |
import gradio as gr | |
import numpy as np | |
from PIL import Image | |
import requests | |
from tensorflow import keras | |
from geopy.geocoders import Nominatim | |
from datetime import datetime | |
import hopsworks | |
GUI_TITLE = "Earthquake Predictive Analytics" | |
GUI_DESCRIPTION = "Experiment with location and time to predict depth and magnitude." | |
MODEL_NAME = "earthquake_model" | |
MODEL_VERSION = 1 | |
TIME_FORMAT = "%Y-%m-%d %H:%M:%S" | |
hw = hopsworks.login() | |
registry = hw.get_model_registry() | |
model = registry.get_model(MODEL_NAME, version=MODEL_VERSION) | |
model_dir = model.download() | |
model = keras.models.load_model(os.path.join(model_dir, MODEL_NAME + ".h5")) | |
geolocator = Nominatim(user_agent=GUI_TITLE) | |
def earthquake(location, time): | |
geodata = geolocator.geocode(location) | |
time_input = datetime.strptime(time, TIME_FORMAT).timestamp() / 1E7 | |
input_list = [] | |
input_list.append(geodata.latitude) | |
input_list.append(geodata.longitude) | |
input_list.append(time_input) | |
# prediction result of the model | |
res = model.predict(np.asarray(input_list).reshape(1, -1))[0] | |
# split the result to 2 separate variables | |
print(res) | |
return geodata.address, res[0] | |
demo = gr.Interface( | |
fn=earthquake, | |
title=GUI_TITLE, | |
description=GUI_DESCRIPTION, | |
allow_flagging="never", | |
inputs=[ | |
gr.inputs.Textbox(default="Stockholm", label="location"), | |
gr.inputs.Textbox(default=datetime.now().strftime(TIME_FORMAT), label="time"), | |
], | |
outputs=[ | |
gr.Textbox(label="full location"), | |
gr.Number(label="magnitude"), | |
] | |
) | |
demo.launch() | |