missbaj commited on
Commit
57ecc28
·
verified ·
1 Parent(s): 2fa5f66
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import pandas as pd
4
+ import plotly.graph_objs as go
5
+ from transformers import pipeline
6
+
7
+ # Load ChatGPT model (adjust to use a model supported in Hugging Face Spaces)
8
+ chatgpt = pipeline("text-generation", model="gpt2") # Change "gpt2" to "chatgpt" if available
9
+
10
+ # Function to fetch and process data from GPT model
11
+ def fetch_and_process_data(prompt):
12
+ response = chatgpt(prompt, max_length=200, do_sample=True)[0]['generated_text']
13
+ return response
14
+
15
+ # Function to fetch historical price data from CoinGecko
16
+ def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
17
+ url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart/range?vs_currency=usd&from={from_timestamp}&to={to_timestamp}"
18
+ response = requests.get(url)
19
+ if response.status_code == 200:
20
+ data = response.json()
21
+ prices = data['prices']
22
+ return prices
23
+ else:
24
+ return f"Error fetching historical data for {coin_id}"
25
+
26
+ # Function to convert dates to timestamps
27
+ def date_to_timestamp(date_str):
28
+ return int(pd.Timestamp(date_str).timestamp())
29
+
30
+ # Function to plot historical prices using Plotly
31
+ def plot_historical_prices(coin_name, from_date, to_date):
32
+ from_timestamp = date_to_timestamp(from_date)
33
+ to_timestamp = date_to_timestamp(to_date)
34
+
35
+ prices = fetch_historical_data(coin_name, from_timestamp, to_timestamp)
36
+
37
+ if isinstance(prices, str): # In case of error
38
+ return prices
39
+
40
+ df = pd.DataFrame(prices, columns=['timestamp', 'price'])
41
+ df['date'] = pd.to_datetime(df['timestamp'], unit='ms')
42
+
43
+ fig = go.Figure()
44
+ fig.add_trace(go.Scatter(x=df['date'], y=df['price'], mode='lines', name=coin_name))
45
+ fig.update_layout(title=f'{coin_name.capitalize()} Prices from {from_date} to {to_date}', xaxis_title='Date', yaxis_title='Price (USD)')
46
+ return fig
47
+
48
+ # Top 100 Cryptocurrencies (by CoinGecko IDs)
49
+ top_100_cryptos = [
50
+ 'bitcoin', 'ethereum', 'binancecoin', 'ripple', 'solana', 'cardano', 'dogecoin', 'polygon', 'polkadot', 'tron',
51
+ # Add more top coins as necessary
52
+ ]
53
+
54
+ # Function to display both ChatGPT response and price chart
55
+ def combined_analysis(prompt, coin_name, from_date, to_date):
56
+ # Fetch ChatGPT response
57
+ chatgpt_response = fetch_and_process_data(prompt)
58
+
59
+ # Fetch and plot historical price data
60
+ price_chart = plot_historical_prices(coin_name, from_date, to_date)
61
+
62
+ return chatgpt_response, price_chart
63
+
64
+ # Create Gradio Interface
65
+ interface = gr.Interface(
66
+ fn=combined_analysis,
67
+ inputs=[
68
+ gr.Textbox(label="Enter a prompt for ChatGPT"),
69
+ gr.Dropdown(choices=top_100_cryptos, label="Select Cryptocurrency"),
70
+ gr.Textbox(value="2024-01-01", label="From Date (YYYY-MM-DD)"),
71
+ gr.Textbox(value="2025-12-31", label="To Date (YYYY-MM-DD)")
72
+ ],
73
+ outputs=[
74
+ gr.Textbox(label="ChatGPT Response"),
75
+ gr.Plot(label="Cryptocurrency Price Chart")
76
+ ],
77
+ title="ChatGPT and Cryptocurrency Analysis",
78
+ description="This tool provides real-time cryptocurrency analysis and allows you to interact with ChatGPT for insights."
79
+ )
80
+
81
+ # Launch Gradio app
82
+ interface.launch(server_name="0.0.0.0")