muhammadrazapathan commited on
Commit
fbe46ad
Β·
verified Β·
1 Parent(s): b91cdf0

Update app.py

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