Spaces:
Sleeping
Sleeping
# app.py | |
pip install -r requirements.txt | |
import gradio as gr | |
import pandas as pd | |
import numpy as np | |
import yfinance as yf | |
from ta import add_all_ta_features | |
from ta.utils import dropna | |
from transformers import pipeline, AutoModelForQuestionAnswering, AutoTokenizer | |
from sentence_transformers import SentenceTransformer | |
import faiss | |
# Load the dataset from Google Drive | |
def load_data(): | |
url = "https://drive.google.com/uc?export=download&id=1N1bCVRSacs7_nENJzleqqNRHA22-H9B5" | |
df = pd.read_csv(url) | |
return df | |
# Preprocess the data | |
def preprocess_data(df): | |
df = dropna(df) | |
df = add_all_ta_features(df, open="Open", high="High", low="Low", close="Close", volume="Volume", fillna=True) | |
return df | |
# Train the FAISS index for RAG | |
def train_faiss_index(df): | |
model = SentenceTransformer('all-MiniLM-L6-v2') | |
embeddings = model.encode(df['Close'].astype(str).tolist()) | |
dimension = embeddings.shape[1] | |
index = faiss.IndexFlatL2(dimension) | |
index.add(embeddings) | |
return index, model | |
# Load the QA model | |
def load_qa_model(): | |
model_name = "deepset/roberta-base-squad2" | |
model = AutoModelForQuestionAnswering.from_pretrained(model_name) | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer) | |
return qa_pipeline | |
# Get technical analysis | |
def get_technical_analysis(df): | |
analysis = { | |
"SMA_50": df['Close'].rolling(window=50).mean().iloc[-1], | |
"SMA_200": df['Close'].rolling(window=200).mean().iloc[-1], | |
"RSI": df['momentum_rsi'].iloc[-1], | |
"MACD": df['trend_macd'].iloc[-1], | |
} | |
return analysis | |
# RAG-based QA function | |
def rag_qa(question, df, index, model, qa_pipeline): | |
query_embedding = model.encode([question]) | |
distances, indices = index.search(query_embedding, k=1) | |
context = df.iloc[indices[0][0]]['Close'] | |
result = qa_pipeline(question=question, context=str(context)) | |
return result['answer'] | |
# Gradio Interface | |
def trading_analysis_app(question): | |
df = load_data() | |
df = preprocess_data(df) | |
index, model = train_faiss_index(df) | |
qa_pipeline = load_qa_model() | |
analysis = get_technical_analysis(df) | |
answer = rag_qa(question, df, index, model, qa_pipeline) | |
return f"Technical Analysis: {analysis}\n\nAnswer: {answer}" | |
# Gradio Interface | |
iface = gr.Interface( | |
fn=trading_analysis_app, | |
inputs="text", | |
outputs="text", | |
title="RAG-based Trading Analysis", | |
description="Enter your question about ICICIBANK's stock to get technical analysis and answers." | |
) | |
iface.launch() |