Spaces:
Sleeping
Sleeping
Update smartbot.py
Browse files- smartbot.py +26 -31
smartbot.py
CHANGED
|
@@ -1,41 +1,36 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import faiss
|
| 3 |
-
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
-
import torch
|
| 6 |
-
import os
|
| 7 |
from sentence_transformers import SentenceTransformer
|
|
|
|
|
|
|
| 8 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
-
from huggingface_hub import login
|
| 10 |
-
|
| 11 |
-
# --- HuggingFace login ---
|
| 12 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 13 |
-
if HF_TOKEN:
|
| 14 |
-
login(token=HF_TOKEN)
|
| 15 |
|
| 16 |
# --- Load data ---
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
# --- Embedding model ---
|
| 24 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 25 |
-
answer_embeddings = embedding_model.encode(answers)
|
| 26 |
|
| 27 |
# --- FAISS index ---
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
)
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# --- Helper Functions ---
|
| 41 |
def sanitize_answer(question, answer):
|
|
@@ -49,7 +44,7 @@ def is_finance_question(user_query):
|
|
| 49 |
f"Question: {user_query}\n\n"
|
| 50 |
f"Respond only with 'Yes' or 'No'."
|
| 51 |
)
|
| 52 |
-
input_ids = tokenizer(check_prompt, return_tensors="pt")
|
| 53 |
output_ids = model.generate(
|
| 54 |
**input_ids,
|
| 55 |
max_new_tokens=10,
|
|
@@ -68,7 +63,7 @@ def ask_finance_bot(user_query, top_k=3):
|
|
| 68 |
# Embed user query
|
| 69 |
query_embedding = embedding_model.encode([user_query])
|
| 70 |
D, I = index.search(np.array(query_embedding), top_k)
|
| 71 |
-
retrieved_answers = [
|
| 72 |
context = "\n".join([f"- {text}" for text in retrieved_answers])
|
| 73 |
|
| 74 |
temperature = min(0.7 + 0.1 * (count - 1), 1.0)
|
|
@@ -98,7 +93,7 @@ User question: {user_query}
|
|
| 98 |
|
| 99 |
Answer:"""
|
| 100 |
|
| 101 |
-
input_ids = tokenizer(prompt, return_tensors="pt")
|
| 102 |
output_ids = model.generate(
|
| 103 |
**input_ids,
|
| 104 |
max_new_tokens=256,
|
|
@@ -116,8 +111,8 @@ Answer:"""
|
|
| 116 |
return "I'm not confident in the response. Please consult a certified financial expert."
|
| 117 |
|
| 118 |
# --- Streamlit App UI ---
|
| 119 |
-
st.set_page_config(page_title="
|
| 120 |
-
st.title("
|
| 121 |
|
| 122 |
user_query = st.text_input("Enter your finance-related question:")
|
| 123 |
|
|
|
|
| 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 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# --- Load data ---
|
| 9 |
+
@st.cache_data
|
| 10 |
+
def load_data():
|
| 11 |
+
df = pd.read_csv("train_data.csv")
|
| 12 |
+
return df
|
| 13 |
|
| 14 |
+
df = load_data()
|
| 15 |
|
| 16 |
# --- Embedding model ---
|
| 17 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
|
|
| 18 |
|
| 19 |
# --- FAISS index ---
|
| 20 |
+
@st.cache_resource
|
| 21 |
+
def create_index(df):
|
| 22 |
+
embeddings = embedding_model.encode(df['question'].tolist(), show_progress_bar=True)
|
| 23 |
+
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 24 |
+
index.add(embeddings)
|
| 25 |
+
return index, embeddings
|
| 26 |
+
|
| 27 |
+
index, embeddings = create_index(df)
|
| 28 |
+
|
| 29 |
+
# --- Replace LLaMA with a smaller model ---
|
| 30 |
+
# Using a smaller model for CPU inference (FLAN-T5)
|
| 31 |
+
model_name = "google/flan-t5-base"
|
| 32 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 33 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 34 |
|
| 35 |
# --- Helper Functions ---
|
| 36 |
def sanitize_answer(question, answer):
|
|
|
|
| 44 |
f"Question: {user_query}\n\n"
|
| 45 |
f"Respond only with 'Yes' or 'No'."
|
| 46 |
)
|
| 47 |
+
input_ids = tokenizer(check_prompt, return_tensors="pt")
|
| 48 |
output_ids = model.generate(
|
| 49 |
**input_ids,
|
| 50 |
max_new_tokens=10,
|
|
|
|
| 63 |
# Embed user query
|
| 64 |
query_embedding = embedding_model.encode([user_query])
|
| 65 |
D, I = index.search(np.array(query_embedding), top_k)
|
| 66 |
+
retrieved_answers = [df.iloc[i]['answer'] for i in I[0]]
|
| 67 |
context = "\n".join([f"- {text}" for text in retrieved_answers])
|
| 68 |
|
| 69 |
temperature = min(0.7 + 0.1 * (count - 1), 1.0)
|
|
|
|
| 93 |
|
| 94 |
Answer:"""
|
| 95 |
|
| 96 |
+
input_ids = tokenizer(prompt, return_tensors="pt")
|
| 97 |
output_ids = model.generate(
|
| 98 |
**input_ids,
|
| 99 |
max_new_tokens=256,
|
|
|
|
| 111 |
return "I'm not confident in the response. Please consult a certified financial expert."
|
| 112 |
|
| 113 |
# --- Streamlit App UI ---
|
| 114 |
+
st.set_page_config(page_title="Finsmart Bot", layout="centered")
|
| 115 |
+
st.title(" Finsmart Bot - Your Finance Q&A Assistant")
|
| 116 |
|
| 117 |
user_query = st.text_input("Enter your finance-related question:")
|
| 118 |
|