Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gdown
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 6 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 7 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 8 |
+
from langchain_community.vectorstores import FAISS
|
| 9 |
+
|
| 10 |
+
# --- CONFIGURATION ---
|
| 11 |
+
FILE_ID = "1jmHwcjA-gLIUTIKkXfQ36ucJI_VPdULS"
|
| 12 |
+
PDF_PATH = "finance_data.pdf"
|
| 13 |
+
GDRIVE_URL = f'https://drive.google.com/uc?id={FILE_ID}'
|
| 14 |
+
|
| 15 |
+
# Use environment variable for security on Hugging Face
|
| 16 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 17 |
+
|
| 18 |
+
# --- INITIALIZATION ---
|
| 19 |
+
def initialize_system():
|
| 20 |
+
if not os.path.exists(PDF_PATH):
|
| 21 |
+
gdown.download(GDRIVE_URL, PDF_PATH, quiet=False)
|
| 22 |
+
|
| 23 |
+
loader = PyPDFLoader(PDF_PATH)
|
| 24 |
+
docs = loader.load()
|
| 25 |
+
|
| 26 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
|
| 27 |
+
chunks = splitter.split_documents(docs)
|
| 28 |
+
|
| 29 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 30 |
+
return FAISS.from_documents(chunks, embeddings)
|
| 31 |
+
|
| 32 |
+
# Global Vector DB
|
| 33 |
+
vector_db = initialize_system()
|
| 34 |
+
|
| 35 |
+
def process_query(user_query):
|
| 36 |
+
if not GROQ_API_KEY:
|
| 37 |
+
return "Error: GROQ_API_KEY not found in Environment Variables.", ""
|
| 38 |
+
|
| 39 |
+
if not user_query.strip():
|
| 40 |
+
return "Please enter a valid question.", ""
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
# Retrieval
|
| 44 |
+
search_results = vector_db.similarity_search(user_query, k=3)
|
| 45 |
+
context = "\n\n".join([res.page_content for res in search_results])
|
| 46 |
+
|
| 47 |
+
# Groq Client
|
| 48 |
+
client = OpenAI(api_key=GROQ_API_KEY, base_url="https://api.groq.com/openai/v1")
|
| 49 |
+
|
| 50 |
+
# Generation
|
| 51 |
+
prompt = f"Context: {context}\n\nQuestion: {user_query}\n\nAnswer using context only:"
|
| 52 |
+
response = client.responses.create(model="openai/gpt-oss-20b", input=prompt)
|
| 53 |
+
|
| 54 |
+
return response.output_text, context
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"System Error: {str(e)}", ""
|
| 57 |
+
|
| 58 |
+
# --- CUSTOM PINK THEME ---
|
| 59 |
+
pink_theme = gr.themes.Soft(
|
| 60 |
+
primary_hue="pink",
|
| 61 |
+
secondary_hue="rose",
|
| 62 |
+
neutral_hue="slate",
|
| 63 |
+
font=[gr.themes.GoogleFont("Poppins"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 64 |
+
).set(
|
| 65 |
+
button_primary_background_fill="*primary_500",
|
| 66 |
+
button_primary_background_fill_hover="*primary_600",
|
| 67 |
+
button_primary_text_color="white",
|
| 68 |
+
body_background_fill="*neutral_50",
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# --- GRADIO UI ---
|
| 72 |
+
with gr.Blocks(theme=pink_theme, title="Muhammad Bilal Finance RAG") as demo:
|
| 73 |
+
gr.Markdown(
|
| 74 |
+
"""
|
| 75 |
+
# <span style='color:#e91e63;'>๐ Muhammad Bilal Finance RAG App</span>
|
| 76 |
+
### Analyzing financial documents with precision and style.
|
| 77 |
+
"""
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
with gr.Row():
|
| 81 |
+
with gr.Column(scale=2):
|
| 82 |
+
query_input = gr.Textbox(
|
| 83 |
+
label="Your Question",
|
| 84 |
+
placeholder="Ask about revenue, assets, or liabilities...",
|
| 85 |
+
lines=2
|
| 86 |
+
)
|
| 87 |
+
analyze_btn = gr.Button("โจ Analyze Document", variant="primary")
|
| 88 |
+
|
| 89 |
+
with gr.Column(scale=3):
|
| 90 |
+
answer_output = gr.Textbox(
|
| 91 |
+
label="AI Financial Insights",
|
| 92 |
+
placeholder="Analysis will appear here...",
|
| 93 |
+
lines=10,
|
| 94 |
+
interactive=False
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
with gr.Accordion("๐ View Retrieved Source Data", open=False):
|
| 98 |
+
context_output = gr.Markdown()
|
| 99 |
+
|
| 100 |
+
analyze_btn.click(
|
| 101 |
+
fn=process_query,
|
| 102 |
+
inputs=[query_input],
|
| 103 |
+
outputs=[answer_output, context_output]
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
gr.Markdown(
|
| 107 |
+
"""
|
| 108 |
+
<center>Developed by **Muhammad Bilal** | Powered by FAISS & Groq</center>
|
| 109 |
+
"""
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
+
demo.launch()
|