Upload 3 files
Browse files- app.py +55 -0
- requirements.txt +4 -0
- stock_utils.py +25 -0
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from stock_utils import get_stock_data
|
| 5 |
+
|
| 6 |
+
# Initialize OpenAI client using Hugging Face secret
|
| 7 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 8 |
+
|
| 9 |
+
def analyze_stock(ticker):
|
| 10 |
+
try:
|
| 11 |
+
# Get stock data using yfinance
|
| 12 |
+
data = get_stock_data(ticker)
|
| 13 |
+
if "error" in data:
|
| 14 |
+
return f"Data fetch error: {data['error']}"
|
| 15 |
+
|
| 16 |
+
# Construct prompt
|
| 17 |
+
prompt = (
|
| 18 |
+
"You are a financial analyst. Analyze the following stock data and provide:\n"
|
| 19 |
+
"1. A BUY / HOLD / SELL recommendation\n"
|
| 20 |
+
"2. Reason for the recommendation\n"
|
| 21 |
+
"3. Summary in bullet points\n\n"
|
| 22 |
+
f"Stock Data:\n{data}"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Call OpenAI chat model
|
| 26 |
+
response = client.chat.completions.create(
|
| 27 |
+
model="gpt-4o",
|
| 28 |
+
messages=[{"role": "user", "content": prompt}]
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
raw_text = response.choices[0].message.content.strip()
|
| 32 |
+
|
| 33 |
+
# Clean markdown artifacts (if model wraps in ``` or quotes)
|
| 34 |
+
if raw_text.startswith("```") and raw_text.endswith("```"):
|
| 35 |
+
raw_text = raw_text[3:-3].strip()
|
| 36 |
+
elif raw_text.startswith("'''") and raw_text.endswith("'''"):
|
| 37 |
+
raw_text = raw_text[3:-3].strip()
|
| 38 |
+
elif raw_text.startswith('"') and raw_text.endswith('"'):
|
| 39 |
+
raw_text = raw_text[1:-1].strip()
|
| 40 |
+
|
| 41 |
+
return raw_text
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"❌ Unexpected error: {str(e)}"
|
| 45 |
+
|
| 46 |
+
# Gradio app UI
|
| 47 |
+
demo = gr.Interface(
|
| 48 |
+
fn=analyze_stock,
|
| 49 |
+
inputs=gr.Textbox(label="Enter Stock Ticker (e.g., AAPL)"),
|
| 50 |
+
outputs=gr.Textbox(label="Agent Recommendation"),
|
| 51 |
+
title="📈 AI Stock Advisor Agent",
|
| 52 |
+
description="Enter a stock ticker to get GPT-4o-powered insights and a recommendation"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
openai>=1.2.0
|
| 3 |
+
yfinance
|
| 4 |
+
pandas
|
stock_utils.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import yfinance as yf
|
| 2 |
+
|
| 3 |
+
def get_stock_data(ticker):
|
| 4 |
+
try:
|
| 5 |
+
stock = yf.Ticker(ticker)
|
| 6 |
+
hist = stock.history(period="5d")
|
| 7 |
+
info = stock.info
|
| 8 |
+
|
| 9 |
+
data = {
|
| 10 |
+
"ticker": ticker,
|
| 11 |
+
"company_name": info.get("longName", "N/A"),
|
| 12 |
+
"current_price": info.get("currentPrice", "N/A"),
|
| 13 |
+
"pe_ratio": info.get("trailingPE", "N/A"),
|
| 14 |
+
"market_cap": info.get("marketCap", "N/A"),
|
| 15 |
+
"day_low": info.get("dayLow", "N/A"),
|
| 16 |
+
"day_high": info.get("dayHigh", "N/A"),
|
| 17 |
+
"volume": info.get("volume", "N/A"),
|
| 18 |
+
"52_week_high": info.get("fiftyTwoWeekHigh", "N/A"),
|
| 19 |
+
"52_week_low": info.get("fiftyTwoWeekLow", "N/A"),
|
| 20 |
+
"recent_close_prices": hist['Close'].tail(3).to_list()
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
return data
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return {"error": str(e)}
|