|
|
|
import gradio as gr |
|
import requests |
|
import pandas as pd |
|
import plotly.graph_objs as go |
|
from transformers import pipeline |
|
|
|
|
|
chatgpt = pipeline("text-generation", model="gpt2") |
|
|
|
|
|
def fetch_and_process_data(prompt): |
|
response = chatgpt(prompt, max_length=200, do_sample=True)[0]['generated_text'] |
|
return response |
|
|
|
|
|
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}" |
|
|
|
|
|
def date_to_timestamp(date_str): |
|
return int(pd.Timestamp(date_str).timestamp()) |
|
|
|
|
|
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): |
|
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 fig |
|
|
|
|
|
top_100_cryptos = [ |
|
'bitcoin', 'ethereum', 'binancecoin', 'ripple', 'solana', 'cardano', 'dogecoin', 'polygon', 'polkadot', 'tron', |
|
|
|
] |
|
|
|
|
|
def combined_analysis(prompt, coin_name, from_date, to_date): |
|
|
|
chatgpt_response = fetch_and_process_data(prompt) |
|
|
|
|
|
price_chart = plot_historical_prices(coin_name, from_date, to_date) |
|
|
|
return chatgpt_response, price_chart |
|
|
|
|
|
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.Plot(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." |
|
) |
|
|
|
|
|
interface.launch(server_name="0.0.0.0") |
|
|