muhammadrazapathan commited on
Commit
406a95b
·
verified ·
1 Parent(s): f87c112

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -0
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import tempfile
4
+ from dotenv import load_dotenv
5
+
6
+ from langchain_community.document_loaders import PyPDFLoader
7
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
8
+ from langchain_huggingface import HuggingFaceEmbeddings
9
+ from langchain_community.vectorstores import FAISS
10
+
11
+ from groq import Groq
12
+
13
+ # ================= ENVIRONMENT =================
14
+ load_dotenv()
15
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Use HF secret
16
+
17
+ client = None
18
+ if GROQ_API_KEY:
19
+ client = Groq(api_key=GROQ_API_KEY)
20
+
21
+ vector_db = None
22
+
23
+ # ================= LLM FUNCTION =================
24
+ def groq_llm(prompt):
25
+ if client is None:
26
+ return "❌ GROQ API key not set. Set it in Hugging Face Secrets."
27
+
28
+ response = client.chat.completions.create(
29
+ model="llama-3.3-70b-versatile",
30
+ messages=[{"role": "user", "content": prompt}],
31
+ )
32
+ return response.choices[0].message.content
33
+
34
+ # ================= PROCESS PDF =================
35
+ def process_pdf(file):
36
+ global vector_db
37
+
38
+ if file is None:
39
+ return "❌ Please upload a PDF."
40
+
41
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
42
+ tmp.write(file)
43
+ pdf_path = tmp.name
44
+
45
+ loader = PyPDFLoader(pdf_path)
46
+ documents = loader.load()
47
+
48
+ splitter = RecursiveCharacterTextSplitter(
49
+ chunk_size=500,
50
+ chunk_overlap=100
51
+ )
52
+ docs = splitter.split_documents(documents)
53
+
54
+ embeddings = HuggingFaceEmbeddings(
55
+ model_name="sentence-transformers/all-MiniLM-L6-v2"
56
+ )
57
+
58
+ vector_db = FAISS.from_documents(docs, embeddings)
59
+
60
+ return f"✅ PDF processed successfully! {len(docs)} chunks created."
61
+
62
+ # ================= ASK QUESTION =================
63
+ def ask_question(question, chat_history):
64
+ global vector_db
65
+
66
+ if vector_db is None:
67
+ return chat_history + [["System", "❌ Please upload and process a PDF first."]]
68
+
69
+ retriever = vector_db.as_retriever(search_kwargs={"k": 3})
70
+ docs = retriever.get_relevant_documents(question)
71
+ context = "\n\n".join([doc.page_content for doc in docs])
72
+
73
+ prompt = f"""
74
+ You are a smart assistant.
75
+ Answer ONLY using the provided context.
76
+
77
+ Context:
78
+ {context}
79
+
80
+ Question:
81
+ {question}
82
+
83
+ Answer:
84
+ """
85
+
86
+ answer = groq_llm(prompt)
87
+ chat_history.append(["User", question])
88
+ chat_history.append(["Assistant", answer])
89
+ return chat_history, chat_history
90
+
91
+ # ================= GRADIO UI =================
92
+ with gr.Blocks(css="""
93
+ body {background-color: #f5f5f5;}
94
+ .gradio-container {max-width: 900px; margin:auto; padding:20px; border-radius:12px; box-shadow: 0 4px 15px rgba(0,0,0,0.1);}
95
+ .chat-message {border-radius:12px; padding:10px; margin:5px 0;}
96
+ .user {background-color:#4f46e5; color:white;}
97
+ .assistant {background-color:#e0e7ff; color:black;}
98
+ """) as demo:
99
+
100
+ gr.Markdown("<h1 style='text-align:center; color:#4f46e5;'>📄 RAG PDF Question Answering</h1>", elem_id="title")
101
+ gr.Markdown("<p style='text-align:center; color:#333;'>Upload a PDF and chat with it like a smart assistant!</p>")
102
+
103
+ if client is None:
104
+ gr.Markdown("⚠️ GROQ_API_KEY not set. Set it in Hugging Face Secrets to enable answering.")
105
+
106
+ with gr.Row():
107
+ with gr.Column(scale=1):
108
+ pdf_upload = gr.File(label="Upload PDF", file_types=[".pdf"])
109
+ process_btn = gr.Button("Process PDF", elem_id="process_btn")
110
+ status = gr.Textbox(label="Status", interactive=False)
111
+ with gr.Column(scale=2):
112
+ chatbot = gr.Chatbot(label="Chat with PDF")
113
+ question = gr.Textbox(placeholder="Type your question here and press Enter")
114
+
115
+ process_btn.click(process_pdf, inputs=pdf_upload, outputs=status)
116
+ question.submit(ask_question, inputs=[question, chatbot], outputs=[chatbot, chatbot])
117
+
118
+ demo.launch()