Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,13 @@
|
|
| 1 |
-
# @title Weather App - Hugging Face/Gradio
|
| 2 |
-
|
| 3 |
-
import requests
|
| 4 |
-
import pandas as pd
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
import gradio as gr
|
| 7 |
-
from io import BytesIO
|
| 8 |
-
|
| 9 |
-
# Funkcja do pobierania pogody
|
| 10 |
-
def get_current_weather(api_key, city):
|
| 11 |
-
url = "http://api.weatherstack.com/current"
|
| 12 |
-
params = {
|
| 13 |
-
"access_key": api_key,
|
| 14 |
-
"query": city,
|
| 15 |
-
"units": "m"
|
| 16 |
-
}
|
| 17 |
-
response = requests.get(url, params=params)
|
| 18 |
-
if response.status_code == 200:
|
| 19 |
-
return response.json()
|
| 20 |
-
else:
|
| 21 |
-
return {"error": {"info": f"Błąd HTTP: {response.status_code}"}}
|
| 22 |
-
|
| 23 |
-
# Funkcja do wyświetlania danych i wykresu
|
| 24 |
-
def display_weather(api_key, city):
|
| 25 |
-
data = get_current_weather(api_key, city)
|
| 26 |
-
|
| 27 |
-
if 'error' in data:
|
| 28 |
-
return data['error']['info'], None
|
| 29 |
-
|
| 30 |
-
location = data['location']
|
| 31 |
-
current = data['current']
|
| 32 |
-
|
| 33 |
-
desc = f"""
|
| 34 |
-
### Pogoda w {location['name']}, {location['country']}
|
| 35 |
-
|
| 36 |
-
**Opis:** {current['weather_descriptions'][0]}
|
| 37 |
-
**Temperatura:** {current['temperature']}°C (odczuwalna: {current['feelslike']}°C)
|
| 38 |
-
**Wilgotność:** {current['humidity']}%
|
| 39 |
-
**Wiatr:** {current['wind_speed']} km/h, {current['wind_dir']}
|
| 40 |
-
**Ciśnienie:** {current['pressure']} hPa
|
| 41 |
-
**Zachmurzenie:** {current['cloudcover']}%
|
| 42 |
-
**Widoczność:** {current['visibility']} km
|
| 43 |
-
**UV:** {current['uv_index']}
|
| 44 |
-
**Lokalny czas:** {location['localtime']}
|
| 45 |
-
"""
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
colors = ['#FF9500', '#FFB04F', '#3498db', '#95a5a6']
|
| 51 |
-
|
| 52 |
-
fig, ax = plt.subplots(figsize=(6, 4))
|
| 53 |
-
bars = ax.bar(labels, values, color=colors, edgecolor='white')
|
| 54 |
-
for bar in bars:
|
| 55 |
-
ax.text(bar.get_x() + bar.get_width()/2., bar.get_height() + 0.5, f'{bar.get_height()}', ha='center', fontweight='bold')
|
| 56 |
-
|
| 57 |
-
ax.set_ylim(0, max(values)*1.2)
|
| 58 |
-
ax.set_title(f'{location["name"]} - {location["localtime"]}')
|
| 59 |
-
ax.grid(True, linestyle='--', alpha=0.5)
|
| 60 |
-
plt.tight_layout()
|
| 61 |
-
|
| 62 |
-
buf = BytesIO()
|
| 63 |
-
plt.savefig(buf, format="png")
|
| 64 |
-
buf.seek(0)
|
| 65 |
-
|
| 66 |
-
return desc, buf
|
| 67 |
|
| 68 |
# Interfejs Gradio
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
api_input = gr.Textbox(label="API Key", placeholder="Wklej swój klucz Weatherstack", type='password')
|
| 73 |
-
city_input = gr.Textbox(label="Miasto", value="Warsaw")
|
| 74 |
-
|
| 75 |
-
submit_btn = gr.Button("Pobierz pogodę")
|
| 76 |
-
output_markdown = gr.Markdown()
|
| 77 |
-
output_plot = gr.Image(type="pil")
|
| 78 |
-
|
| 79 |
-
submit_btn.click(fn=display_weather,
|
| 80 |
-
inputs=[api_input, city_input],
|
| 81 |
-
outputs=[output_markdown, output_plot])
|
| 82 |
|
| 83 |
-
#
|
| 84 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
# Funkcja bota
|
| 4 |
+
def pomidor_bot(user_input):
|
| 5 |
+
return "POMIDOR"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Interfejs Gradio
|
| 8 |
+
demo = gr.ChatInterface(pomidor_bot,
|
| 9 |
+
title="🍅 Bot Pomidor",
|
| 10 |
+
description="Zadaj mi dowolne pytanie. Odpowiem ci... pomidorem 😏")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Uruchomienie
|
| 13 |
demo.launch()
|