Spaces:
Sleeping
Sleeping
Create app.py
Browse files
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()
|