Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
from groq import Groq
|
| 5 |
+
|
| 6 |
+
# Set up API keys and URLs
|
| 7 |
+
google_api_key = "AIzaSyBP5XGE4ZtrH2nBJZb7qe3Pjhw61rQvjBM"
|
| 8 |
+
google_weather_api_url = "https://api.openweathermap.org/data/2.5/weather"
|
| 9 |
+
|
| 10 |
+
# Set up Groq API
|
| 11 |
+
groq_api_key = "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"
|
| 12 |
+
client = Groq(api_key=groq_api_key)
|
| 13 |
+
|
| 14 |
+
# Streamlit UI
|
| 15 |
+
st.title("Real-time Weather App with Groq Integration")
|
| 16 |
+
|
| 17 |
+
# City input
|
| 18 |
+
city = st.text_input("Enter city name")
|
| 19 |
+
|
| 20 |
+
# Weather data display
|
| 21 |
+
if city:
|
| 22 |
+
# Google Weather API request
|
| 23 |
+
params = {
|
| 24 |
+
"q": city,
|
| 25 |
+
"appid": google_api_key,
|
| 26 |
+
"units": "metric" # For temperature in Celsius; use "imperial" for Fahrenheit
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
response = requests.get(google_weather_api_url, params=params)
|
| 31 |
+
response.raise_for_status() # Will raise an HTTPError for bad responses
|
| 32 |
+
weather_data = response.json()
|
| 33 |
+
|
| 34 |
+
if weather_data.get("cod") != 200:
|
| 35 |
+
st.write(f"Error fetching weather data: {weather_data.get('message', 'Unknown error')}")
|
| 36 |
+
else:
|
| 37 |
+
# Display weather data
|
| 38 |
+
st.write("Current Weather:")
|
| 39 |
+
st.write(f"Temperature: {weather_data['main']['temp']}°C")
|
| 40 |
+
st.write(f"Weather: {weather_data['weather'][0]['description'].capitalize()}")
|
| 41 |
+
st.write(f"Humidity: {weather_data['main']['humidity']}%")
|
| 42 |
+
st.write(f"Wind Speed: {weather_data['wind']['speed']} m/s")
|
| 43 |
+
|
| 44 |
+
except requests.exceptions.RequestException as e:
|
| 45 |
+
st.write(f"Error fetching weather data: {e}")
|
| 46 |
+
|
| 47 |
+
# Groq API request
|
| 48 |
+
try:
|
| 49 |
+
chat_completion = client.chat.completions.create(
|
| 50 |
+
messages=[
|
| 51 |
+
{
|
| 52 |
+
"role": "user",
|
| 53 |
+
"content": "Explain the importance of fast language models",
|
| 54 |
+
}
|
| 55 |
+
],
|
| 56 |
+
model="llama3-8b-8192",
|
| 57 |
+
)
|
| 58 |
+
st.write("Groq AI Response:")
|
| 59 |
+
st.write(chat_completion.choices[0].message.content)
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
st.write(f"Error fetching Groq data: {e}")
|
| 63 |
+
|
| 64 |
+
# Run the Streamlit app with `streamlit run your_script.py`
|