Spaces:
Runtime error
Runtime error
nananie143
commited on
Upload folder using huggingface_hub
Browse files- reasoning/market_analysis.py +108 -0
reasoning/market_analysis.py
CHANGED
@@ -9,6 +9,8 @@ from datetime import datetime
|
|
9 |
import numpy as np
|
10 |
from collections import defaultdict
|
11 |
|
|
|
|
|
12 |
@dataclass
|
13 |
class MarketSegment:
|
14 |
"""Market segment analysis."""
|
@@ -340,3 +342,109 @@ class MarketAnalyzer:
|
|
340 |
for t in self.trends
|
341 |
]
|
342 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
import numpy as np
|
10 |
from collections import defaultdict
|
11 |
|
12 |
+
from .base import ReasoningStrategy
|
13 |
+
|
14 |
@dataclass
|
15 |
class MarketSegment:
|
16 |
"""Market segment analysis."""
|
|
|
342 |
for t in self.trends
|
343 |
]
|
344 |
}
|
345 |
+
|
346 |
+
class MarketAnalysisStrategy(ReasoningStrategy):
|
347 |
+
"""
|
348 |
+
Advanced market analysis strategy that combines multiple analytical tools
|
349 |
+
to provide comprehensive market insights.
|
350 |
+
"""
|
351 |
+
|
352 |
+
def __init__(self, config: Optional[Dict[str, Any]] = None):
|
353 |
+
"""Initialize market analysis strategy."""
|
354 |
+
super().__init__()
|
355 |
+
self.config = config or {}
|
356 |
+
self.analyzer = MarketAnalyzer()
|
357 |
+
|
358 |
+
async def reason(self, query: str, context: Dict[str, Any]) -> Dict[str, Any]:
|
359 |
+
"""
|
360 |
+
Perform market analysis based on query and context.
|
361 |
+
|
362 |
+
Args:
|
363 |
+
query: The market analysis query
|
364 |
+
context: Additional context and parameters
|
365 |
+
|
366 |
+
Returns:
|
367 |
+
Dict containing market analysis results and confidence scores
|
368 |
+
"""
|
369 |
+
try:
|
370 |
+
# Extract market segment from query/context
|
371 |
+
segment = self._extract_segment(query, context)
|
372 |
+
|
373 |
+
# Perform market analysis
|
374 |
+
analysis = await self._analyze_market(segment, context)
|
375 |
+
|
376 |
+
# Get insights
|
377 |
+
insights = self.analyzer.get_market_insights()
|
378 |
+
|
379 |
+
# Calculate confidence based on data quality and completeness
|
380 |
+
confidence = self._calculate_confidence(analysis, insights)
|
381 |
+
|
382 |
+
return {
|
383 |
+
'answer': self._format_insights(insights),
|
384 |
+
'confidence': confidence,
|
385 |
+
'analysis': analysis,
|
386 |
+
'insights': insights,
|
387 |
+
'segment': segment
|
388 |
+
}
|
389 |
+
|
390 |
+
except Exception as e:
|
391 |
+
logging.error(f"Market analysis failed: {str(e)}")
|
392 |
+
return {
|
393 |
+
'error': f"Market analysis failed: {str(e)}",
|
394 |
+
'confidence': 0.0
|
395 |
+
}
|
396 |
+
|
397 |
+
def _extract_segment(self, query: str, context: Dict[str, Any]) -> str:
|
398 |
+
"""Extract market segment from query and context."""
|
399 |
+
# Use context if available
|
400 |
+
if 'segment' in context:
|
401 |
+
return context['segment']
|
402 |
+
|
403 |
+
# Default to general market
|
404 |
+
return 'general'
|
405 |
+
|
406 |
+
async def _analyze_market(self, segment: str, context: Dict[str, Any]) -> Dict[str, Any]:
|
407 |
+
"""Perform comprehensive market analysis."""
|
408 |
+
return await self.analyzer.analyze_market(segment, context)
|
409 |
+
|
410 |
+
def _calculate_confidence(self, analysis: Dict[str, Any], insights: Dict[str, Any]) -> float:
|
411 |
+
"""Calculate confidence score based on analysis quality."""
|
412 |
+
# Base confidence
|
413 |
+
confidence = 0.5
|
414 |
+
|
415 |
+
# Adjust based on data completeness
|
416 |
+
if analysis.get('segment_analysis'):
|
417 |
+
confidence += 0.1
|
418 |
+
if analysis.get('competitor_analysis'):
|
419 |
+
confidence += 0.1
|
420 |
+
if analysis.get('trend_analysis'):
|
421 |
+
confidence += 0.1
|
422 |
+
|
423 |
+
# Adjust based on insight quality
|
424 |
+
if insights.get('opportunities'):
|
425 |
+
confidence += 0.1
|
426 |
+
if insights.get('risks'):
|
427 |
+
confidence += 0.1
|
428 |
+
|
429 |
+
return min(confidence, 1.0)
|
430 |
+
|
431 |
+
def _format_insights(self, insights: Dict[str, Any]) -> str:
|
432 |
+
"""Format market insights into readable text."""
|
433 |
+
sections = []
|
434 |
+
|
435 |
+
if 'market_overview' in insights:
|
436 |
+
sections.append(f"Market Overview: {insights['market_overview']}")
|
437 |
+
|
438 |
+
if 'opportunities' in insights:
|
439 |
+
opps = insights['opportunities']
|
440 |
+
sections.append("Key Opportunities:\n- " + "\n- ".join(opps))
|
441 |
+
|
442 |
+
if 'risks' in insights:
|
443 |
+
risks = insights['risks']
|
444 |
+
sections.append("Key Risks:\n- " + "\n- ".join(risks))
|
445 |
+
|
446 |
+
if 'recommendations' in insights:
|
447 |
+
recs = insights['recommendations']
|
448 |
+
sections.append("Recommendations:\n- " + "\n- ".join(recs))
|
449 |
+
|
450 |
+
return "\n\n".join(sections)
|