Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -8,59 +8,85 @@ from crypto__analysis_tasks import CryptoAnalysisTasks
|
|
| 8 |
class CryptoCrew:
|
| 9 |
def __init__(self, crypto):
|
| 10 |
self.crypto = crypto
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def run(self):
|
| 13 |
return asyncio.run(self.run_async())
|
| 14 |
|
| 15 |
async def run_async(self):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
market_analyst_agent = agents.market_analyst()
|
| 20 |
-
technical_analyst_agent = agents.technical_analyst()
|
| 21 |
-
crypto_advisor_agent = agents.crypto_advisor()
|
| 22 |
-
|
| 23 |
-
market_research_task = tasks.market_research(market_analyst_agent, self.crypto)
|
| 24 |
-
technical_analysis_task = tasks.technical_analysis(technical_analyst_agent, self.crypto)
|
| 25 |
-
sentiment_analysis_task = tasks.sentiment_analysis(market_analyst_agent, self.crypto)
|
| 26 |
-
recommend_task = tasks.recommend(crypto_advisor_agent, self.crypto)
|
| 27 |
-
|
| 28 |
-
crew = Crew(
|
| 29 |
-
agents=[
|
| 30 |
-
market_analyst_agent,
|
| 31 |
-
technical_analyst_agent,
|
| 32 |
-
crypto_advisor_agent
|
| 33 |
-
],
|
| 34 |
-
tasks=[
|
| 35 |
-
market_research_task,
|
| 36 |
-
technical_analysis_task,
|
| 37 |
-
sentiment_analysis_task,
|
| 38 |
-
recommend_task
|
| 39 |
-
],
|
| 40 |
-
verbose=True,
|
| 41 |
-
process=Process.sequential,
|
| 42 |
-
max_iterations=100,
|
| 43 |
-
task_timeout=600
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
result = await asyncio.to_thread(crew.kickoff)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
return self.parse_result(result)
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
def parse_result(self, result):
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
"sentiment": {
|
| 56 |
-
"overall":
|
| 57 |
-
"social_media":
|
| 58 |
-
"news":
|
| 59 |
-
"community":
|
| 60 |
-
}
|
|
|
|
|
|
|
| 61 |
}
|
| 62 |
-
# Add your parsing logic here
|
| 63 |
-
return parsed
|
| 64 |
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
|
|
|
| 8 |
class CryptoCrew:
|
| 9 |
def __init__(self, crypto):
|
| 10 |
self.crypto = crypto
|
| 11 |
+
# Initialize agents once for reuse
|
| 12 |
+
self.agents_instance = CryptoAnalysisAgents()
|
| 13 |
+
self.tasks_instance = CryptoAnalysisTasks()
|
| 14 |
|
| 15 |
def run(self):
|
| 16 |
return asyncio.run(self.run_async())
|
| 17 |
|
| 18 |
async def run_async(self):
|
| 19 |
+
start_time = time.time()
|
| 20 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
+
# Create agents
|
| 23 |
+
market_analyst = self.agents_instance.market_analyst()
|
| 24 |
+
technical_analyst = self.agents_instance.technical_analyst()
|
| 25 |
+
advisor = self.agents_instance.crypto_advisor()
|
| 26 |
+
|
| 27 |
+
# Create tasks
|
| 28 |
+
tasks = [
|
| 29 |
+
self.tasks_instance.market_research(market_analyst, self.crypto),
|
| 30 |
+
self.tasks_instance.technical_analysis(technical_analyst, self.crypto),
|
| 31 |
+
self.tasks_instance.sentiment_analysis(advisor, self.crypto),
|
| 32 |
+
self.tasks_instance.recommend(advisor, self.crypto)
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
# Optimized crew configuration
|
| 36 |
+
crew = Crew(
|
| 37 |
+
agents=[market_analyst, technical_analyst, advisor],
|
| 38 |
+
tasks=tasks,
|
| 39 |
+
verbose=False, # Disable verbose output for speed
|
| 40 |
+
process=Process.sequential,
|
| 41 |
+
max_iterations=5, # Reduced from 100
|
| 42 |
+
task_timeout=60 # Reduced from 600
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Run crew
|
| 46 |
result = await asyncio.to_thread(crew.kickoff)
|
| 47 |
+
|
| 48 |
+
end_time = time.time()
|
| 49 |
+
print(f"Analysis completed in {end_time - start_time:.2f} seconds")
|
| 50 |
+
|
| 51 |
return self.parse_result(result)
|
| 52 |
+
|
| 53 |
except Exception as e:
|
| 54 |
+
return {
|
| 55 |
+
"summary": f"Analysis failed: {str(e)}",
|
| 56 |
+
"sentiment": {"overall": "Neutral", "confidence": "Low"},
|
| 57 |
+
"recommendation": "HOLD",
|
| 58 |
+
"execution_time": time.time() - start_time
|
| 59 |
+
}
|
| 60 |
|
| 61 |
def parse_result(self, result):
|
| 62 |
+
# Enhanced parsing logic
|
| 63 |
+
result_str = str(result)
|
| 64 |
+
|
| 65 |
+
# Extract key information using simple parsing
|
| 66 |
+
sentiment = "Neutral"
|
| 67 |
+
recommendation = "HOLD"
|
| 68 |
+
|
| 69 |
+
if "Positive" in result_str or "bullish" in result_str.lower():
|
| 70 |
+
sentiment = "Positive"
|
| 71 |
+
elif "Negative" in result_str or "bearish" in result_str.lower():
|
| 72 |
+
sentiment = "Negative"
|
| 73 |
+
|
| 74 |
+
if "BUY" in result_str:
|
| 75 |
+
recommendation = "BUY"
|
| 76 |
+
elif "SELL" in result_str:
|
| 77 |
+
recommendation = "SELL"
|
| 78 |
+
|
| 79 |
+
return {
|
| 80 |
+
"summary": result_str,
|
| 81 |
"sentiment": {
|
| 82 |
+
"overall": sentiment,
|
| 83 |
+
"social_media": sentiment,
|
| 84 |
+
"news": sentiment,
|
| 85 |
+
"community": sentiment
|
| 86 |
+
},
|
| 87 |
+
"recommendation": recommendation,
|
| 88 |
+
"confidence": "Medium"
|
| 89 |
}
|
|
|
|
|
|
|
| 90 |
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|