Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import pandas as pd | |
| import plotly.graph_objs as go | |
| from transformers import pipeline | |
| # Load GPT-2 model (adjust if you're using a different supported model) | |
| chatgpt = pipeline("text-generation", model="gpt2") | |
| # Function to fetch and process data from GPT model | |
| def fetch_and_process_data(prompt): | |
| response = chatgpt(prompt, max_length=200, do_sample=True)[0]['generated_text'] | |
| return response | |
| # Function to fetch historical price data from CoinGecko | |
| def fetch_historical_data(coin_id, from_timestamp, to_timestamp): | |
| url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart/range?vs_currency=usd&from={from_timestamp}&to={to_timestamp}" | |
| response = requests.get(url) | |
| if response.status_code == 200: | |
| data = response.json() | |
| prices = data['prices'] | |
| return prices | |
| else: | |
| return f"Error fetching historical data for {coin_id}" | |
| # Function to convert dates to timestamps | |
| def date_to_timestamp(date_str): | |
| return int(pd.Timestamp(date_str).timestamp()) | |
| # Function to plot historical prices using Plotly | |
| def plot_historical_prices(coin_name, from_date, to_date): | |
| from_timestamp = date_to_timestamp(from_date) | |
| to_timestamp = date_to_timestamp(to_date) | |
| prices = fetch_historical_data(coin_name, from_timestamp, to_timestamp) | |
| if isinstance(prices, str): # In case of error | |
| return prices | |
| df = pd.DataFrame(prices, columns=['timestamp', 'price']) | |
| df['date'] = pd.to_datetime(df['timestamp'], unit='ms') | |
| fig = go.Figure() | |
| fig.add_trace(go.Scatter(x=df['date'], y=df['price'], mode='lines', name=coin_name)) | |
| fig.update_layout(title=f'{coin_name.capitalize()} Prices from {from_date} to {to_date}', | |
| xaxis_title='Date', | |
| yaxis_title='Price (USD)') | |
| # Return the plot as an HTML div element | |
| return fig.to_html() | |
| # Top 100 Cryptocurrencies (by CoinGecko IDs) | |
| top_100_cryptos = [ | |
| 'bitcoin', 'ethereum', 'binancecoin', 'ripple', 'solana', 'cardano', 'dogecoin', 'polygon', 'polkadot', 'tron', | |
| # Add more top coins as necessary | |
| ] | |
| # Function to display both ChatGPT response and price chart | |
| def combined_analysis(prompt, coin_name, from_date, to_date): | |
| # Fetch ChatGPT response | |
| chatgpt_response = fetch_and_process_data(prompt) | |
| # Fetch and plot historical price data | |
| price_chart = plot_historical_prices(coin_name, from_date, to_date) | |
| return chatgpt_response, price_chart | |
| # Create Gradio Interface | |
| interface = gr.Interface( | |
| fn=combined_analysis, | |
| inputs=[ | |
| gr.Textbox(label="Enter a prompt for ChatGPT"), | |
| gr.Dropdown(choices=top_100_cryptos, label="Select Cryptocurrency"), | |
| gr.Textbox(value="2024-01-01", label="From Date (YYYY-MM-DD)"), | |
| gr.Textbox(value="2025-12-31", label="To Date (YYYY-MM-DD)") | |
| ], | |
| outputs=[ | |
| gr.Textbox(label="ChatGPT Response"), | |
| gr.HTML(label="Cryptocurrency Price Chart") | |
| ], | |
| title="ChatGPT and Cryptocurrency Analysis", | |
| description="This tool provides real-time cryptocurrency analysis and allows you to interact with ChatGPT for insights." | |
| ) | |
| # Launch Gradio app | |
| interface.launch(server_name="0.0.0.0") | |