maghwa commited on
Commit
99cdd53
β€’
1 Parent(s): 7e2ac9b

Integrated weather API

Browse files
Files changed (1) hide show
  1. app.py +49 -4
app.py CHANGED
@@ -5,6 +5,9 @@ from streamlit_extras.colored_header import colored_header
5
  from streamlit_extras.add_vertical_space import add_vertical_space
6
  import requests
7
  from gradio_client import Client
 
 
 
8
 
9
  st.set_page_config(page_title="HugChat - An LLM-powered Streamlit app")
10
 
@@ -32,6 +35,43 @@ soil_types = {
32
  "Saharan zone": "Yermosols, associated with sierozems, lithosols, and saline soils"
33
  }
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  def query(payload):
36
  response = requests.post(API_URL, headers=headers, json=payload)
37
  return response.json()
@@ -51,14 +91,19 @@ def translate(text,source="English",target="Moroccan Arabic"):
51
  # Function to generate a response from the chatbot
52
  def generate_response(user_input,region):
53
 
 
 
 
 
 
54
  user_input_translated = str(translate(user_input, "Moroccan Arabic", "English"))
55
  name = 'Mohammed'
56
  date = 'December'
57
  location = 'Fes, Morocco'
58
- soil_type = 'loam soil'
59
- humidity = '40%'
60
- weather = 'Sunny/Haze'
61
- temp = '10C'
62
  # agriculture = 'olives'
63
 
64
  # Add your chatbot logic here
 
5
  from streamlit_extras.add_vertical_space import add_vertical_space
6
  import requests
7
  from gradio_client import Client
8
+ import datetime as dt
9
+ import urllib.parse
10
+
11
 
12
  st.set_page_config(page_title="HugChat - An LLM-powered Streamlit app")
13
 
 
35
  "Saharan zone": "Yermosols, associated with sierozems, lithosols, and saline soils"
36
  }
37
 
38
+
39
+
40
+ def get_weather_data(city):
41
+ base_url = "http://api.openweathermap.org/data/2.5/weather?"
42
+ api_key = "84f9ed7c3738f567e5f1cbf2068d96a6" # API key
43
+ encoded_api_key = urllib.parse.quote(api_key) # URL encoding the API key
44
+
45
+ url = base_url + "appid=" + encoded_api_key + "&q=" + city
46
+
47
+ try:
48
+ response = requests.get(url)
49
+ response.raise_for_status()
50
+ weather_data = response.json()
51
+
52
+ # Processing the weather data
53
+ temp_kelvin = weather_data['main']['temp']
54
+ humidity = weather_data['main']['humidity']
55
+ weather_condition = weather_data['weather'][0]['description']
56
+
57
+ # Converting temperature to Celsius
58
+ temp_celsius = temp_kelvin - 273.15
59
+
60
+ return {
61
+ "temperature": f"{temp_celsius:.2f}C",
62
+ "humidity": f"{humidity}%",
63
+ "weather_condition": weather_condition
64
+ }
65
+ except requests.exceptions.HTTPError as err:
66
+ print(f"HTTP error: {err}")
67
+ return None
68
+ except requests.exceptions.RequestException as e:
69
+ print(f"Error: {e}")
70
+ return None
71
+
72
+ # Example usage
73
+
74
+
75
  def query(payload):
76
  response = requests.post(API_URL, headers=headers, json=payload)
77
  return response.json()
 
91
  # Function to generate a response from the chatbot
92
  def generate_response(user_input,region):
93
 
94
+ city = "Fez"
95
+ weather_info = get_weather_data(city)
96
+ if weather_info:
97
+ print(weather_info)
98
+
99
  user_input_translated = str(translate(user_input, "Moroccan Arabic", "English"))
100
  name = 'Mohammed'
101
  date = 'December'
102
  location = 'Fes, Morocco'
103
+ soil_type = soil_types[region] # Use the selected region's soil type
104
+ humidity = weather_info["humidity"]
105
+ weather = weather_info["weather_condition"]
106
+ temp = weather_info["temperature"]
107
  # agriculture = 'olives'
108
 
109
  # Add your chatbot logic here