Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +112 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
|
| 7 |
+
# Load environment variables from .env file
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# Get API keys from environment variables
|
| 11 |
+
ALPHA_VANTAGE_API_KEY = os.getenv('ALPHA_VANTAGE_API_KEY')
|
| 12 |
+
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
| 13 |
+
|
| 14 |
+
# Validate API keys
|
| 15 |
+
if not ALPHA_VANTAGE_API_KEY or not OPENAI_API_KEY:
|
| 16 |
+
raise ValueError("Missing API keys. Please check your .env file.")
|
| 17 |
+
|
| 18 |
+
# Configure OpenAI
|
| 19 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 20 |
+
|
| 21 |
+
def get_stock_price(symbol):
|
| 22 |
+
"""Fetch stock price from Alpha Vantage API"""
|
| 23 |
+
if not symbol:
|
| 24 |
+
return "Please enter a stock symbol"
|
| 25 |
+
|
| 26 |
+
url = f"https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={symbol}&apikey={ALPHA_VANTAGE_API_KEY}"
|
| 27 |
+
response = requests.get(url)
|
| 28 |
+
data = response.json()
|
| 29 |
+
|
| 30 |
+
if "Global Quote" in data and "05. price" in data["Global Quote"]:
|
| 31 |
+
return float(data["Global Quote"]["05. price"])
|
| 32 |
+
elif "Note" in data: # API rate limit message
|
| 33 |
+
return "API rate limit reached. Please try again in a minute."
|
| 34 |
+
return "Stock symbol not found"
|
| 35 |
+
|
| 36 |
+
def get_ai_analysis(company_name):
|
| 37 |
+
"""Get AI analysis using GPT-3.5"""
|
| 38 |
+
prompt = f"""
|
| 39 |
+
Provide a brief analysis of {company_name} as a long-term investment.
|
| 40 |
+
Consider historical milestones, recent announcements, and market position.
|
| 41 |
+
Keep the response under 150 words.
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
response = client.chat.completions.create(
|
| 45 |
+
model="gpt-3.5-turbo",
|
| 46 |
+
messages=[
|
| 47 |
+
{"role": "system", "content": "You are a financial analyst providing brief stock analyses."},
|
| 48 |
+
{"role": "user", "content": prompt}
|
| 49 |
+
]
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
return response.choices[0].message.content
|
| 53 |
+
|
| 54 |
+
def get_stock_symbol(company_name):
|
| 55 |
+
"""Convert company name to stock symbol using GPT-3.5"""
|
| 56 |
+
prompt = f"""
|
| 57 |
+
What is the stock symbol for {company_name}?
|
| 58 |
+
Provide only the symbol without any additional text or explanation.
|
| 59 |
+
For example, if asked about Apple, respond with: AAPL
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
response = client.chat.completions.create(
|
| 64 |
+
model="gpt-3.5-turbo",
|
| 65 |
+
messages=[
|
| 66 |
+
{"role": "system", "content": "You are a financial expert. Respond only with stock symbols in capital letters."},
|
| 67 |
+
{"role": "user", "content": prompt}
|
| 68 |
+
]
|
| 69 |
+
)
|
| 70 |
+
return response.choices[0].message.content.strip()
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return None
|
| 73 |
+
|
| 74 |
+
def analyze_stock(company_input):
|
| 75 |
+
"""Main function to process user input and return results"""
|
| 76 |
+
try:
|
| 77 |
+
# Convert company name to symbol if needed
|
| 78 |
+
if not company_input.isupper() or len(company_input.split()) > 1:
|
| 79 |
+
symbol = get_stock_symbol(company_input)
|
| 80 |
+
if not symbol:
|
| 81 |
+
return "Could not determine stock symbol", "Please try entering the stock symbol directly"
|
| 82 |
+
else:
|
| 83 |
+
symbol = company_input
|
| 84 |
+
|
| 85 |
+
# Get stock price
|
| 86 |
+
price = get_stock_price(symbol)
|
| 87 |
+
|
| 88 |
+
# Get AI analysis
|
| 89 |
+
analysis = get_ai_analysis(company_input)
|
| 90 |
+
|
| 91 |
+
return f"${price:.2f}" if isinstance(price, float) else price, analysis
|
| 92 |
+
except Exception as e:
|
| 93 |
+
return str(e), "Error occurred while analyzing the stock"
|
| 94 |
+
|
| 95 |
+
# Update Gradio interface
|
| 96 |
+
iface = gr.Interface(
|
| 97 |
+
fn=analyze_stock,
|
| 98 |
+
inputs=[
|
| 99 |
+
gr.Textbox(label="Enter Company Name or Symbol (e.g., 'Apple' or 'AAPL')", placeholder="Enter company name or stock symbol...")
|
| 100 |
+
],
|
| 101 |
+
outputs=[
|
| 102 |
+
gr.Textbox(label="Current Stock Price"),
|
| 103 |
+
gr.Textbox(label="AI Analysis", lines=5)
|
| 104 |
+
],
|
| 105 |
+
title="Stock Analysis Assistant",
|
| 106 |
+
description="Get real-time stock prices and AI-powered investment analysis",
|
| 107 |
+
theme=gr.themes.Soft()
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# Launch the application
|
| 111 |
+
if __name__ == "__main__":
|
| 112 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
requests>=2.31.0
|
| 3 |
+
openai>=1.12.0
|
| 4 |
+
python-dotenv>=1.0.0
|