Spaces:
Sleeping
Sleeping
vighnesh-shetty-vs commited on
Commit ·
0461653
1
Parent(s): 4a9d6cb
Add updated files
Browse files
app.py
CHANGED
|
@@ -8,76 +8,208 @@ from game_data import calculate_impact, MODELS
|
|
| 8 |
from dialogues import get_dialogue
|
| 9 |
from alternatives import ALTERNATIVES
|
| 10 |
|
|
|
|
|
|
|
| 11 |
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def get_relatable_translations(impact):
|
| 14 |
"""Converts abstract numbers into physical examples."""
|
| 15 |
-
# Water: 1 bottle = 500mL
|
| 16 |
bottles = impact['water_ml'] / 500.0
|
| 17 |
w_ex = f"{bottles:.1f} water bottles" if bottles >= 0.1 else "A few sips"
|
| 18 |
|
| 19 |
-
# Energy: 10 LED bulbs (10W each) for 1 hour = 100Wh
|
| 20 |
-
# Let's use 1 LED bulb for X hours
|
| 21 |
bulb_hours = impact['energy_wh'] / 10.0
|
| 22 |
e_ex = f"{bulb_hours:.1f} hours of 1 LED bulb" if bulb_hours >= 0.1 else f"{impact['energy_wh']*12:.1f} mins of 1 LED bulb"
|
| 23 |
|
| 24 |
-
# CO2: 1 phone charge = ~5g
|
| 25 |
charges = impact['co2_g'] / 5.0
|
| 26 |
c_ex = f"{charges:.1f} phone charges" if charges >= 0.1 else "Negligible emissions"
|
| 27 |
|
| 28 |
return w_ex, e_ex, c_ex
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def get_impact_cards(impact, category):
|
| 31 |
w_ex, e_ex, c_ex = get_relatable_translations(impact)
|
| 32 |
return f"""
|
| 33 |
<div class="impact-container">
|
| 34 |
<div class="impact-card bad-impact">
|
| 35 |
<h3 style="margin: 0 0 10px 0; color: #fca5a5;">🔥 LLM Impact</h3>
|
| 36 |
-
<p>💧 <strong>{impact['water_ml']} mL</strong>
|
| 37 |
-
<p>⚡ <strong>{impact['energy_wh']} Wh</strong>
|
| 38 |
-
<p>☁️ <strong>{impact['co2_g']} g</strong>
|
| 39 |
</div>
|
| 40 |
<div class="impact-card good-impact">
|
| 41 |
<h3 style="margin: 0 0 10px 0; color: #86efac;">🌿 Eco-Alternative</h3>
|
| 42 |
-
<p><strong>~0.0 mL</strong> Water
|
| 43 |
-
<p style="
|
|
|
|
| 44 |
</div>
|
| 45 |
</div>
|
| 46 |
"""
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def generate_victory_dashboard(state):
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
rows = ""
|
| 54 |
for item in state["history"]:
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
rows += f"""
|
| 57 |
-
<tr style="border-bottom: 1px solid rgba(255,255,255,0.1);">
|
| 58 |
-
<td style="padding:
|
| 59 |
-
<td style="padding:
|
| 60 |
-
<td style="padding:
|
| 61 |
-
<td style="padding:
|
| 62 |
</tr>
|
| 63 |
"""
|
| 64 |
|
| 65 |
return f"""
|
| 66 |
-
<div class="victory-
|
| 67 |
-
<div class="
|
| 68 |
-
<h1
|
| 69 |
-
<p style="text-align: center; font-size: 1.
|
| 70 |
|
| 71 |
-
<div class="
|
| 72 |
-
<div class="v-box">
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
</div>
|
| 75 |
|
| 76 |
-
<h3 style="margin-top:
|
| 77 |
-
<div style="overflow-
|
| 78 |
-
<table style="width: 100%;
|
| 79 |
-
<tr style="background: rgba(
|
| 80 |
-
<th style="padding:
|
|
|
|
|
|
|
|
|
|
| 81 |
</tr>
|
| 82 |
{rows}
|
| 83 |
</table>
|
|
@@ -85,44 +217,192 @@ def generate_victory_dashboard(state):
|
|
| 85 |
</div>
|
| 86 |
"""
|
| 87 |
|
| 88 |
-
# ... [The custom_css should include .pyro animations for the 'blast' effect] ...
|
| 89 |
-
|
| 90 |
def process_query(user_input, selected_model, state):
|
| 91 |
if state["game_over"] or not user_input.strip():
|
| 92 |
-
return [state] + [gr.update()]*
|
| 93 |
|
| 94 |
result = classifier(user_input, CATEGORIES)
|
| 95 |
category = result['labels'][0]
|
| 96 |
confidence = result['scores'][0]
|
|
|
|
| 97 |
impact = calculate_impact(category, confidence, user_input, selected_model)
|
| 98 |
|
| 99 |
-
# Update State [cite: 12, 37]
|
| 100 |
state["queries"] += 1
|
| 101 |
state["water"] = max(0, state["water"] - impact["water_l"])
|
| 102 |
state["energy"] += impact["energy_kwh"]
|
| 103 |
state["co2"] += impact["co2_g"]
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
dialogue = get_dialogue(state["water"], category)
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
state["history"].append({
|
| 112 |
-
"query": user_input,
|
| 113 |
-
"
|
|
|
|
|
|
|
|
|
|
| 114 |
})
|
| 115 |
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
if state["points"] >= 500 or state["water"] <= 0:
|
| 118 |
state["game_over"] = True
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
gr.update(
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
return (state,
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from dialogues import get_dialogue
|
| 9 |
from alternatives import ALTERNATIVES
|
| 10 |
|
| 11 |
+
# Added flush=True so we can always see this in the Hugging Face logs
|
| 12 |
+
print("Loading classification model...", flush=True)
|
| 13 |
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 14 |
|
| 15 |
+
CATEGORIES = [
|
| 16 |
+
"simple factual question",
|
| 17 |
+
"mathematical calculation",
|
| 18 |
+
"creative content generation",
|
| 19 |
+
"complex research query"
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
def generate_audio(text):
|
| 23 |
+
tts = gTTS(text, lang='en')
|
| 24 |
+
filepath = "temp_drip_voice.mp3"
|
| 25 |
+
tts.save(filepath)
|
| 26 |
+
return filepath
|
| 27 |
+
|
| 28 |
def get_relatable_translations(impact):
|
| 29 |
"""Converts abstract numbers into physical examples."""
|
|
|
|
| 30 |
bottles = impact['water_ml'] / 500.0
|
| 31 |
w_ex = f"{bottles:.1f} water bottles" if bottles >= 0.1 else "A few sips"
|
| 32 |
|
|
|
|
|
|
|
| 33 |
bulb_hours = impact['energy_wh'] / 10.0
|
| 34 |
e_ex = f"{bulb_hours:.1f} hours of 1 LED bulb" if bulb_hours >= 0.1 else f"{impact['energy_wh']*12:.1f} mins of 1 LED bulb"
|
| 35 |
|
|
|
|
| 36 |
charges = impact['co2_g'] / 5.0
|
| 37 |
c_ex = f"{charges:.1f} phone charges" if charges >= 0.1 else "Negligible emissions"
|
| 38 |
|
| 39 |
return w_ex, e_ex, c_ex
|
| 40 |
|
| 41 |
+
def get_stats_html(water, energy, co2, points):
|
| 42 |
+
water_percent = max(0, min(100, (water / 10.0) * 100))
|
| 43 |
+
return f"""
|
| 44 |
+
<div class="stats-container">
|
| 45 |
+
<div class="stat-card water-card">
|
| 46 |
+
<div class="stat-label">WATER TANK (10L MAX)</div>
|
| 47 |
+
<div class="stat-value">{water:.2f}L</div>
|
| 48 |
+
<div class="progress-bar-bg">
|
| 49 |
+
<div class="progress-bar-fill" style="width: {water_percent}%;"></div>
|
| 50 |
+
</div>
|
| 51 |
+
</div>
|
| 52 |
+
<div class="stat-card energy-card">
|
| 53 |
+
<div class="stat-label">ENERGY WASTED</div>
|
| 54 |
+
<div class="stat-value">{energy:.4f} <span style="font-size: 0.5em;">kWh</span></div>
|
| 55 |
+
</div>
|
| 56 |
+
<div class="stat-card co2-card">
|
| 57 |
+
<div class="stat-label">CARBON (CO₂e)</div>
|
| 58 |
+
<div class="stat-value">{co2:.2f}g</div>
|
| 59 |
+
</div>
|
| 60 |
+
<div class="stat-card points-card">
|
| 61 |
+
<div class="stat-label">SCORE</div>
|
| 62 |
+
<div class="stat-value">{points}</div>
|
| 63 |
+
</div>
|
| 64 |
+
</div>
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
def get_drip_visuals(water_level):
|
| 68 |
+
if water_level > 7.0:
|
| 69 |
+
g_start, g_end = "#7dd3fc", "#0369a1"
|
| 70 |
+
mouth_d = "M43 112 Q60 122 77 112"
|
| 71 |
+
cracks_op, sweat_op, tear_op = "0", "0", "0"
|
| 72 |
+
mood, mood_color = "Hydrated and coping", "rgb(34, 211, 238)"
|
| 73 |
+
anim = "bounce 2s infinite ease-in-out"
|
| 74 |
+
elif 3.0 <= water_level <= 7.0:
|
| 75 |
+
g_start, g_end = "#38bdf8", "#0284c7"
|
| 76 |
+
mouth_d = "M43 112 Q60 110 77 112"
|
| 77 |
+
cracks_op, sweat_op, tear_op = "0", "1", "0"
|
| 78 |
+
mood, mood_color = "Shrinking & Sweaty", "#0ea5e9"
|
| 79 |
+
anim = "float 4s infinite ease-in-out"
|
| 80 |
+
elif water_level > 0:
|
| 81 |
+
g_start, g_end = "#fca5a5", "#b91c1c"
|
| 82 |
+
mouth_d = "M43 116 Q60 102 77 116"
|
| 83 |
+
cracks_op, sweat_op, tear_op = "1", "0", "1"
|
| 84 |
+
mood, mood_color = "Overheating & Raspy", "#ef4444"
|
| 85 |
+
anim = "shake 0.5s infinite"
|
| 86 |
+
else:
|
| 87 |
+
g_start, g_end = "#94a3b8", "#334155"
|
| 88 |
+
mouth_d = "M40 112 Q45 105 50 112 T60 112 T70 112 T80 112"
|
| 89 |
+
cracks_op, sweat_op, tear_op = "0", "0", "0"
|
| 90 |
+
mood, mood_color = "Evaporated!", "#94a3b8"
|
| 91 |
+
anim = "floatUp 3s forwards"
|
| 92 |
+
|
| 93 |
+
return f"""
|
| 94 |
+
<div class="drip-panel" style="animation: {anim}; display: flex; flex-direction: column; align-items: center; justify-content: center; width: 100%;">
|
| 95 |
+
<div class="drip-lbl" style="font-weight: 800; color: #94a3b8; letter-spacing: 3px; font-size: 14px; margin-bottom: 5px;">DRIP</div>
|
| 96 |
+
<svg id="drip-svg" viewBox="0 0 120 148" style="width: 140px; height: 160px; overflow: visible;" xmlns="http://www.w3.org/2000/svg">
|
| 97 |
+
<defs>
|
| 98 |
+
<radialGradient id="dg" cx="38%" cy="32%">
|
| 99 |
+
<stop id="dg1" offset="0%" stop-color="{g_start}"></stop>
|
| 100 |
+
<stop id="dg2" offset="100%" stop-color="{g_end}"></stop>
|
| 101 |
+
</radialGradient>
|
| 102 |
+
</defs>
|
| 103 |
+
<path id="dbody" d="M60 8 C60 8,102 66,102 90 C102 118,83 137,60 137 C37 137,18 118,18 90 C18 66,60 8,60 8Z" fill="url(#dg)" stroke="rgba(34,211,238,0.35)" stroke-width="1.5" opacity="1"></path>
|
| 104 |
+
<ellipse cx="42" cy="70" rx="9" ry="13" fill="rgba(255,255,255,0.22)" transform="rotate(-18,42,70)"></ellipse>
|
| 105 |
+
<circle cx="44" cy="90" r="10" fill="white"></circle>
|
| 106 |
+
<circle cx="76" cy="90" r="10" fill="white"></circle>
|
| 107 |
+
<circle id="pl" cx="45" cy="91" r="6" fill="#0f172a"></circle>
|
| 108 |
+
<circle id="pr" cx="77" cy="91" r="6" fill="#0f172a"></circle>
|
| 109 |
+
<circle cx="47" cy="88" r="2.5" fill="white"></circle>
|
| 110 |
+
<circle cx="79" cy="88" r="2.5" fill="white"></circle>
|
| 111 |
+
<path id="dm" d="{mouth_d}" stroke="#0f172a" stroke-width="2.5" stroke-linecap="round" fill="none"></path>
|
| 112 |
+
<g id="cracks" opacity="{cracks_op}" style="transition: opacity 0.5s;">
|
| 113 |
+
<path d="M80 46 L86 58 L79 65 L85 77" stroke="#bae6fd" stroke-width="1.3" stroke-linecap="round" fill="none" opacity=".7"></path>
|
| 114 |
+
<path d="M37 82 L31 93 L39 99" stroke="#bae6fd" stroke-width="1.3" stroke-linecap="round" fill="none" opacity=".7"></path>
|
| 115 |
+
</g>
|
| 116 |
+
<path id="sweat" d="M108 76 C110 70,116 70,116 78 C116 84,110 88,108 82Z" fill="#7dd3fc" opacity="{sweat_op}" style="transition: opacity 0.5s;"></path>
|
| 117 |
+
<path id="tear" d="M38 104 C37 108,33 108,33 112 C33 115,36 117,38 114 C40 117,43 115,43 112 C43 108,39 108,38 104Z" fill="#93c5fd" opacity="{tear_op}" style="transition: opacity 0.5s;"></path>
|
| 118 |
+
</svg>
|
| 119 |
+
<div class="drip-mood" id="drip-mood" style="color: {mood_color}; font-weight: 500; font-size: 1.1rem; margin-top: 15px;">{mood}</div>
|
| 120 |
+
<div style="text-align:center; margin-top:10px;">
|
| 121 |
+
<div style="font-size:10px; color:#94a3b8; text-transform:uppercase; letter-spacing:1px;">Water Left</div>
|
| 122 |
+
<div style="font-size:26px; font-weight:700; color:{mood_color};" id="d-water">{water_level:.2f}L</div>
|
| 123 |
+
</div>
|
| 124 |
+
</div>
|
| 125 |
+
"""
|
| 126 |
+
|
| 127 |
def get_impact_cards(impact, category):
|
| 128 |
w_ex, e_ex, c_ex = get_relatable_translations(impact)
|
| 129 |
return f"""
|
| 130 |
<div class="impact-container">
|
| 131 |
<div class="impact-card bad-impact">
|
| 132 |
<h3 style="margin: 0 0 10px 0; color: #fca5a5;">🔥 LLM Impact</h3>
|
| 133 |
+
<p style="margin:4px 0; font-size:1.1em;">💧 <strong>{impact['water_ml']} mL</strong><br><small>({w_ex})</small></p>
|
| 134 |
+
<p style="margin:4px 0; font-size:1.1em;">⚡ <strong>{impact['energy_wh']} Wh</strong><br><small>({e_ex})</small></p>
|
| 135 |
+
<p style="margin:4px 0; font-size:1.1em;">☁️ <strong>{impact['co2_g']} g</strong><br><small>({c_ex})</small></p>
|
| 136 |
</div>
|
| 137 |
<div class="impact-card good-impact">
|
| 138 |
<h3 style="margin: 0 0 10px 0; color: #86efac;">🌿 Eco-Alternative</h3>
|
| 139 |
+
<p style="margin:4px 0; font-size:1.1em;"><strong>~0.0 mL</strong> Water</p>
|
| 140 |
+
<p style="margin:4px 0; font-size:1.1em;"><strong>~0.001 Wh</strong> Energy</p>
|
| 141 |
+
<p style="margin:4px 0; font-size:1.1em;"><strong>~0.01 g</strong> CO₂e</p>
|
| 142 |
</div>
|
| 143 |
</div>
|
| 144 |
"""
|
| 145 |
|
| 146 |
+
def format_alt_button(text, impact):
|
| 147 |
+
icon = "💡"
|
| 148 |
+
if "Search" in text or "Google" in text: icon = "🔍"
|
| 149 |
+
elif "Wikipedia" in text or "Encyclopedia" in text: icon = "📚"
|
| 150 |
+
elif "Expert" in text or "Quora" in text: icon = "👤"
|
| 151 |
+
elif "Wolfram" in text or "Geogebra" in text or "Desmos" in text: icon = "🧮"
|
| 152 |
+
elif "Reddit" in text or "Gutenberg" in text or "Thesaurus" in text: icon = "📝"
|
| 153 |
+
return f"{icon} {text}\n\n🏆 Score: +10 Pts\n💧 Save: {impact['water_ml']} mL\n⚡ Save: {impact['energy_wh']} Wh\n☁️ Save: {impact['co2_g']} g"
|
| 154 |
+
|
| 155 |
def generate_victory_dashboard(state):
|
| 156 |
+
total_w_saved = sum(item['water_ml'] for item in state["history"] if item['alt_name'] != "None (Justified Use)")
|
| 157 |
+
total_e_saved = sum(item['energy_wh'] for item in state["history"] if item['alt_name'] != "None (Justified Use)")
|
| 158 |
+
total_c_saved = sum(item['co2_g'] for item in state["history"] if item['alt_name'] != "None (Justified Use)")
|
| 159 |
|
| 160 |
rows = ""
|
| 161 |
for item in state["history"]:
|
| 162 |
+
if item['alt_name'] == "None (Justified Use)":
|
| 163 |
+
saved_w, saved_e, saved_c = 0.0, 0.0, 0.0
|
| 164 |
+
alt_display = "<span style='color: #94a3b8;'>Justified Use</span>"
|
| 165 |
+
else:
|
| 166 |
+
saved_w = item['water_ml']
|
| 167 |
+
saved_e = max(0, item['energy_wh'] - 0.001)
|
| 168 |
+
saved_c = max(0, item['co2_g'] - 0.01)
|
| 169 |
+
alt_display = f"<span style='color: #34d399;'>{item['alt_name']}</span>"
|
| 170 |
+
|
| 171 |
rows += f"""
|
| 172 |
+
<tr style="border-bottom: 1px solid rgba(255,255,255,0.1); background: rgba(0,0,0,0.2);">
|
| 173 |
+
<td style="padding: 15px; font-style: italic;">"{item['query']}"</td>
|
| 174 |
+
<td style="padding: 15px; color: #fca5a5;">{item['water_ml']:.1f}mL / {item['energy_wh']:.2f}Wh / {item['co2_g']:.2f}g</td>
|
| 175 |
+
<td style="padding: 15px;">{alt_display}</td>
|
| 176 |
+
<td style="padding: 15px; color: #60a5fa; font-weight: bold;">{saved_w:.1f}mL / {saved_e:.2f}Wh / {saved_c:.2f}g</td>
|
| 177 |
</tr>
|
| 178 |
"""
|
| 179 |
|
| 180 |
return f"""
|
| 181 |
+
<div class="victory-dashboard">
|
| 182 |
+
<div class="confetti"></div>
|
| 183 |
+
<h1 class="victory-title">🏆 PLANET SAVED! 🏆</h1>
|
| 184 |
+
<p style="text-align: center; font-size: 1.3em; color: #a7f3d0; margin-bottom: 30px;">You reached {state['points']} points and kept Drip alive!</p>
|
| 185 |
|
| 186 |
+
<div class="victory-stats-row">
|
| 187 |
+
<div class="v-stat-box">
|
| 188 |
+
<h4>Final Water</h4>
|
| 189 |
+
<h2>{state['water']:.2f} L</h2>
|
| 190 |
+
</div>
|
| 191 |
+
<div class="v-stat-box" style="border-color: #34d399;">
|
| 192 |
+
<h4 style="color: #34d399;">Planetary Savings</h4>
|
| 193 |
+
<p style="color: #34d399; margin: 5px 0;">💧 {total_w_saved:.1f} mL Saved</p>
|
| 194 |
+
<p style="color: #facc15; margin: 5px 0;">⚡ {total_e_saved:.2f} Wh Saved</p>
|
| 195 |
+
<p style="color: #a8a29e; margin: 5px 0;">☁️ {total_c_saved:.2f} g Saved</p>
|
| 196 |
+
</div>
|
| 197 |
+
<div class="v-stat-box" style="border-color: #fca5a5;">
|
| 198 |
+
<h4 style="color: #fca5a5;">Total Usage</h4>
|
| 199 |
+
<p style="color: #fca5a5; margin: 5px 0;">💧 {10.0 - state['water']:.2f} L</p>
|
| 200 |
+
<p style="color: #fca5a5; margin: 5px 0;">⚡ {state['energy']:.4f} kWh</p>
|
| 201 |
+
<p style="color: #fca5a5; margin: 5px 0;">☁️ {state['co2']:.2f} g</p>
|
| 202 |
+
</div>
|
| 203 |
</div>
|
| 204 |
|
| 205 |
+
<h3 style="margin-top: 40px; color: #38bdf8; font-size: 1.8em;">📊 AI Usage Audit Log</h3>
|
| 206 |
+
<div style="max-height: 400px; overflow-y: auto; background: rgba(15, 23, 42, 0.9); border-radius: 12px; border: 1px solid rgba(255,255,255,0.1);">
|
| 207 |
+
<table style="width: 100%; text-align: left; border-collapse: collapse; font-size: 1em;">
|
| 208 |
+
<tr style="background: rgba(56, 189, 248, 0.1); border-bottom: 2px solid #38bdf8;">
|
| 209 |
+
<th style="padding: 15px;">User Query</th>
|
| 210 |
+
<th style="padding: 15px;">LLM Cost (Wasted)</th>
|
| 211 |
+
<th style="padding: 15px;">Best Alternative</th>
|
| 212 |
+
<th style="padding: 15px;">Potential Savings</th>
|
| 213 |
</tr>
|
| 214 |
{rows}
|
| 215 |
</table>
|
|
|
|
| 217 |
</div>
|
| 218 |
"""
|
| 219 |
|
|
|
|
|
|
|
| 220 |
def process_query(user_input, selected_model, state):
|
| 221 |
if state["game_over"] or not user_input.strip():
|
| 222 |
+
return [state, gr.update()] + [gr.update()]*10 + [gr.update(visible=True), gr.update(visible=False), gr.update()]
|
| 223 |
|
| 224 |
result = classifier(user_input, CATEGORIES)
|
| 225 |
category = result['labels'][0]
|
| 226 |
confidence = result['scores'][0]
|
| 227 |
+
|
| 228 |
impact = calculate_impact(category, confidence, user_input, selected_model)
|
| 229 |
|
|
|
|
| 230 |
state["queries"] += 1
|
| 231 |
state["water"] = max(0, state["water"] - impact["water_l"])
|
| 232 |
state["energy"] += impact["energy_kwh"]
|
| 233 |
state["co2"] += impact["co2_g"]
|
| 234 |
|
| 235 |
+
show_alts = False
|
| 236 |
+
alt_choices = ["", "", ""]
|
| 237 |
+
best_alt_name = "None (Justified Use)"
|
|
|
|
|
|
|
| 238 |
|
| 239 |
+
if category == "complex research query":
|
| 240 |
+
state["points"] += 50
|
| 241 |
+
dialogue = f"A legitimate research query! Finally, a worthy use of my massive architecture. Drain away!"
|
| 242 |
+
else:
|
| 243 |
+
show_alts = True
|
| 244 |
+
raw_alts = ALTERNATIVES.get(category, [{"text": "Google", "url": "https://google.com"}])
|
| 245 |
+
state["current_best_alt"] = raw_alts[0]
|
| 246 |
+
best_alt_name = raw_alts[0]["text"]
|
| 247 |
+
random.shuffle(raw_alts)
|
| 248 |
+
state["current_shuffled_alts"] = raw_alts
|
| 249 |
+
alt_choices = [format_alt_button(a["text"], impact) for a in raw_alts]
|
| 250 |
+
dialogue = get_dialogue(state["water"], category)
|
| 251 |
+
|
| 252 |
state["history"].append({
|
| 253 |
+
"query": user_input,
|
| 254 |
+
"water_ml": impact["water_ml"],
|
| 255 |
+
"energy_wh": impact["energy_wh"],
|
| 256 |
+
"co2_g": impact["co2_g"],
|
| 257 |
+
"alt_name": best_alt_name
|
| 258 |
})
|
| 259 |
|
| 260 |
+
stats_ui = get_stats_html(state["water"], state["energy"], state["co2"], state["points"])
|
| 261 |
+
drip_ui = get_drip_visuals(state["water"])
|
| 262 |
+
impact_ui = get_impact_cards(impact, category)
|
| 263 |
+
audio_path = generate_audio(dialogue)
|
| 264 |
+
feedback_text = f"<div class='feedback-box'>🧠 <strong>{category.upper()}</strong> Detected!<br><br><em>\"{dialogue}\"</em></div>"
|
| 265 |
+
|
| 266 |
if state["points"] >= 500 or state["water"] <= 0:
|
| 267 |
state["game_over"] = True
|
| 268 |
+
victory_html = generate_victory_dashboard(state)
|
| 269 |
+
return (state, stats_ui, drip_ui, audio_path, impact_ui, gr.update(), gr.update(),
|
| 270 |
+
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(value=""),
|
| 271 |
+
gr.update(visible=False), gr.update(visible=True), gr.update(value=victory_html))
|
| 272 |
+
|
| 273 |
+
return (state, stats_ui, drip_ui, audio_path, impact_ui, gr.update(value=feedback_text), gr.update(visible=show_alts),
|
| 274 |
+
gr.update(value=alt_choices[0]), gr.update(value=alt_choices[1]), gr.update(value=alt_choices[2]), gr.update(value=""), gr.update(value=""),
|
| 275 |
+
gr.update(visible=True), gr.update(visible=False), gr.update())
|
| 276 |
+
|
| 277 |
+
def select_alternative(choice_text, state):
|
| 278 |
+
if state["game_over"] or not state.get("current_best_alt"):
|
| 279 |
+
return state, gr.update(), gr.update(), gr.update(), gr.update(visible=True), gr.update(visible=False), gr.update()
|
| 280 |
+
|
| 281 |
+
clean_choice = choice_text.split("\n")[0][2:].strip()
|
| 282 |
+
selected_dict = next((item for item in state["current_shuffled_alts"] if item["text"] == clean_choice), None)
|
| 283 |
+
|
| 284 |
+
if clean_choice == state["current_best_alt"]["text"]:
|
| 285 |
+
state["points"] += 10
|
| 286 |
+
msg = f"✅ **Excellent!** +10 Points.\n\n<a href='{selected_dict['url']}' target='_blank' style='display:inline-block; margin-top:10px; padding: 10px 20px; background:#10b981; color:white; text-decoration:none; border-radius:8px; font-weight:bold;'>👉 Click here to use {clean_choice}</a>"
|
| 287 |
+
else:
|
| 288 |
+
msg = f"⚠️ **Okay choice.** Better than an LLM, but there was a slightly more optimal tool.\n\n<a href='{selected_dict['url']}' target='_blank' style='display:inline-block; margin-top:10px; padding: 10px 20px; background:#38bdf8; color:white; text-decoration:none; border-radius:8px; font-weight:bold;'>👉 Click here to use {clean_choice}</a>"
|
| 289 |
+
|
| 290 |
+
stats_ui = get_stats_html(state["water"], state["energy"], state["co2"], state["points"])
|
| 291 |
+
|
| 292 |
+
if state["points"] >= 500:
|
| 293 |
+
state["game_over"] = True
|
| 294 |
+
victory_html = generate_victory_dashboard(state)
|
| 295 |
+
return state, stats_ui, gr.update(), gr.update(), gr.update(visible=False), gr.update(visible=True), gr.update(value=victory_html)
|
| 296 |
+
|
| 297 |
+
return state, stats_ui, gr.update(value=msg), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update()
|
| 298 |
+
|
| 299 |
+
custom_css = """
|
| 300 |
+
@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;500;700&display=swap');
|
| 301 |
+
body, .gradio-container { background-color: #0B1120 !important; font-family: 'Outfit', sans-serif !important; color: #e2e8f0 !important; }
|
| 302 |
+
.gradio-container { max-width: 950px !important; margin: auto; padding-top: 20px; }
|
| 303 |
+
h1 { text-align: center; color: #38bdf8; text-shadow: 0 0 15px rgba(56, 189, 248, 0.5); font-size: 2.5em !important; margin-bottom: 5px !important;}
|
| 304 |
+
.subtitle { text-align: center; color: #94a3b8; margin-bottom: 30px; font-weight: 300; }
|
| 305 |
+
|
| 306 |
+
.stats-container { display: flex; gap: 10px; justify-content: space-between; margin-bottom: 20px; }
|
| 307 |
+
.stat-card { flex: 1; padding: 15px 10px; border-radius: 12px; text-align: center; background: rgba(30, 41, 59, 0.6); border: 1px solid rgba(255,255,255,0.1); }
|
| 308 |
+
.water-card { box-shadow: 0 4px 20px rgba(56, 189, 248, 0.15); border-top: 2px solid #38bdf8; }
|
| 309 |
+
.energy-card { box-shadow: 0 4px 20px rgba(250, 204, 21, 0.15); border-top: 2px solid #facc15; }
|
| 310 |
+
.co2-card { box-shadow: 0 4px 20px rgba(168, 162, 158, 0.15); border-top: 2px solid #a8a29e; }
|
| 311 |
+
.points-card { box-shadow: 0 4px 20px rgba(52, 211, 153, 0.15); border-top: 2px solid #34d399; }
|
| 312 |
+
.stat-label { font-size: 0.75em; letter-spacing: 1px; opacity: 0.8; margin-bottom: 5px; }
|
| 313 |
+
.stat-value { font-size: 1.8em; font-weight: 700; color: white; }
|
| 314 |
+
.progress-bar-bg { background: #1e293b; height: 8px; border-radius: 4px; margin-top: 10px; overflow: hidden; }
|
| 315 |
+
.progress-bar-fill { background: linear-gradient(90deg, #0284c7, #38bdf8); height: 100%; transition: width 0.5s ease; }
|
| 316 |
+
|
| 317 |
+
@keyframes bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-15px); } }
|
| 318 |
+
@keyframes float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-5px); } }
|
| 319 |
+
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }
|
| 320 |
+
@keyframes floatUp { to { transform: translateY(-30px); opacity: 0; } }
|
| 321 |
+
|
| 322 |
+
.drip-pod { background: rgba(15, 23, 42, 0.6); border-radius: 24px; border: 1px solid rgba(56, 189, 248, 0.2); padding: 30px 20px; display: flex; flex-direction: column; align-items: center; box-shadow: inset 0 0 40px rgba(0,0,0,0.5); }
|
| 323 |
+
.drip-audio { background: transparent !important; border: none !important; box-shadow: none !important; width: 100%; max-width: 300px; filter: invert(1) hue-rotate(180deg) brightness(1.5); margin-bottom: 15px;}
|
| 324 |
+
.feedback-box { background: rgba(56, 189, 248, 0.1); border-left: 4px solid #38bdf8; padding: 15px; border-radius: 8px; font-size: 1.05em; color: #e2e8f0; width: 100%; text-align: left; line-height: 1.4; box-sizing: border-box;}
|
| 325 |
+
|
| 326 |
+
.input-row { align-items: stretch !important; margin: 10px 0; }
|
| 327 |
+
.custom-textbox textarea { background: rgba(30, 41, 59, 0.8) !important; border: 1px solid #38bdf8 !important; color: white !important; font-size: 1.1em !important; border-radius: 12px !important; box-shadow: inset 0 2px 10px rgba(0,0,0,0.3) !important; padding: 15px !important; resize: none !important; }
|
| 328 |
+
.custom-btn { background: linear-gradient(135deg, #0284c7, #0ea5e9) !important; border: none !important; color: white !important; font-weight: bold !important; font-size: 1.1em !important; border-radius: 12px !important; transition: all 0.3s !important; height: 100%; min-height: 85px; }
|
| 329 |
+
.custom-btn:hover { box-shadow: 0 0 20px rgba(56, 189, 248, 0.6) !important; transform: scale(1.02); }
|
| 330 |
+
|
| 331 |
+
.impact-container { display: flex; gap: 20px; width: 100%; }
|
| 332 |
+
.impact-card { flex: 1; padding: 20px; border-radius: 16px; border: 1px solid rgba(255,255,255,0.1); }
|
| 333 |
+
.bad-impact { background: linear-gradient(180deg, rgba(127, 29, 29, 0.3), rgba(69, 10, 10, 0.5)); border-top: 3px solid #ef4444; }
|
| 334 |
+
.good-impact { background: linear-gradient(180deg, rgba(20, 83, 45, 0.3), rgba(6, 78, 59, 0.5)); border-top: 3px solid #10b981; }
|
| 335 |
+
|
| 336 |
+
.alt-group { padding: 25px; background: linear-gradient(145deg, rgba(16, 185, 129, 0.05), rgba(6, 95, 70, 0.1)); border-radius: 20px; border: 1px solid rgba(16, 185, 129, 0.3); margin-top: 25px; box-shadow: 0 10px 30px rgba(0,0,0,0.2);}
|
| 337 |
+
.alt-card { background: linear-gradient(145deg, #1e293b, #0f172a) !important; border: 1px solid #10b981 !important; color: #a7f3d0 !important; height: auto !important; min-height: 180px !important; border-radius: 16px !important; font-size: 1.05em !important; font-weight: 500 !important; white-space: pre-wrap !important; line-height: 1.5 !important; padding: 15px !important; transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important; cursor: pointer; box-shadow: 0 4px 15px rgba(0,0,0,0.4) !important;}
|
| 338 |
+
.alt-card:hover { background: linear-gradient(145deg, #064e3b, #065f46) !important; transform: translateY(-8px) !important; box-shadow: 0 12px 30px rgba(16, 185, 129, 0.3) !important; color: white !important; border-color: #34d399 !important;}
|
| 339 |
+
.custom-dropdown { background: rgba(30, 41, 59, 0.8) !important; border: 1px solid #38bdf8 !important; border-radius: 12px !important; }
|
| 340 |
+
|
| 341 |
+
/* Victory Dashboard & Celebration */
|
| 342 |
+
@keyframes pulseGlow { 0% { box-shadow: 0 0 20px rgba(52, 211, 153, 0.4); } 50% { box-shadow: 0 0 50px rgba(52, 211, 153, 0.8); } 100% { box-shadow: 0 0 20px rgba(52, 211, 153, 0.4); } }
|
| 343 |
+
.victory-dashboard { background: linear-gradient(135deg, #0f172a, #1e293b); padding: 40px; border-radius: 24px; border: 2px solid #34d399; animation: pulseGlow 2s infinite; color: white; position: relative; overflow: hidden; }
|
| 344 |
+
.victory-title { text-align: center; color: #34d399; font-size: 3.5em !important; text-shadow: 0 0 20px rgba(52, 211, 153, 0.6); margin-bottom: 5px; }
|
| 345 |
+
.victory-stats-row { display: flex; gap: 20px; margin-top: 30px; }
|
| 346 |
+
.v-stat-box { flex: 1; background: rgba(0,0,0,0.4); padding: 25px; border-radius: 16px; text-align: center; border: 1px solid rgba(52, 211, 153, 0.3); }
|
| 347 |
+
.v-stat-box h4 { color: #94a3b8; font-size: 0.9em; text-transform: uppercase; letter-spacing: 1px; margin: 0 0 10px 0; }
|
| 348 |
+
.v-stat-box h2 { color: #38bdf8; font-size: 2.5em; margin: 0; }
|
| 349 |
+
|
| 350 |
+
/* Confetti Animation */
|
| 351 |
+
.confetti { position: absolute; width: 100%; height: 100%; top: 0; left: 0; pointer-events: none; }
|
| 352 |
+
.pyro > .before, .pyro > .after { position: absolute; width: 5px; height: 5px; border-radius: 50%; box-shadow: 0 0 #fff, 0 0 #fff, 0 0 #fff, 0 0 #fff, 0 0 #fff; animation: 1s bang ease-out infinite backwards, 1s gravity ease-in infinite backwards, 5s position linear infinite backwards; }
|
| 353 |
+
.pyro > .after { animation-delay: 1.25s, 1.25s, 1.25s; animation-duration: 1.25s, 1.25s, 6.25s; }
|
| 354 |
+
@keyframes bang { to { box-shadow: -70px -115.67px #47ff00, -28px -99.67px #00ff2b, 15px -106.67px #00ffdd, -20px -54.67px #ff0073, 5px -144.67px #ff0055, 60px -95.67px #0015ff, 80px -82.67px #ffeb00, 10px -100.67px #ff0022; } }
|
| 355 |
+
@keyframes gravity { to { transform: translateY(200px); opacity: 0; } }
|
| 356 |
+
@keyframes position { 0%, 19.9% { margin-top: 10%; margin-left: 40%; } 20%, 39.9% { margin-top: 40%; margin-left: 30%; } 40%, 59.9% { margin-top: 20%; margin-left: 70%; } 60%, 79.9% { margin-top: 30%; margin-left: 20%; } 80%, 99.9% { margin-top: 30%; margin-left: 80%; } }
|
| 357 |
+
"""
|
| 358 |
+
|
| 359 |
+
with gr.Blocks(css=custom_css, title="EcoQueryQuest") as demo:
|
| 360 |
+
game_state = gr.State({
|
| 361 |
+
"water": 10.0, "energy": 0.0, "co2": 0.0, "points": 0, "queries": 0,
|
| 362 |
+
"history": [], "game_over": False, "current_best_alt": None, "current_shuffled_alts": []
|
| 363 |
+
})
|
| 364 |
+
|
| 365 |
+
gr.Markdown("<h1>🌍 EcoQueryQuest</h1>")
|
| 366 |
+
gr.Markdown("<div class='subtitle'>Select a model, type a query, and watch Drip react. Reach 500 points to win!</div>")
|
| 367 |
+
|
| 368 |
+
stats_html = gr.HTML(get_stats_html(10.0, 0.0, 0.0, 0))
|
| 369 |
+
|
| 370 |
+
with gr.Column(visible=True) as main_game_ui:
|
| 371 |
+
with gr.Row():
|
| 372 |
+
with gr.Column(scale=1, elem_classes=["drip-pod"]):
|
| 373 |
+
drip_html = gr.HTML(get_drip_visuals(10.0))
|
| 374 |
+
drip_audio = gr.Audio(label="Drip's Voice", autoplay=True, interactive=False, elem_classes=["drip-audio"])
|
| 375 |
+
drip_feedback = gr.HTML("<div class='feedback-box'>Waiting for your first query...</div>")
|
| 376 |
+
|
| 377 |
+
with gr.Column(scale=2):
|
| 378 |
+
model_selector = gr.Dropdown(choices=list(MODELS.keys()), value="GPT-5.4", label="Select Target LLM Backend", elem_classes=["custom-dropdown"])
|
| 379 |
+
with gr.Row(elem_classes=["input-row"]):
|
| 380 |
+
user_input = gr.Textbox(show_label=False, placeholder="Type your query here...", elem_classes=["custom-textbox"], scale=4, lines=3)
|
| 381 |
+
submit_btn = gr.Button("Send Query", elem_classes=["custom-btn"], scale=1)
|
| 382 |
+
impact_display = gr.HTML()
|
| 383 |
+
|
| 384 |
+
with gr.Group(visible=False, elem_classes=["alt-group"]) as alternatives_group:
|
| 385 |
+
gr.Markdown("<h3 style='text-align:center; color:#34d399; margin-bottom: 20px;'>🌱 Drip says: 'Quick! Pick a greener tool to earn points!'</h3>")
|
| 386 |
+
with gr.Row():
|
| 387 |
+
alt_btn_1 = gr.Button("", elem_classes=["alt-card"])
|
| 388 |
+
alt_btn_2 = gr.Button("", elem_classes=["alt-card"])
|
| 389 |
+
alt_btn_3 = gr.Button("", elem_classes=["alt-card"])
|
| 390 |
+
alt_feedback = gr.Markdown()
|
| 391 |
+
|
| 392 |
+
with gr.Column(visible=False) as victory_ui:
|
| 393 |
+
victory_display = gr.HTML()
|
| 394 |
+
|
| 395 |
+
submit_btn.click(
|
| 396 |
+
fn=process_query,
|
| 397 |
+
inputs=[user_input, model_selector, game_state],
|
| 398 |
+
outputs=[game_state, stats_html, drip_html, drip_audio, impact_display, drip_feedback, alternatives_group, alt_btn_1, alt_btn_2, alt_btn_3, alt_feedback, user_input, main_game_ui, victory_ui, victory_display]
|
| 399 |
+
)
|
| 400 |
+
|
| 401 |
+
for btn in [alt_btn_1, alt_btn_2, alt_btn_3]:
|
| 402 |
+
btn.click(
|
| 403 |
+
fn=select_alternative,
|
| 404 |
+
inputs=[btn, game_state],
|
| 405 |
+
outputs=[game_state, stats_html, alt_feedback, alternatives_group, main_game_ui, victory_ui, victory_display]
|
| 406 |
+
)
|
| 407 |
+
|
| 408 |
+
demo.launch()
|