Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import yfinance as yf
|
| 6 |
+
from ta import add_all_ta_features
|
| 7 |
+
from ta.utils import dropna
|
| 8 |
+
from transformers import pipeline, AutoModelForQuestionAnswering, AutoTokenizer
|
| 9 |
+
from sentence_transformers import SentenceTransformer
|
| 10 |
+
import faiss
|
| 11 |
+
|
| 12 |
+
# Load the dataset from Google Drive
|
| 13 |
+
def load_data():
|
| 14 |
+
url = "https://drive.google.com/uc?export=download&id=1N1bCVRSacs7_nENJzleqqNRHA22-H9B5"
|
| 15 |
+
df = pd.read_csv(url)
|
| 16 |
+
return df
|
| 17 |
+
|
| 18 |
+
# Preprocess the data
|
| 19 |
+
def preprocess_data(df):
|
| 20 |
+
df = dropna(df)
|
| 21 |
+
df = add_all_ta_features(df, open="Open", high="High", low="Low", close="Close", volume="Volume", fillna=True)
|
| 22 |
+
return df
|
| 23 |
+
|
| 24 |
+
# Train the FAISS index for RAG
|
| 25 |
+
def train_faiss_index(df):
|
| 26 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 27 |
+
embeddings = model.encode(df['Close'].astype(str).tolist())
|
| 28 |
+
dimension = embeddings.shape[1]
|
| 29 |
+
index = faiss.IndexFlatL2(dimension)
|
| 30 |
+
index.add(embeddings)
|
| 31 |
+
return index, model
|
| 32 |
+
|
| 33 |
+
# Load the QA model
|
| 34 |
+
def load_qa_model():
|
| 35 |
+
model_name = "deepset/roberta-base-squad2"
|
| 36 |
+
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
|
| 37 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 38 |
+
qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer)
|
| 39 |
+
return qa_pipeline
|
| 40 |
+
|
| 41 |
+
# Get technical analysis
|
| 42 |
+
def get_technical_analysis(df):
|
| 43 |
+
analysis = {
|
| 44 |
+
"SMA_50": df['Close'].rolling(window=50).mean().iloc[-1],
|
| 45 |
+
"SMA_200": df['Close'].rolling(window=200).mean().iloc[-1],
|
| 46 |
+
"RSI": df['momentum_rsi'].iloc[-1],
|
| 47 |
+
"MACD": df['trend_macd'].iloc[-1],
|
| 48 |
+
}
|
| 49 |
+
return analysis
|
| 50 |
+
|
| 51 |
+
# RAG-based QA function
|
| 52 |
+
def rag_qa(question, df, index, model, qa_pipeline):
|
| 53 |
+
query_embedding = model.encode([question])
|
| 54 |
+
distances, indices = index.search(query_embedding, k=1)
|
| 55 |
+
context = df.iloc[indices[0][0]]['Close']
|
| 56 |
+
result = qa_pipeline(question=question, context=str(context))
|
| 57 |
+
return result['answer']
|
| 58 |
+
|
| 59 |
+
# Gradio Interface
|
| 60 |
+
def trading_analysis_app(question):
|
| 61 |
+
df = load_data()
|
| 62 |
+
df = preprocess_data(df)
|
| 63 |
+
index, model = train_faiss_index(df)
|
| 64 |
+
qa_pipeline = load_qa_model()
|
| 65 |
+
|
| 66 |
+
analysis = get_technical_analysis(df)
|
| 67 |
+
answer = rag_qa(question, df, index, model, qa_pipeline)
|
| 68 |
+
|
| 69 |
+
return f"Technical Analysis: {analysis}\n\nAnswer: {answer}"
|
| 70 |
+
|
| 71 |
+
# Gradio Interface
|
| 72 |
+
iface = gr.Interface(
|
| 73 |
+
fn=trading_analysis_app,
|
| 74 |
+
inputs="text",
|
| 75 |
+
outputs="text",
|
| 76 |
+
title="RAG-based Trading Analysis",
|
| 77 |
+
description="Enter your question about ICICIBANK's stock to get technical analysis and answers."
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
iface.launch()
|