vighnesh-shetty-vs commited on
Commit
b3ba296
Β·
1 Parent(s): 0461653

Add updated files

Browse files
Files changed (1) hide show
  1. app.py +94 -72
app.py CHANGED
@@ -8,7 +8,6 @@ from game_data import calculate_impact, MODELS
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
 
@@ -153,69 +152,90 @@ def format_alt_button(text, impact):
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>
216
- </div>
217
- </div>
218
- """
219
 
220
  def process_query(user_input, selected_model, state):
221
  if state["game_over"] or not user_input.strip():
@@ -296,10 +316,11 @@ def select_alternative(choice_text, state):
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
 
@@ -340,20 +361,19 @@ h1 { text-align: center; color: #38bdf8; text-shadow: 0 0 15px rgba(56, 189, 248
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:
@@ -405,4 +425,6 @@ with gr.Blocks(css=custom_css, title="EcoQueryQuest") as demo:
405
  outputs=[game_state, stats_html, alt_feedback, alternatives_group, main_game_ui, victory_ui, victory_display]
406
  )
407
 
408
- demo.launch()
 
 
 
8
  from dialogues import get_dialogue
9
  from alternatives import ALTERNATIVES
10
 
 
11
  print("Loading classification model...", flush=True)
12
  classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
13
 
 
152
  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"
153
 
154
  def generate_victory_dashboard(state):
155
+ try:
156
+ total_w_saved = 0.0
157
+ total_e_saved = 0.0
158
+ total_c_saved = 0.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
+ rows = ""
161
+ for item in state.get("history", []):
162
+ alt_name = item.get('alt_name', 'None (Justified Use)')
163
+ if alt_name == "None (Justified Use)":
164
+ saved_w, saved_e, saved_c = 0.0, 0.0, 0.0
165
+ alt_display = "<span style='color: #94a3b8; font-style: italic;'>Justified Use (No Alt needed)</span>"
166
+ else:
167
+ saved_w = item.get('water_ml', 0.0)
168
+ saved_e = max(0.0, item.get('energy_wh', 0.0) - 0.001)
169
+ saved_c = max(0.0, item.get('co2_g', 0.0) - 0.01)
170
+
171
+ total_w_saved += saved_w
172
+ total_e_saved += saved_e
173
+ total_c_saved += saved_c
174
+ alt_display = f"<span style='color: #34d399; font-weight: bold;'>{alt_name}</span>"
175
+
176
+ rows += f"""
177
+ <tr style="border-bottom: 1px solid rgba(255,255,255,0.1); background: rgba(0,0,0,0.2);">
178
+ <td style="padding: 15px; font-style: italic;">"{item.get('query', '')}"</td>
179
+ <td style="padding: 15px; color: #fca5a5; line-height: 1.5;">
180
+ πŸ’§ {item.get('water_ml', 0):.1f} mL<br>
181
+ ⚑ {item.get('energy_wh', 0):.2f} Wh<br>
182
+ ☁️ {item.get('co2_g', 0):.2f} g
183
+ </td>
184
+ <td style="padding: 15px;">{alt_display}</td>
185
+ <td style="padding: 15px; color: #60a5fa; font-weight: bold; line-height: 1.5;">
186
+ πŸ’§ {saved_w:.1f} mL<br>
187
+ ⚑ {saved_e:.2f} Wh<br>
188
+ ☁️ {saved_c:.2f} g
189
+ </td>
190
+ </tr>
191
+ """
192
+
193
+ # Dynamically generate 50 confetti divs
194
+ confetti_elements = "".join([f'<div class="confetti-piece" style="left: {random.randint(0, 100)}%; background-color: hsl({random.randint(0, 360)}, 100%, 60%); animation-duration: {random.uniform(2.5, 5)}s; animation-delay: {random.uniform(0, 2)}s;"></div>' for _ in range(60)])
195
 
196
+ return f"""
197
+ <div class="victory-wrapper">
198
+ {confetti_elements}
199
+ <div class="victory-dashboard">
200
+ <h1 class="victory-title">πŸŽ‰ PLANET SAVED! πŸŽ‰</h1>
201
+ <p style="text-align: center; font-size: 1.3em; color: #a7f3d0; margin-bottom: 30px;">
202
+ Congratulations! You reached {state.get('points', 0)} points and prevented Drip from evaporating!
203
+ </p>
204
+
205
+ <div class="victory-stats-row">
206
+ <div class="v-stat-box" style="border-top: 4px solid #34d399;">
207
+ <h4 style="color: #34d399;">Planetary Savings</h4>
208
+ <p style="color: #34d399; font-size: 1.3em; font-weight: bold; margin: 8px 0;">πŸ’§ {total_w_saved:.1f} mL Saved</p>
209
+ <p style="color: #facc15; font-size: 1.3em; font-weight: bold; margin: 8px 0;">⚑ {total_e_saved:.2f} Wh Saved</p>
210
+ <p style="color: #a8a29e; font-size: 1.3em; font-weight: bold; margin: 8px 0;">☁️ {total_c_saved:.2f} g Saved</p>
211
+ </div>
212
+ <div class="v-stat-box" style="border-top: 4px solid #fca5a5;">
213
+ <h4 style="color: #fca5a5;">Total Wasted</h4>
214
+ <p style="color: #fca5a5; font-size: 1.1em; margin: 8px 0;">πŸ’§ {10.0 - state.get('water', 10.0):.2f} L</p>
215
+ <p style="color: #fca5a5; font-size: 1.1em; margin: 8px 0;">⚑ {state.get('energy', 0):.4f} kWh</p>
216
+ <p style="color: #fca5a5; font-size: 1.1em; margin: 8px 0;">☁️ {state.get('co2', 0):.2f} g</p>
217
+ </div>
218
+ </div>
219
+
220
+ <h3 style="margin-top: 40px; color: #38bdf8; font-size: 1.8em; text-align: center;">πŸ“Š AI Usage Audit Log</h3>
221
+ <p style="opacity: 0.9; margin-bottom: 20px; text-align: center;">A detailed breakdown of your session's environmental impact and the greener choices you made.</p>
222
+
223
+ <div style="max-height: 500px; overflow-y: auto; background: rgba(15, 23, 42, 0.9); border-radius: 12px; border: 1px solid rgba(255,255,255,0.1); padding: 10px;">
224
+ <table style="width: 100%; text-align: left; border-collapse: collapse; font-size: 1.05em;">
225
+ <tr style="background: rgba(56, 189, 248, 0.1); border-bottom: 2px solid #38bdf8;">
226
+ <th style="padding: 15px; color: #e2e8f0;">User Query</th>
227
+ <th style="padding: 15px; color: #fca5a5;">LLM Cost (Wasted)</th>
228
+ <th style="padding: 15px; color: #34d399;">Best Alternative</th>
229
+ <th style="padding: 15px; color: #60a5fa;">Resources Saved</th>
230
+ </tr>
231
+ {rows}
232
+ </table>
233
+ </div>
234
  </div>
235
  </div>
236
+ """
237
+ except Exception as e:
238
+ return f"<div style='color:red;'>Error generating dashboard: {str(e)}</div>"
 
 
 
 
 
 
 
 
 
 
 
 
239
 
240
  def process_query(user_input, selected_model, state):
241
  if state["game_over"] or not user_input.strip():
 
316
 
317
  return state, stats_ui, gr.update(value=msg), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update()
318
 
319
+
320
  custom_css = """
321
  @import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;500;700&display=swap');
322
  body, .gradio-container { background-color: #0B1120 !important; font-family: 'Outfit', sans-serif !important; color: #e2e8f0 !important; }
323
+ .gradio-container { max-width: 1000px !important; margin: auto; padding-top: 20px; }
324
  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;}
325
  .subtitle { text-align: center; color: #94a3b8; margin-bottom: 30px; font-weight: 300; }
326
 
 
361
 
362
  /* Victory Dashboard & Celebration */
363
  @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); } }
364
+ .victory-wrapper { position: relative; width: 100%; min-height: 600px; overflow: hidden; border-radius: 24px; padding-top: 10px; }
365
+ .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; z-index: 5; margin-top: 20px;}
366
  .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; }
367
  .victory-stats-row { display: flex; gap: 20px; margin-top: 30px; }
368
  .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); }
369
  .v-stat-box h4 { color: #94a3b8; font-size: 0.9em; text-transform: uppercase; letter-spacing: 1px; margin: 0 0 10px 0; }
370
+
371
+ /* Pure CSS Confetti Physics */
372
+ .confetti-piece { position: absolute; width: 10px; height: 15px; top: -20px; opacity: 0; animation: fall linear forwards infinite; z-index: 10; border-radius: 2px;}
373
+ @keyframes fall {
374
+ 0% { transform: translateY(-20px) rotate(0deg); opacity: 1; }
375
+ 100% { transform: translateY(1000px) rotate(720deg); opacity: 1; }
376
+ }
 
 
377
  """
378
 
379
  with gr.Blocks(css=custom_css, title="EcoQueryQuest") as demo:
 
425
  outputs=[game_state, stats_html, alt_feedback, alternatives_group, main_game_ui, victory_ui, victory_display]
426
  )
427
 
428
+ # Launch with CSS parameter mapping directly to demo launch block
429
+ if __name__ == "__main__":
430
+ demo.launch(css=custom_css)