File size: 12,742 Bytes
20eee66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import gradio as gr
import os
import json
from src.enhanced_agent import EnhancedResearchAgent
from src.portfolio_analyzer import portfolio_analyzer
from src.visualizations import create_price_chart, create_market_overview, create_comparison_chart
from src.cache_manager import cache_manager
import asyncio

research_agent = EnhancedResearchAgent()

async def process_research_query(query, history):
    try:
        if not query.strip():
            return history + [["Please enter a research query.", ""]]
        
        response = await research_agent.research_with_context(query)
        return history + [[query, response]]
    except Exception as e:
        error_msg = f"Enhanced research failed: {str(e)}"
        return history + [[query, error_msg]]

def research_query_sync(query, history):
    return asyncio.run(process_research_query(query, history))

async def get_market_data():
    try:
        data = await research_agent.get_comprehensive_market_data()
        chart = create_market_overview(data)
        return chart
    except Exception as e:
        return f"Enhanced market data unavailable: {str(e)}"

def get_market_data_sync():
    return asyncio.run(get_market_data())

async def get_price_chart(symbol):
    try:
        if not symbol.strip():
            return "Please enter a cryptocurrency symbol"
        
        data = await research_agent.get_price_history(symbol)
        chart = create_price_chart(data, symbol)
        return chart
    except Exception as e:
        return f"Chart generation failed: {str(e)}"

def get_price_chart_sync(symbol):
    return asyncio.run(get_price_chart(symbol))

async def analyze_portfolio_async(portfolio_text):
    try:
        if not portfolio_text.strip():
            return "Please enter your portfolio holdings in JSON format"
        
        holdings = json.loads(portfolio_text)
        analysis = await portfolio_analyzer.analyze_portfolio(holdings)
        
        result = f"πŸ“Š PORTFOLIO ANALYSIS\n\n"
        result += f"πŸ’° Total Value: ${analysis['total_value']:,.2f}\n"
        result += f"πŸ“ˆ 24h Change: ${analysis['change_24h']:+,.2f} ({analysis['change_24h_percentage']:+.2f}%)\n\n"
        
        result += "🏦 ASSET ALLOCATION:\n"
        for asset in analysis['asset_allocation'][:10]:
            result += f"β€’ {asset['name']} ({asset['symbol']}): {asset['percentage']:.1f}% (${asset['value']:,.2f})\n"
        
        result += f"\n⚠️ RISK ASSESSMENT:\n"
        result += f"Overall Risk: {analysis['risk_metrics']['overall_risk']}\n"
        result += f"Diversification Score: {analysis['risk_metrics']['diversification_score']}/10\n"
        result += f"Largest Position: {analysis['risk_metrics']['largest_holding_percentage']:.1f}%\n\n"
        
        result += "πŸ’‘ RECOMMENDATIONS:\n"
        for i, rec in enumerate(analysis['recommendations'], 1):
            result += f"{i}. {rec}\n"
        
        return result
        
    except json.JSONDecodeError:
        return "❌ Invalid JSON format. Please use format: [{'symbol': 'BTC', 'amount': 1.0}, {'symbol': 'ETH', 'amount': 10.0}]"
    except Exception as e:
        return f"❌ Portfolio analysis failed: {str(e)}"

async def get_defi_analysis_async():
    try:
        data = await research_agent.get_defi_analysis()
        
        result = "🏦 DeFi ECOSYSTEM ANALYSIS\n\n"
        
        if "top_protocols" in data:
            result += "πŸ“Š TOP PROTOCOLS BY TVL:\n"
            for i, protocol in enumerate(data["top_protocols"][:10], 1):
                name = protocol.get("name", "Unknown")
                tvl = protocol.get("tvl", 0)
                chain = protocol.get("chain", "Unknown")
                change = protocol.get("change_1d", 0)
                result += f"{i:2d}. {name} ({chain}): ${tvl/1e9:.2f}B TVL ({change:+.2f}%)\n"
        
        if "top_yields" in data:
            result += "\nπŸ’° HIGH YIELD OPPORTUNITIES:\n"
            for i, pool in enumerate(data["top_yields"][:5], 1):
                symbol = pool.get("symbol", "Unknown")
                apy = pool.get("apy", 0)
                tvl = pool.get("tvlUsd", 0)
                result += f"{i}. {symbol}: {apy:.2f}% APY (${tvl/1e6:.1f}M TVL)\n"
        
        return result
        
    except Exception as e:
        return f"❌ DeFi analysis failed: {str(e)}"

def clear_cache():
    cache_manager.clear()
    return "Cache cleared successfully"

def analyze_portfolio_sync(portfolio_text):
    return asyncio.run(analyze_portfolio_async(portfolio_text))

def get_defi_analysis_sync():
    return asyncio.run(get_defi_analysis_async())

with gr.Blocks(
    title="Web3 Research Co-Pilot",
    theme=gr.themes.Soft(primary_hue="blue", secondary_hue="gray"),
    css="""
    .container { max-width: 1200px; margin: 0 auto; }
    .header { text-align: center; padding: 20px; }
    .chat-container { min-height: 400px; }
    .chart-container { min-height: 500px; }
    """
) as app:
    
    gr.Markdown("# πŸš€ Web3 Research Co-Pilot", elem_classes=["header"])
    gr.Markdown("*AI-powered cryptocurrency research with real-time data integration*", elem_classes=["header"])
    
    with gr.Tabs():
        
        with gr.Tab("πŸ€– Research Chat"):
            with gr.Row():
                with gr.Column(scale=3):
                    chatbot = gr.Chatbot(
                        value=[],
                        height=400,
                        elem_classes=["chat-container"],
                        show_label=False
                    )
                    
                    with gr.Row():
                        query_input = gr.Textbox(
                            placeholder="Ask about crypto markets, prices, trends, analysis...",
                            scale=4,
                            show_label=False
                        )
                        submit_btn = gr.Button("Research", variant="primary")
                    
                    gr.Examples(
                        examples=[
                            "What's the current Bitcoin price and trend?",
                            "Compare Ethereum vs Solana DeFi ecosystems",
                            "Analyze top DeFi protocols and TVL trends",
                            "What are the trending coins and latest crypto news?",
                            "Show me high-yield DeFi opportunities with risk analysis"
                        ],
                        inputs=query_input
                    )
                
                with gr.Column(scale=1):
                    gr.Markdown("### πŸ“Š Quick Actions")
                    market_btn = gr.Button("Market Overview", size="sm")
                    market_output = gr.HTML()
                    
                    clear_btn = gr.Button("Clear Cache", size="sm", variant="secondary")
                    clear_output = gr.Textbox(show_label=False, interactive=False)
        
        with gr.Tab("πŸ“ˆ Price Charts"):
            with gr.Row():
                symbol_input = gr.Textbox(
                    label="Cryptocurrency Symbol",
                    placeholder="BTC, ETH, SOL, etc.",
                    value="BTC"
                )
                chart_btn = gr.Button("Generate Chart", variant="primary")
            
            chart_output = gr.HTML(elem_classes=["chart-container"])
            
            gr.Examples(
                examples=["BTC", "ETH", "SOL", "ADA", "DOT"],
                inputs=symbol_input
            )
        
        with gr.Tab("πŸ’Ό Portfolio Analysis"):
            with gr.Row():
                with gr.Column(scale=1):
                    portfolio_input = gr.Textbox(
                        label="Portfolio Holdings (JSON Format)",
                        placeholder='[{"symbol": "BTC", "amount": 1.0}, {"symbol": "ETH", "amount": 10.0}, {"symbol": "SOL", "amount": 50.0}]',
                        lines=5,
                        info="Enter your crypto holdings in JSON format with symbol and amount"
                    )
                    portfolio_btn = gr.Button("Analyze Portfolio", variant="primary")
                
                with gr.Column(scale=2):
                    portfolio_output = gr.Textbox(
                        label="Portfolio Analysis Results",
                        lines=20,
                        show_copy_button=True,
                        interactive=False
                    )
            
            gr.Examples(
                examples=[
                    '[{"symbol": "BTC", "amount": 0.5}, {"symbol": "ETH", "amount": 5.0}]',
                    '[{"symbol": "BTC", "amount": 1.0}, {"symbol": "ETH", "amount": 10.0}, {"symbol": "SOL", "amount": 100.0}]'
                ],
                inputs=portfolio_input
            )
        
        with gr.Tab("🏦 DeFi Analytics"):
            defi_btn = gr.Button("Get DeFi Ecosystem Analysis", variant="primary", size="lg")
            defi_output = gr.Textbox(
                label="DeFi Analysis Results",
                lines=25,
                show_copy_button=True,
                interactive=False,
                info="Comprehensive DeFi protocol analysis with TVL data and yield opportunities"
            )
        
        with gr.Tab("ℹ️ About"):
            gr.Markdown("""
            ## πŸš€ Enhanced Features
            - **Multi-API Integration**: CoinGecko, CryptoCompare, and DeFiLlama data sources
            - **AI-Powered Analysis**: Google Gemini 2.5 Flash with contextual market intelligence
            - **DeFi Analytics**: Protocol TVL analysis, yield farming opportunities, and ecosystem insights
            - **Portfolio Analysis**: Risk assessment, diversification scoring, and personalized recommendations
            - **News Integration**: Real-time crypto news aggregation and sentiment analysis
            - **Interactive Charts**: Advanced price visualizations with technical indicators
            - **Smart Caching**: Optimized performance with intelligent data caching (TTL-based)
            - **Rate Limiting**: Respectful API usage with automatic throttling
            
            ## 🎯 Core Capabilities
            1. **Enhanced Research Chat**: Context-aware conversations with real-time market data integration
            2. **Advanced Price Charts**: Interactive visualizations with 30-day historical data
            3. **Portfolio Optimization**: Comprehensive portfolio analysis with risk metrics and recommendations
            4. **DeFi Intelligence**: Protocol rankings, TVL trends, and high-yield opportunity identification
            5. **Market Intelligence**: Global market metrics, trending assets, and breaking news analysis
            
            ## πŸ’‘ Query Examples
            - "Analyze Bitcoin vs Ethereum DeFi ecosystem performance"
            - "What are the top DeFi protocols by TVL with lowest risk?"
            - "Show me high-yield farming opportunities under 15% volatility"
            - "Compare my portfolio risk to market benchmarks"
            - "Latest crypto news impact on altcoin market sentiment"
            - "Which Layer 1 protocols have strongest DeFi adoption?"
            
            ## πŸ”§ Technical Architecture
            - **Async Processing**: Non-blocking operations for optimal performance
            - **Error Handling**: Comprehensive exception management with graceful degradation
            - **Symbol Mapping**: Intelligent cryptocurrency identifier resolution
            - **Data Validation**: Input sanitization and response formatting
            """)
    
    submit_btn.click(
        research_query_sync,
        inputs=[query_input, chatbot],
        outputs=chatbot
    ).then(lambda: "", outputs=query_input)
    
    query_input.submit(
        research_query_sync,
        inputs=[query_input, chatbot],
        outputs=chatbot
    ).then(lambda: "", outputs=query_input)
    
    chart_btn.click(
        get_price_chart_sync,
        inputs=symbol_input,
        outputs=chart_output
    )
    
    symbol_input.submit(
        get_price_chart_sync,
        inputs=symbol_input,
        outputs=chart_output
    )
    
    market_btn.click(
        get_market_data_sync,
        outputs=market_output
    )
    
    clear_btn.click(
        clear_cache,
        outputs=clear_output
    )
    
    portfolio_btn.click(
        analyze_portfolio_sync,
        inputs=portfolio_input,
        outputs=portfolio_output
    )
    
    defi_btn.click(
        get_defi_analysis_sync,
        outputs=defi_output
    )

if __name__ == "__main__":
    app.launch(
        server_name="0.0.0.0",
        server_port=5000,
        share=False,
        show_error=True
    )