import gradio as gr import openai import requests import datetime openai.api_key = "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" weather_api_key = "1aafc3163909c1493596da9340e00aee" def get_weather_data(city): url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={weather_api_key}&units=metric" response = requests.get(url) data = response.json() if data["cod"] == "404": return "City not found." weather = data["weather"][0]["description"] temperature = data["main"]["temp"] location = data["name"] time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") return f"Location: {location}\nTime: {time}\nWeather: {weather}\nTemperature: {temperature}°C" def get_weather_feedback(user_input): response = openai.Completion.create( engine="text-davinci-003", prompt=f"The weather today is {user_input}.", max_tokens=50, n=1, stop=None, temperature=0.6 ) feedback = response.choices[0].text.strip() return feedback iface = gr.Interface( fn=get_weather_data, inputs="text", outputs="text", title="Weather Forecast", description="Enter the name of a city to get the weather forecast.", examples=[["New York"], ["London"], ["Tokyo"]] ) feedback_iface = gr.Interface( fn=get_weather_feedback, inputs="text", outputs="text", title="Feedback", description="Enter a weather description to get feedback.", examples=[["sunny"], ["rainy"], ["cloudy"]] ) if __name__ == "__main__": iface.launch()