Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,76 +1,94 @@
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
import requests
|
4 |
|
5 |
-
#
|
6 |
-
st.set_page_config(page_title="ClimateGPT
|
7 |
|
8 |
-
#
|
9 |
st.title("π ClimateGPT: Your Eco-Education Buddy")
|
10 |
-
st.markdown("
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
#
|
14 |
-
|
|
|
|
|
|
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
return pipeline("text2text-generation", model="google/flan-t5-small")
|
20 |
|
21 |
-
|
|
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
}
|
36 |
-
try:
|
37 |
-
response = requests.post(url, json=payload, headers=headers, timeout=10)
|
38 |
-
data = response.json()
|
39 |
-
co2 = data.get('co2e', 0)
|
40 |
-
return f"{co2:.2f} kg COβ for {distance_km} km"
|
41 |
-
except requests.exceptions.RequestException:
|
42 |
-
return "Emission data not available."
|
43 |
|
44 |
-
#
|
45 |
-
def translate_text(text, target_lang
|
46 |
-
if target_lang == "en":
|
47 |
-
return text
|
48 |
try:
|
49 |
-
|
|
|
|
|
50 |
"q": text,
|
51 |
"source": "en",
|
52 |
"target": target_lang,
|
53 |
"format": "text"
|
54 |
-
}
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
#
|
60 |
-
st.
|
61 |
-
|
62 |
-
target_language = st.selectbox("π Translate the response to:", ["None", "en", "es", "fr", "de", "ur", "zh"], index=0)
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
import streamlit as st
|
4 |
from transformers import pipeline
|
5 |
import requests
|
6 |
|
7 |
+
# π’ Streamlit Page Configuration (must be first Streamlit command)
|
8 |
+
st.set_page_config(page_title="ClimateGPT", layout="centered")
|
9 |
|
10 |
+
# π App Title and Intro
|
11 |
st.title("π ClimateGPT: Your Eco-Education Buddy")
|
12 |
+
st.markdown("""
|
13 |
+
Welcome to **ClimateGPT**, your personal assistant for learning about climate change, sustainability, and carbon footprint.
|
14 |
+
Ask a question in any supported language, and get informative answers with optional translation!
|
15 |
+
""")
|
16 |
|
17 |
+
# π Climate Topics for Filtering
|
18 |
+
CLIMATE_TOPICS = [
|
19 |
+
"climate", "carbon", "recycle", "emission", "eco", "green",
|
20 |
+
"pollution", "environment", "sustainability", "energy", "co2"
|
21 |
+
]
|
22 |
|
23 |
+
# β
Relevance Check
|
24 |
+
def is_relevant_query(query):
|
25 |
+
return any(word in query.lower() for word in CLIMATE_TOPICS)
|
|
|
26 |
|
27 |
+
# π§ Load the lightweight model for faster response
|
28 |
+
llm = pipeline("text2text-generation", model="google/flan-t5-small")
|
29 |
|
30 |
+
# π Language Mapping for LibreTranslate
|
31 |
+
LANGUAGES = {
|
32 |
+
"English": "en",
|
33 |
+
"Urdu": "ur",
|
34 |
+
"Spanish": "es",
|
35 |
+
"French": "fr",
|
36 |
+
"German": "de",
|
37 |
+
"Chinese": "zh"
|
38 |
+
}
|
39 |
+
|
40 |
+
# π LibreTranslate API Endpoint
|
41 |
+
TRANSLATE_API = "https://libretranslate.de/translate"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
# π Translation function
|
44 |
+
def translate_text(text, target_lang):
|
|
|
|
|
45 |
try:
|
46 |
+
if target_lang == "en":
|
47 |
+
return text
|
48 |
+
payload = {
|
49 |
"q": text,
|
50 |
"source": "en",
|
51 |
"target": target_lang,
|
52 |
"format": "text"
|
53 |
+
}
|
54 |
+
response = requests.post(TRANSLATE_API, json=payload, timeout=10)
|
55 |
+
return response.json().get("translatedText", text)
|
56 |
+
except Exception as e:
|
57 |
+
return f"Translation failed: {e}"
|
58 |
+
|
59 |
+
# π Carbon Emission Estimation via Climatiq API
|
60 |
+
def get_carbon_estimate(distance_km):
|
61 |
+
headers = {
|
62 |
+
"Authorization": f"Bearer {st.secrets['climate_api']}"
|
63 |
+
}
|
64 |
+
payload = {
|
65 |
+
"emission_factor": {"activity_id": "passenger_vehicle-vehicle_type_car-fuel_source_na-engine_size_na-vehicle_age_na"},
|
66 |
+
"parameters": {"distance": distance_km, "distance_unit": "km"}
|
67 |
+
}
|
68 |
+
try:
|
69 |
+
response = requests.post("https://beta3.api.climatiq.io/estimate", headers=headers, json=payload)
|
70 |
+
emissions = response.json()["co2e"]
|
71 |
+
return f"Estimated COβ Emission: {emissions:.2f} kg for {distance_km} km"
|
72 |
+
except:
|
73 |
+
return "Failed to fetch emission estimate."
|
74 |
|
75 |
+
# π§Ύ User Inputs
|
76 |
+
user_query = st.text_input("Ask your climate-related question:")
|
77 |
+
language = st.selectbox("Choose output language:", list(LANGUAGES.keys()))
|
|
|
78 |
|
79 |
+
# βοΈ Optional Carbon Estimate
|
80 |
+
with st.expander("Optional: Estimate COβ emissions for travel"):
|
81 |
+
distance = st.number_input("Enter travel distance in kilometers:", min_value=0.0, step=1.0)
|
82 |
+
if distance:
|
83 |
+
st.info(get_carbon_estimate(distance))
|
84 |
|
85 |
+
# π€ Process Query
|
86 |
+
if user_query:
|
87 |
+
if is_relevant_query(user_query):
|
88 |
+
response = llm(user_query, max_length=200, do_sample=False)[0]['generated_text']
|
89 |
+
else:
|
90 |
+
response = "β This chatbot only answers questions related to climate, sustainability, and eco-friendly habits."
|
91 |
|
92 |
+
translated_response = translate_text(response, LANGUAGES[language])
|
93 |
+
st.markdown("---")
|
94 |
+
st.markdown(f"**Response:** {translated_response}")
|