File size: 1,599 Bytes
1deb039
3f7527c
 
4c0801c
efe84a3
 
4c0801c
3f7527c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c0801c
3f7527c
 
 
4c0801c
3f7527c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import gradio as gr
from langchain_groq import ChatGroq
import yfinance as yf

# Initialize the ChatGroq model using Hugging Face's secret management
llm = ChatGroq(model_name="Llama3-8b-8192", api_key="groq_api_key")

def fetch_stock_data(company_name):
    # Fetch stock data using Yahoo Finance
    ticker = yf.Ticker(company_name)
    stock_info = ticker.info
    
    # Extract relevant information
    stock_data = {
        "Company": stock_info.get("longName", "N/A"),
        "Current Price": stock_info.get("currentPrice", "N/A"),
        "Market Cap": stock_info.get("marketCap", "N/A"),
        "PE Ratio": stock_info.get("trailingPE", "N/A"),
        "Dividend Yield": stock_info.get("dividendYield", "N/A"),
        "52 Week High": stock_info.get("fiftyTwoWeekHigh", "N/A"),
        "52 Week Low": stock_info.get("fiftyTwoWeekLow", "N/A"),
        "Sector": stock_info.get("sector", "N/A"),
        "Industry": stock_info.get("industry", "N/A")
    }
    
    return stock_data

def main(company_name):
    # Fetch stock data and generate a response
    stock_data = fetch_stock_data(company_name)
    
    # Prepare a response string
    response = f"Here is the data for {company_name}:\n"
    for key, value in stock_data.items():
        response += f"{key}: {value}\n"
        
    return response

# Create Gradio interface
iface = gr.Interface(
    fn=main,
    inputs="text",
    outputs="text",
    title="Stock Price Data Fetcher",
    description="Enter the company name or ticker symbol to get the latest stock price data."
)

if __name__ == "__main__":
    iface.launch()