Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import faiss
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Load dataset
|
| 8 |
+
@st.cache_data
|
| 9 |
+
def load_data():
|
| 10 |
+
df = pd.read_csv("finance_data.csv")
|
| 11 |
+
return df
|
| 12 |
+
|
| 13 |
+
# Embed questions
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def load_embeddings(df):
|
| 16 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 17 |
+
embeddings = model.encode(df["question"].tolist(), show_progress_bar=True)
|
| 18 |
+
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 19 |
+
index.add(np.array(embeddings))
|
| 20 |
+
return model, index, embeddings
|
| 21 |
+
|
| 22 |
+
# Get answer
|
| 23 |
+
def get_answer(query, model, index, df, embeddings, k=1):
|
| 24 |
+
query_emb = model.encode([query])
|
| 25 |
+
D, I = index.search(query_emb, k)
|
| 26 |
+
return df["answer"].iloc[I[0][0]]
|
| 27 |
+
|
| 28 |
+
# Streamlit UI
|
| 29 |
+
st.title("💸 Financial Q&A Chatbot")
|
| 30 |
+
st.markdown("Ask me anything about finance!")
|
| 31 |
+
|
| 32 |
+
df = load_data()
|
| 33 |
+
model, index, embeddings = load_embeddings(df)
|
| 34 |
+
|
| 35 |
+
query = st.text_input("Enter your question")
|
| 36 |
+
|
| 37 |
+
if query:
|
| 38 |
+
answer = get_answer(query, model, index, df, embeddings)
|
| 39 |
+
st.success(answer)
|