Spaces:
Sleeping
Sleeping
vighnesh-shetty-vs commited on
Commit ·
742b3a2
1
Parent(s): d8cc9c0
Add updated files
Browse files- alternatives.py +18 -0
- app.py +46 -5
- game_data.py +34 -0
alternatives.py
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ALTERNATIVES = {
|
| 2 |
+
"simple factual question": [
|
| 3 |
+
"Use a traditional Search Engine (like Google Search)",
|
| 4 |
+
"Check Wikipedia or an offline encyclopedia",
|
| 5 |
+
"Consult a subject-matter expert or ask a friend"
|
| 6 |
+
],
|
| 7 |
+
"mathematical calculation": [
|
| 8 |
+
"Use a dedicated math engine like Wolfram Alpha",
|
| 9 |
+
"Fire up Geogebra or a standard calculator app",
|
| 10 |
+
"Solve it on good old-fashioned paper"
|
| 11 |
+
],
|
| 12 |
+
"creative content generation": [
|
| 13 |
+
"Brainstorm with a notebook and pen",
|
| 14 |
+
"Use offline writing prompts or a dictionary/thesaurus",
|
| 15 |
+
"Listen to a podcast or read a book for inspiration"
|
| 16 |
+
],
|
| 17 |
+
"complex research query": []
|
| 18 |
+
}
|
app.py
CHANGED
|
@@ -1,7 +1,48 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
from game_data import calculate_impact
|
| 3 |
|
| 4 |
+
# Load zero-shot classification pipeline
|
| 5 |
+
# (This runs server-side and uses the free CPU tier effectively)
|
| 6 |
+
print("Loading classification model (facebook/bart-large-mnli)...")
|
| 7 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 8 |
|
| 9 |
+
# Predefined categories mapping to your zero-shot labels
|
| 10 |
+
CATEGORIES = [
|
| 11 |
+
"simple factual question",
|
| 12 |
+
"mathematical calculation",
|
| 13 |
+
"creative content generation",
|
| 14 |
+
"complex research query"
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
def classify_query(user_input):
|
| 18 |
+
"""
|
| 19 |
+
Takes user input, classifies it using zero-shot classification,
|
| 20 |
+
and returns the highest probability category and its confidence score.
|
| 21 |
+
"""
|
| 22 |
+
result = classifier(user_input, CATEGORIES)
|
| 23 |
+
top_category = result['labels'][0]
|
| 24 |
+
confidence_score = result['scores'][0]
|
| 25 |
+
|
| 26 |
+
return top_category, confidence_score
|
| 27 |
+
|
| 28 |
+
# --- Testing Block (You can remove this when building the Gradio UI) ---
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
# Test 1: Simple Fact
|
| 31 |
+
test_query = "What is the capital of France?"
|
| 32 |
+
print(f"\nQuery: '{test_query}'")
|
| 33 |
+
|
| 34 |
+
category, confidence = classify_query(test_query)
|
| 35 |
+
print(f"Classification: {category} (Confidence: {confidence:.2f})")
|
| 36 |
+
|
| 37 |
+
impact = calculate_impact(category, confidence, test_query)
|
| 38 |
+
print(f"Impact: {impact['water']}L water, {impact['energy']}kWh energy")
|
| 39 |
+
|
| 40 |
+
# Test 2: Creative Generation
|
| 41 |
+
test_query_2 = "Write a 500-word sci-fi story about a robot who learns to love painting landscapes."
|
| 42 |
+
print(f"\nQuery: '{test_query_2}'")
|
| 43 |
+
|
| 44 |
+
category_2, confidence_2 = classify_query(test_query_2)
|
| 45 |
+
print(f"Classification: {category_2} (Confidence: {confidence_2:.2f})")
|
| 46 |
+
|
| 47 |
+
impact_2 = calculate_impact(category_2, confidence_2, test_query_2)
|
| 48 |
+
print(f"Impact: {impact_2['water']}L water, {impact_2['energy']}kWh energy")
|
game_data.py
CHANGED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Base costs mapping: {"category": {"water": L, "energy": kWh}}
|
| 2 |
+
BASE_COSTS = {
|
| 3 |
+
"simple factual question": {"water": 0.5, "energy": 0.01},
|
| 4 |
+
"mathematical calculation": {"water": 0.3, "energy": 0.005},
|
| 5 |
+
"creative content generation": {"water": 5.0, "energy": 0.5},
|
| 6 |
+
"complex research query": {"water": 10.0, "energy": 1.0}
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
def get_confidence_multiplier(confidence_score):
|
| 10 |
+
if confidence_score > 0.8:
|
| 11 |
+
return 1.0
|
| 12 |
+
elif 0.5 <= confidence_score <= 0.8:
|
| 13 |
+
return 1.2 # Add 20% uncertainty buffer
|
| 14 |
+
else:
|
| 15 |
+
return 1.5 # Add 50% buffer
|
| 16 |
+
|
| 17 |
+
def get_length_factor(query_text):
|
| 18 |
+
word_count = len(query_text.split())
|
| 19 |
+
if word_count < 20:
|
| 20 |
+
return 1.0
|
| 21 |
+
elif 20 <= word_count <= 50:
|
| 22 |
+
return 1.3
|
| 23 |
+
else:
|
| 24 |
+
return 1.5
|
| 25 |
+
|
| 26 |
+
def calculate_impact(category, confidence_score, query_text):
|
| 27 |
+
base_cost = BASE_COSTS.get(category, {"water": 0, "energy": 0})
|
| 28 |
+
conf_mult = get_confidence_multiplier(confidence_score)
|
| 29 |
+
len_factor = get_length_factor(query_text)
|
| 30 |
+
|
| 31 |
+
water_impact = base_cost["water"] * conf_mult * len_factor
|
| 32 |
+
energy_impact = base_cost["energy"] * conf_mult * len_factor
|
| 33 |
+
|
| 34 |
+
return {"water": round(water_impact, 2), "energy": round(energy_impact, 3)}
|