Spaces:
Sleeping
Sleeping
Hybrid GVFD Assistant with OAuth and AI integration
Browse files- Combines GVFD-specific functionality with HF template OAuth features
- Smart search and calculation handling for value factor queries
- AI responses with user's own HF token (user pays their own costs)
- Fallback to structured responses without sign-in
- Optimized for "value factor for X in Y country" queries
- Enhanced contextual AI responses when logged in
๐ค Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
- .gitattributes +35 -0
- README.md +27 -33
- app.py +140 -272
- requirements.txt +1 -4
.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,46 +1,40 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
An AI-enhanced interactive chatbot that allows users to explore and calculate with the Global Value Factor Database - a comprehensive dataset that converts environmental and social impacts into monetary values (USD).
|
| 4 |
|
| 5 |
## โจ Features
|
| 6 |
|
| 7 |
- ๐ค **AI-Enhanced Responses**: Local AI model provides intelligent, conversational responses
|
| 8 |
-
- ๐ **Search Value
|
| 9 |
-
- ๐งฎ **Impact Calculations**: Calculate monetary impacts using value factors
|
| 10 |
-
- ๐ **Country Analysis**: Explore
|
| 11 |
-
- ๐ **Category Filtering**: Browse factors by environmental categories
|
| 12 |
-
- ๐ฐ **Completely FREE**: Runs locally
|
| 13 |
|
| 14 |
## Dataset
|
| 15 |
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
-
- 229 countries (205 with ISO codes)
|
| 20 |
-
- Multiple environmental categories
|
| 21 |
-
- Standardized monetary conversion factors
|
| 22 |
-
- Precise decimal values for accurate calculations
|
| 23 |
|
| 24 |
-
##
|
| 25 |
|
| 26 |
-
- "
|
| 27 |
-
- "
|
| 28 |
-
- "
|
| 29 |
-
- "
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
- **Frontend**: Gradio for interactive web interface
|
| 34 |
-
- **Data Processing**: Pandas for data manipulation
|
| 35 |
-
- **Dataset**: Hugging Face Datasets library
|
| 36 |
-
- **Backend**: Python with efficient search and calculation algorithms
|
| 37 |
-
|
| 38 |
-
## Categories Covered
|
| 39 |
-
|
| 40 |
-
- Air pollution
|
| 41 |
-
- Land use and conservation
|
| 42 |
-
- Waste generation
|
| 43 |
-
- Water consumption
|
| 44 |
-
- Water pollution
|
| 45 |
-
|
| 46 |
-
Perfect for researchers, sustainability professionals, ESG analysts, and anyone working with environmental impact assessment and monetization.
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: GVFD Explorer
|
| 3 |
+
emoji: ๐
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.42.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
short_description: AI-powered chat interface for exploring the Global Value Factor Database
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# Global Value Factor Database Explorer
|
| 14 |
|
| 15 |
An AI-enhanced interactive chatbot that allows users to explore and calculate with the Global Value Factor Database - a comprehensive dataset that converts environmental and social impacts into monetary values (USD).
|
| 16 |
|
| 17 |
## โจ Features
|
| 18 |
|
| 19 |
- ๐ค **AI-Enhanced Responses**: Local AI model provides intelligent, conversational responses
|
| 20 |
+
- ๐ **Smart Search**: "Value factor for CO2 emissions in Germany"
|
| 21 |
+
- ๐งฎ **Impact Calculations**: Calculate monetary impacts using value factors
|
| 22 |
+
- ๐ **Country Analysis**: Explore factors specific to different countries
|
| 23 |
+
- ๐ **Category Filtering**: Browse factors by environmental categories
|
| 24 |
+
- ๐ฐ **Completely FREE**: Runs locally with no API costs
|
| 25 |
|
| 26 |
## Dataset
|
| 27 |
|
| 28 |
+
Uses the [Global Value Factor Database Refactor V2](https://huggingface.co/datasets/danielrosehill/Global-Value-Factor-Database-Refactor-V2) from the International Foundation for Valuing Impacts (IFVI).
|
| 29 |
|
| 30 |
+
**Coverage**: 229 countries, multiple environmental categories, standardized monetary conversion factors.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
## Example Queries
|
| 33 |
|
| 34 |
+
- "Value factor for CO2 emissions in Germany"
|
| 35 |
+
- "Find air pollution factors for USA"
|
| 36 |
+
- "What's the water consumption factor in Japan?"
|
| 37 |
+
- "Calculate impact for 50 tons with factor 125.75"
|
| 38 |
+
- "Alternatives to methane factors if not available"
|
| 39 |
|
| 40 |
+
Perfect for researchers, sustainability professionals, ESG analysts, and environmental impact assessment.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -5,28 +5,24 @@ from datasets import load_dataset
|
|
| 5 |
import json
|
| 6 |
from typing import Dict, List, Any, Optional
|
| 7 |
import re
|
| 8 |
-
from
|
| 9 |
-
|
| 10 |
|
| 11 |
class GVFDChatbot:
|
| 12 |
def __init__(self):
|
| 13 |
self.dataset = None
|
| 14 |
self.df = None
|
| 15 |
-
self.ai_model = None
|
| 16 |
self.load_data()
|
| 17 |
-
self.load_ai_model()
|
| 18 |
|
| 19 |
def load_data(self):
|
| 20 |
"""Load the Global Value Factor Database from HuggingFace"""
|
| 21 |
try:
|
| 22 |
-
# Try to load the dataset, handling potential CSV parsing issues
|
| 23 |
self.dataset = load_dataset(
|
| 24 |
"danielrosehill/Global-Value-Factor-Database-Refactor-V2",
|
| 25 |
-
split='validation'
|
| 26 |
)
|
| 27 |
self.df = pd.DataFrame(self.dataset)
|
| 28 |
print(f"Dataset loaded successfully with {len(self.df)} records")
|
| 29 |
-
print(f"Columns available: {list(self.df.columns)}")
|
| 30 |
except Exception as e:
|
| 31 |
print(f"Error loading dataset: {e}")
|
| 32 |
# Create a sample dataset for testing
|
|
@@ -39,23 +35,6 @@ class GVFDChatbot:
|
|
| 39 |
})
|
| 40 |
print("Using sample dataset for testing")
|
| 41 |
|
| 42 |
-
def load_ai_model(self):
|
| 43 |
-
"""Load local AI model for enhanced responses"""
|
| 44 |
-
try:
|
| 45 |
-
print("Loading local AI model...")
|
| 46 |
-
# Use a small, efficient model that runs locally
|
| 47 |
-
self.ai_model = pipeline(
|
| 48 |
-
"text-generation",
|
| 49 |
-
model="microsoft/DialoGPT-small",
|
| 50 |
-
tokenizer="microsoft/DialoGPT-small",
|
| 51 |
-
device_map="auto" if torch.cuda.is_available() else "cpu"
|
| 52 |
-
)
|
| 53 |
-
print("โ
Local AI model loaded successfully - completely FREE!")
|
| 54 |
-
except Exception as e:
|
| 55 |
-
print(f"โ ๏ธ AI model loading failed: {e}")
|
| 56 |
-
print("Falling back to rule-based responses")
|
| 57 |
-
self.ai_model = None
|
| 58 |
-
|
| 59 |
def search_value_factors(self, query: str, category: str = "all") -> List[Dict]:
|
| 60 |
"""Search for value factors based on query and category"""
|
| 61 |
if self.df is None or self.df.empty:
|
|
@@ -64,12 +43,10 @@ class GVFDChatbot:
|
|
| 64 |
results = []
|
| 65 |
query_lower = query.lower()
|
| 66 |
|
| 67 |
-
# Filter by category if specified
|
| 68 |
df_filtered = self.df
|
| 69 |
if category != "all" and 'category' in self.df.columns:
|
| 70 |
df_filtered = self.df[self.df['category'].str.lower().str.contains(category.lower(), na=False)]
|
| 71 |
|
| 72 |
-
# Search across text columns
|
| 73 |
text_columns = [col for col in df_filtered.columns if df_filtered[col].dtype == 'object']
|
| 74 |
|
| 75 |
for _, row in df_filtered.iterrows():
|
|
@@ -83,9 +60,8 @@ class GVFDChatbot:
|
|
| 83 |
result['match_score'] = match_score
|
| 84 |
results.append(result)
|
| 85 |
|
| 86 |
-
# Sort by match score
|
| 87 |
results.sort(key=lambda x: x['match_score'], reverse=True)
|
| 88 |
-
return results[:10]
|
| 89 |
|
| 90 |
def calculate_impact_value(self, impact_quantity: float, value_factor: float, country: str = "") -> Dict:
|
| 91 |
"""Calculate monetary impact value"""
|
|
@@ -101,259 +77,151 @@ class GVFDChatbot:
|
|
| 101 |
"country": country,
|
| 102 |
"calculation": f"{impact_quantity} ร {value_factor} = ${monetary_impact:,.2f}"
|
| 103 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
country_data.append(row.to_dict())
|
| 119 |
-
break
|
| 120 |
-
|
| 121 |
-
return country_data
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
if len(search_results) == 0:
|
| 144 |
-
enhanced_context += "\n\nNo exact matches found. Suggest alternatives or related factors."
|
| 145 |
-
else:
|
| 146 |
-
enhanced_context += f"\n\nFound {len(search_results)} matches. Help user understand the results and suggest related options."
|
| 147 |
-
|
| 148 |
-
if enhanced_context:
|
| 149 |
-
prompt = f"{system_context}\n\nSearch results: {enhanced_context}\n\nUser query: {message}\n\nProvide a helpful response that contextualizes the findings and offers guidance:\nAssistant:"
|
| 150 |
-
else:
|
| 151 |
-
prompt = f"{system_context}\n\nUser query: {message}\n\nProvide helpful guidance about value factors:\nAssistant:"
|
| 152 |
-
|
| 153 |
-
# Generate response
|
| 154 |
-
response = self.ai_model(
|
| 155 |
-
prompt,
|
| 156 |
-
max_length=len(prompt) + 200, # More space for contextual responses
|
| 157 |
-
temperature=0.6, # Slightly lower for more focused responses
|
| 158 |
-
do_sample=True,
|
| 159 |
-
pad_token_id=self.ai_model.tokenizer.eos_token_id
|
| 160 |
-
)
|
| 161 |
-
|
| 162 |
-
# Extract just the assistant's response
|
| 163 |
-
full_text = response[0]['generated_text']
|
| 164 |
-
assistant_response = full_text.split("Assistant:")[-1].strip()
|
| 165 |
-
|
| 166 |
-
# Clean up common AI artifacts
|
| 167 |
-
assistant_response = assistant_response.replace("User:", "").strip()
|
| 168 |
-
|
| 169 |
-
return f"๐ค **AI Assistant:**\n\n{assistant_response}"
|
| 170 |
-
|
| 171 |
-
except Exception as e:
|
| 172 |
-
print(f"AI generation error: {e}")
|
| 173 |
-
return None # Fall back to rule-based
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
results = self.search_value_factors(search_terms)
|
| 209 |
-
|
| 210 |
-
# Enhanced context for AI
|
| 211 |
-
if results:
|
| 212 |
-
context = f"Query: '{search_terms}' | Found {len(results)} matches"
|
| 213 |
-
for i, result in enumerate(results[:3]):
|
| 214 |
-
context += f" | Match {i+1}: {result}"
|
| 215 |
-
else:
|
| 216 |
-
context = f"Query: '{search_terms}' | No exact matches found"
|
| 217 |
-
|
| 218 |
-
# AI-enhanced response with results
|
| 219 |
-
ai_response = self.generate_ai_response(message, context, results)
|
| 220 |
-
if ai_response:
|
| 221 |
-
# Add structured data after AI response
|
| 222 |
-
if results:
|
| 223 |
-
data_summary = f"\n\n๐ **Quick Reference:**\n"
|
| 224 |
-
for i, result in enumerate(results[:3], 1):
|
| 225 |
-
key_fields = ['category', 'impact', 'value_factor', 'country', 'units']
|
| 226 |
-
shown = []
|
| 227 |
-
for field in key_fields:
|
| 228 |
-
if field in result and pd.notna(result[field]):
|
| 229 |
-
shown.append(f"{result[field]}")
|
| 230 |
-
data_summary += f"**{i}.** " + " | ".join(shown[:3]) + "\n"
|
| 231 |
-
return ai_response + data_summary
|
| 232 |
-
return ai_response
|
| 233 |
-
|
| 234 |
-
# Fallback to structured response
|
| 235 |
-
if results:
|
| 236 |
-
response = f"๐ **Found {len(results)} value factors:**\n\n"
|
| 237 |
-
for i, result in enumerate(results[:5], 1):
|
| 238 |
-
response += f"**{i}.** "
|
| 239 |
-
key_fields = ['category', 'impact', 'value_factor', 'country', 'units']
|
| 240 |
-
shown_fields = []
|
| 241 |
-
|
| 242 |
-
for field in key_fields:
|
| 243 |
-
if field in result and pd.notna(result[field]):
|
| 244 |
-
shown_fields.append(f"{field.replace('_', ' ').title()}: {result[field]}")
|
| 245 |
-
|
| 246 |
-
response += " | ".join(shown_fields[:3]) + "\n\n"
|
| 247 |
-
return response
|
| 248 |
-
else:
|
| 249 |
-
return "โ No value factors found matching your search. Try different keywords or check spelling."
|
| 250 |
-
|
| 251 |
-
# Country-specific queries (including "in [country]" patterns)
|
| 252 |
-
elif "country" in message_lower or " in " in message_lower:
|
| 253 |
-
# Extract country name more intelligently
|
| 254 |
-
words = message.split()
|
| 255 |
-
country_candidates = []
|
| 256 |
-
|
| 257 |
-
# Look for "in [country]" patterns
|
| 258 |
-
if " in " in message_lower:
|
| 259 |
-
in_index = message_lower.split().index("in")
|
| 260 |
-
if in_index + 1 < len(words):
|
| 261 |
-
country_candidates.append(words[in_index + 1])
|
| 262 |
-
|
| 263 |
-
# Fallback to any capitalized words or country-like terms
|
| 264 |
-
for word in words:
|
| 265 |
-
if len(word) > 2 and (word[0].isupper() or word.lower() in ['usa', 'uk', 'us']):
|
| 266 |
-
country_candidates.append(word)
|
| 267 |
-
|
| 268 |
-
if country_candidates:
|
| 269 |
-
country = country_candidates[-1] # Take the most likely candidate
|
| 270 |
-
results = self.get_country_factors(country)
|
| 271 |
-
|
| 272 |
-
# Enhanced context for AI
|
| 273 |
-
context = f"Country query for '{country}' | Found {len(results)} factors"
|
| 274 |
-
if results:
|
| 275 |
-
context += f" | Sample data: {results[:2]}"
|
| 276 |
-
else:
|
| 277 |
-
context += " | No direct matches - suggest alternatives"
|
| 278 |
-
|
| 279 |
-
# AI-enhanced response
|
| 280 |
-
ai_response = self.generate_ai_response(message, context, results)
|
| 281 |
-
if ai_response:
|
| 282 |
-
return ai_response
|
| 283 |
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
# General queries - try AI first
|
| 291 |
-
ai_response = self.generate_ai_response(message)
|
| 292 |
-
if ai_response:
|
| 293 |
-
return ai_response
|
| 294 |
-
|
| 295 |
-
# Final fallback - help message
|
| 296 |
-
return """๐ **Welcome to the Global Value Factor Database Assistant!**
|
| 297 |
-
|
| 298 |
-
๐ค **AI-Enhanced Responses** - Now with local AI for smarter conversations!
|
| 299 |
-
|
| 300 |
-
I can help you with:
|
| 301 |
-
|
| 302 |
-
๐ **Search value factors:** "Find air pollution factors" or "Search water consumption"
|
| 303 |
-
|
| 304 |
-
๐งฎ **Calculate impacts:** "Calculate impact for 100 units with factor 185.50"
|
| 305 |
-
|
| 306 |
-
๐ **Country data:** "Show factors for Germany" or "Country USA"
|
| 307 |
-
|
| 308 |
-
๐ **Categories available:**
|
| 309 |
-
- Air pollution
|
| 310 |
-
- Land use and conservation
|
| 311 |
-
- Waste generation
|
| 312 |
-
- Water consumption
|
| 313 |
-
- Water pollution
|
| 314 |
-
|
| 315 |
-
๐ก **Example queries:**
|
| 316 |
-
- "Value factor for CO2 emissions in Germany"
|
| 317 |
-
- "Find air pollution factors for USA"
|
| 318 |
-
- "What's the water consumption factor in Japan?"
|
| 319 |
-
- "Calculate impact for 50 tons with factor 125.75"
|
| 320 |
-
- "Alternatives to methane factors if not available"
|
| 321 |
|
| 322 |
-
|
|
|
|
|
|
|
| 323 |
|
| 324 |
-
|
| 325 |
|
| 326 |
-
# Initialize the chatbot
|
| 327 |
-
chatbot = GVFDChatbot()
|
| 328 |
|
| 329 |
-
# Create
|
| 330 |
-
|
| 331 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
**Dataset:** [Global Value Factor Database Refactor V2](https://huggingface.co/datasets/danielrosehill/Global-Value-Factor-Database-Refactor-V2)
|
| 342 |
-
**Source:** International Foundation for Valuing Impacts (IFVI)
|
| 343 |
-
"""
|
| 344 |
-
)
|
| 345 |
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
|
|
|
|
|
|
| 357 |
|
| 358 |
if __name__ == "__main__":
|
| 359 |
-
|
|
|
|
| 5 |
import json
|
| 6 |
from typing import Dict, List, Any, Optional
|
| 7 |
import re
|
| 8 |
+
from huggingface_hub import InferenceClient
|
| 9 |
+
|
| 10 |
|
| 11 |
class GVFDChatbot:
|
| 12 |
def __init__(self):
|
| 13 |
self.dataset = None
|
| 14 |
self.df = None
|
|
|
|
| 15 |
self.load_data()
|
|
|
|
| 16 |
|
| 17 |
def load_data(self):
|
| 18 |
"""Load the Global Value Factor Database from HuggingFace"""
|
| 19 |
try:
|
|
|
|
| 20 |
self.dataset = load_dataset(
|
| 21 |
"danielrosehill/Global-Value-Factor-Database-Refactor-V2",
|
| 22 |
+
split='validation'
|
| 23 |
)
|
| 24 |
self.df = pd.DataFrame(self.dataset)
|
| 25 |
print(f"Dataset loaded successfully with {len(self.df)} records")
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
print(f"Error loading dataset: {e}")
|
| 28 |
# Create a sample dataset for testing
|
|
|
|
| 35 |
})
|
| 36 |
print("Using sample dataset for testing")
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
def search_value_factors(self, query: str, category: str = "all") -> List[Dict]:
|
| 39 |
"""Search for value factors based on query and category"""
|
| 40 |
if self.df is None or self.df.empty:
|
|
|
|
| 43 |
results = []
|
| 44 |
query_lower = query.lower()
|
| 45 |
|
|
|
|
| 46 |
df_filtered = self.df
|
| 47 |
if category != "all" and 'category' in self.df.columns:
|
| 48 |
df_filtered = self.df[self.df['category'].str.lower().str.contains(category.lower(), na=False)]
|
| 49 |
|
|
|
|
| 50 |
text_columns = [col for col in df_filtered.columns if df_filtered[col].dtype == 'object']
|
| 51 |
|
| 52 |
for _, row in df_filtered.iterrows():
|
|
|
|
| 60 |
result['match_score'] = match_score
|
| 61 |
results.append(result)
|
| 62 |
|
|
|
|
| 63 |
results.sort(key=lambda x: x['match_score'], reverse=True)
|
| 64 |
+
return results[:10]
|
| 65 |
|
| 66 |
def calculate_impact_value(self, impact_quantity: float, value_factor: float, country: str = "") -> Dict:
|
| 67 |
"""Calculate monetary impact value"""
|
|
|
|
| 77 |
"country": country,
|
| 78 |
"calculation": f"{impact_quantity} ร {value_factor} = ${monetary_impact:,.2f}"
|
| 79 |
}
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# Initialize the chatbot
|
| 83 |
+
gvfd_bot = GVFDChatbot()
|
| 84 |
+
|
| 85 |
+
def respond(
|
| 86 |
+
message,
|
| 87 |
+
history: list[dict[str, str]],
|
| 88 |
+
system_message,
|
| 89 |
+
max_tokens,
|
| 90 |
+
temperature,
|
| 91 |
+
top_p,
|
| 92 |
+
hf_token: gr.OAuthToken,
|
| 93 |
+
):
|
| 94 |
+
"""Enhanced GVFD response with AI integration"""
|
| 95 |
+
# First, try to handle with GVFD-specific logic
|
| 96 |
+
message_lower = message.lower()
|
| 97 |
|
| 98 |
+
# Handle calculations
|
| 99 |
+
if "calculate" in message_lower:
|
| 100 |
+
numbers = re.findall(r'\d+(?:\.\d+)?', message)
|
| 101 |
+
if len(numbers) >= 2:
|
| 102 |
+
try:
|
| 103 |
+
quantity = float(numbers[0])
|
| 104 |
+
factor = float(numbers[1])
|
| 105 |
+
result = gvfd_bot.calculate_impact_value(quantity, factor)
|
| 106 |
+
if "error" not in result:
|
| 107 |
+
yield f"๐ฐ **Impact Calculation**\n\n{result['calculation']}\n\n**Monetary Impact:** ${result['monetary_impact_usd']:,}"
|
| 108 |
+
return
|
| 109 |
+
except:
|
| 110 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
# Handle searches
|
| 113 |
+
elif any(keyword in message_lower for keyword in ["search", "find", "factor", "value factor for"]):
|
| 114 |
+
search_terms = message_lower
|
| 115 |
+
for word in ["search", "find", "factor", "value factor for"]:
|
| 116 |
+
search_terms = search_terms.replace(word, "")
|
| 117 |
+
search_terms = search_terms.strip()
|
| 118 |
+
|
| 119 |
+
results = gvfd_bot.search_value_factors(search_terms)
|
| 120 |
+
|
| 121 |
+
if results:
|
| 122 |
+
response = f"๐ **Found {len(results)} value factors:**\n\n"
|
| 123 |
+
for i, result in enumerate(results[:3], 1):
|
| 124 |
+
key_fields = ['category', 'impact', 'value_factor', 'country', 'units']
|
| 125 |
+
shown = []
|
| 126 |
+
for field in key_fields:
|
| 127 |
+
if field in result and pd.notna(result[field]):
|
| 128 |
+
shown.append(f"{result[field]}")
|
| 129 |
+
response += f"**{i}.** " + " | ".join(shown[:3]) + "\n\n"
|
| 130 |
+
yield response
|
| 131 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
# For general queries, use AI with GVFD context
|
| 134 |
+
if hf_token and hf_token.token:
|
| 135 |
+
try:
|
| 136 |
+
client = InferenceClient(token=hf_token.token, model="meta-llama/Llama-2-7b-chat-hf")
|
| 137 |
+
|
| 138 |
+
# Enhanced system message for GVFD
|
| 139 |
+
enhanced_system = f"""{system_message}
|
| 140 |
+
|
| 141 |
+
You are specifically helping with the Global Value Factor Database (GVFD).
|
| 142 |
+
This database converts environmental impacts to USD values. Key categories include:
|
| 143 |
+
- Air pollution, Water consumption, Waste generation, Land use
|
| 144 |
+
- Covers 229 countries with standardized monetary conversion factors
|
| 145 |
+
|
| 146 |
+
When users ask about value factors, provide helpful guidance and suggest alternatives if exact matches aren't found."""
|
| 147 |
+
|
| 148 |
+
messages = [{"role": "system", "content": enhanced_system}]
|
| 149 |
+
messages.extend(history)
|
| 150 |
+
messages.append({"role": "user", "content": message})
|
| 151 |
+
|
| 152 |
+
response = ""
|
| 153 |
+
for msg in client.chat_completion(
|
| 154 |
+
messages,
|
| 155 |
+
max_tokens=max_tokens,
|
| 156 |
+
stream=True,
|
| 157 |
+
temperature=temperature,
|
| 158 |
+
top_p=top_p,
|
| 159 |
+
):
|
| 160 |
+
choices = msg.choices
|
| 161 |
+
token = ""
|
| 162 |
+
if len(choices) and choices[0].delta.content:
|
| 163 |
+
token = choices[0].delta.content
|
| 164 |
+
response += token
|
| 165 |
+
yield response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
+
except Exception as e:
|
| 168 |
+
yield f"โ ๏ธ AI Error: {str(e)}"
|
| 169 |
+
else:
|
| 170 |
+
# Fallback response
|
| 171 |
+
yield """๐ **Welcome to the Global Value Factor Database Explorer!**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
+
๐ **Search**: "Find air pollution factors for USA"
|
| 174 |
+
๐งฎ **Calculate**: "Calculate impact for 100 tons with factor 185.50"
|
| 175 |
+
๐ **Explore**: "Value factor for CO2 emissions in Germany"
|
| 176 |
|
| 177 |
+
๐ก For enhanced AI responses, please sign in with your Hugging Face account."""
|
| 178 |
|
|
|
|
|
|
|
| 179 |
|
| 180 |
+
# Create the interface with OAuth
|
| 181 |
+
chatbot = gr.ChatInterface(
|
| 182 |
+
respond,
|
| 183 |
+
type="messages",
|
| 184 |
+
title="๐ GVFD Explorer",
|
| 185 |
+
description="AI-powered exploration of the Global Value Factor Database. Search for environmental impact value factors, perform calculations, and get intelligent guidance.",
|
| 186 |
+
examples=[
|
| 187 |
+
"Value factor for CO2 emissions in Germany",
|
| 188 |
+
"Find air pollution factors for USA",
|
| 189 |
+
"Calculate impact for 100 tons with factor 185.50",
|
| 190 |
+
"What's the water consumption factor in Japan?"
|
| 191 |
+
],
|
| 192 |
+
additional_inputs=[
|
| 193 |
+
gr.Textbox(
|
| 194 |
+
value="You are an expert assistant for the Global Value Factor Database, helping users find environmental impact value factors and perform calculations.",
|
| 195 |
+
label="System message"
|
| 196 |
+
),
|
| 197 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 198 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 199 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
| 200 |
+
],
|
| 201 |
+
)
|
| 202 |
|
| 203 |
+
with gr.Blocks(title="GVFD Explorer") as demo:
|
| 204 |
+
gr.Markdown("""
|
| 205 |
+
# ๐ Global Value Factor Database Explorer
|
| 206 |
+
|
| 207 |
+
**Dataset**: [Global Value Factor Database Refactor V2](https://huggingface.co/datasets/danielrosehill/Global-Value-Factor-Database-Refactor-V2)
|
| 208 |
+
**Source**: International Foundation for Valuing Impacts (IFVI)
|
| 209 |
+
**Coverage**: 229 countries, environmental impact monetization
|
| 210 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
+
with gr.Row():
|
| 213 |
+
with gr.Column(scale=1):
|
| 214 |
+
gr.LoginButton()
|
| 215 |
+
gr.Markdown("""
|
| 216 |
+
**Sign in for enhanced AI responses!**
|
| 217 |
+
|
| 218 |
+
โข Advanced contextual assistance
|
| 219 |
+
โข Smart alternatives when data isn't found
|
| 220 |
+
โข Detailed explanations of value factors
|
| 221 |
+
""")
|
| 222 |
+
|
| 223 |
+
with gr.Column(scale=4):
|
| 224 |
+
chatbot.render()
|
| 225 |
|
| 226 |
if __name__ == "__main__":
|
| 227 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -2,7 +2,4 @@ gradio>=4.0.0
|
|
| 2 |
pandas>=1.5.0
|
| 3 |
numpy>=1.21.0
|
| 4 |
datasets>=2.0.0
|
| 5 |
-
huggingface_hub>=0.16.0
|
| 6 |
-
transformers>=4.21.0
|
| 7 |
-
torch>=1.9.0
|
| 8 |
-
accelerate>=0.20.0
|
|
|
|
| 2 |
pandas>=1.5.0
|
| 3 |
numpy>=1.21.0
|
| 4 |
datasets>=2.0.0
|
| 5 |
+
huggingface_hub>=0.16.0
|
|
|
|
|
|
|
|
|