File size: 4,028 Bytes
00e9f89
 
 
 
 
 
 
 
 
 
 
 
 
 
34426b9
00e9f89
 
 
 
34426b9
00e9f89
34426b9
 
 
 
00e9f89
34426b9
00e9f89
 
 
 
 
 
34426b9
00e9f89
 
 
34426b9
00e9f89
 
 
 
34426b9
00e9f89
 
34426b9
00e9f89
 
34426b9
00e9f89
 
 
34426b9
00e9f89
 
34426b9
00e9f89
 
34426b9
 
 
 
 
 
 
00e9f89
 
 
34426b9
 
00e9f89
 
 
 
34426b9
00e9f89
 
 
 
 
 
34426b9
00e9f89
 
 
 
34426b9
00e9f89
 
 
 
 
 
34426b9
 
00e9f89
 
34426b9
00e9f89
 
 
34426b9
00e9f89
 
 
 
34426b9
00e9f89
 
 
 
 
 
 
 
34426b9
00e9f89
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
import gdown
import gradio as gr
from openai import OpenAI
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS

# --- CONFIGURATION ---
FILE_ID = "1jmHwcjA-gLIUTIKkXfQ36ucJI_VPdULS"
PDF_PATH = "finance_data.pdf"
GDRIVE_URL = f'https://drive.google.com/uc?id={FILE_ID}'

# Hugging Face Space will pull this from your "Secrets" settings
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")

# --- INITIALIZATION ---
def initialize_system():
    # Download PDF if not present
    if not os.path.exists(PDF_PATH):
        try:
            gdown.download(GDRIVE_URL, PDF_PATH, quiet=False)
        except Exception as e:
            print(f"Download Error: {e}")
    
    # Load and Split
    loader = PyPDFLoader(PDF_PATH)
    docs = loader.load()
    
    splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
    chunks = splitter.split_documents(docs)
    
    # Embeddings using Sentence Transformers
    embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
    return FAISS.from_documents(chunks, embeddings)

# Initialize the vector database once at startup
vector_db = initialize_system()

def process_query(user_query):
    if not GROQ_API_KEY:
        return "⚠️ Error: GROQ_API_KEY is missing. Add it to Space Secrets.", ""
    
    if not user_query.strip():
        return "Please enter a question.", ""

    try:
        # Search the PDF
        search_results = vector_db.similarity_search(user_query, k=3)
        context = "\n\n".join([res.page_content for res in search_results])
        
        # Groq API Call
        client = OpenAI(api_key=GROQ_API_KEY, base_url="https://api.groq.com/openai/v1")
        
        # Construct the prompt
        prompt = f"Context: {context}\n\nQuestion: {user_query}\n\nAnswer using context only:"
        
        # Note: Using standard chat completion for maximum compatibility
        chat_completion = client.chat.completions.create(
            messages=[{"role": "user", "content": prompt}],
            model="llama-3.3-70b-versatile", # Reliable high-speed Groq model
        )
        
        return chat_completion.choices[0].message.content, context
    except Exception as e:
        return f"System Error: {str(e)}", ""

# --- STYLISH PINK THEME ---
# Using Poppins font and Pink/Rose color palette
pink_theme = gr.themes.Soft(
    primary_hue="pink",
    secondary_hue="rose",
    neutral_hue="slate",
    font=[gr.themes.GoogleFont("Poppins"), "sans-serif"],
).set(
    button_primary_background_fill="*primary_500",
    button_primary_background_fill_hover="*primary_600",
    button_primary_text_color="white",
)

# --- UI LAYOUT ---
with gr.Blocks(theme=pink_theme, title="Muhammad Bilal Finance RAG") as demo:
    gr.Markdown(
        """
        # <span style='color:#e91e63;'>📊 Muhammad Bilal Finance RAG App</span>
        ### AI-Powered Financial Document Analysis
        """
    )
    
    with gr.Row():
        with gr.Column(scale=2):
            query_input = gr.Textbox(
                label="Ask a Question", 
                placeholder="e.g., What are the total assets?",
                lines=2
            )
            analyze_btn = gr.Button("✨ Run Financial Analysis", variant="primary")
        
        with gr.Column(scale=3):
            answer_output = gr.Textbox(
                label="Muhammad Bilal AI Insights", 
                lines=10,
                interactive=False
            )
            
    with gr.Accordion("🔍 View Context Sources", open=False):
        context_output = gr.Markdown()

    analyze_btn.click(
        fn=process_query, 
        inputs=[query_input], 
        outputs=[answer_output, context_output]
    )

    gr.Markdown("<center>Built by Muhammad Bilal | Powered by Groq & FAISS</center>")

if __name__ == "__main__":
    demo.launch()