| import gradio as gr |
| import requests |
| import os |
| import groq |
| from dotenv import load_dotenv |
|
|
| |
| load_dotenv() |
|
|
| |
| WAQI_API_KEY = os.getenv("WAQI_API_KEY") |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") |
|
|
| |
| WAQI_BASE_URL = "https://api.waqi.info/feed/{}/?token={}" |
|
|
| |
| def get_aqi(location: str): |
| location = location.strip() |
| url = WAQI_BASE_URL.format(location, WAQI_API_KEY) |
| response = requests.get(url) |
| |
| if response.status_code == 200: |
| data = response.json() |
| if data["status"] == "ok": |
| aqi = data["data"]["aqi"] |
| return aqi |
| else: |
| return None |
| return None |
|
|
| |
| def get_health_suggestions(aqi: int, health_conditions: str): |
| |
| prompt = f"Given an AQI of {aqi}, provide health advice for someone with the following conditions: {health_conditions}." |
|
|
| |
| client = groq.Groq(api_key=GROQ_API_KEY) |
|
|
| try: |
| |
| chat_completion = client.chat.completions.create( |
| messages=[{"role": "user", "content": prompt}], |
| model="llama-3.3-70b-versatile", |
| ) |
| |
| |
| health_advice = chat_completion.choices[0].message.content.strip() |
| return health_advice |
| except Exception as e: |
| return f"Error fetching advice: {e}" |
|
|
| |
| def chatbot(city_name, health_conditions): |
| |
| aqi = get_aqi(city_name) |
| if aqi: |
| |
| health_advice = get_health_suggestions(aqi, health_conditions) |
| return f"Current AQI in {city_name}: {aqi}\nHealth Suggestions: {health_advice}" |
| else: |
| return f"Could not retrieve AQI data for {city_name}. Please check the location name and try again." |
|
|
| |
| city_list = ["Lahore", "Karachi", "Islamabad", "Peshawar", "Faisalabad"] |
|
|
| |
| gr.Interface( |
| fn=chatbot, |
| inputs=[ |
| gr.Dropdown(choices=city_list, label="Select Your City"), |
| gr.Textbox(placeholder="Enter health conditions (e.g., asthma, allergies)", label="Health Conditions") |
| ], |
| outputs="text", |
| live=True |
| ).launch() |
|
|
|
|