udhayakumarj85 commited on
Commit
cd248ef
·
verified ·
1 Parent(s): 2171b68

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +145 -0
app.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+
4
+ from langchain_community.document_loaders import PyPDFLoader
5
+
6
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
7
+
8
+ from langchain_community.embeddings import HuggingFaceEmbeddings
9
+
10
+ from langchain_community.vectorstores import FAISS
11
+
12
+ from groq import Groq
13
+
14
+
15
+ # Read API key
16
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
17
+
18
+ client = Groq(api_key=GROQ_API_KEY)
19
+
20
+ # Global vector database
21
+ vectorstore = None
22
+
23
+
24
+ def process_pdf(pdf_file):
25
+
26
+ global vectorstore
27
+
28
+ if pdf_file is None:
29
+ return "Please upload a PDF."
30
+
31
+ # Load PDF
32
+ loader = PyPDFLoader(pdf_file.name)
33
+
34
+ documents = loader.load()
35
+
36
+ # Split text
37
+ splitter = RecursiveCharacterTextSplitter(
38
+ chunk_size=1000,
39
+ chunk_overlap=200
40
+ )
41
+
42
+ docs = splitter.split_documents(documents)
43
+
44
+ # Embeddings
45
+ embeddings = HuggingFaceEmbeddings(
46
+ model_name="sentence-transformers/all-MiniLM-L6-v2"
47
+ )
48
+
49
+ # Create vector DB
50
+ vectorstore = FAISS.from_documents(
51
+ docs,
52
+ embeddings
53
+ )
54
+
55
+ return "PDF processed successfully."
56
+
57
+
58
+ def ask_question(question):
59
+
60
+ global vectorstore
61
+
62
+ if vectorstore is None:
63
+ return "Please process a PDF first."
64
+
65
+ # Retrieve relevant chunks
66
+ docs = vectorstore.similarity_search(
67
+ question,
68
+ k=3
69
+ )
70
+
71
+ context = "\n\n".join(
72
+ [doc.page_content for doc in docs]
73
+ )
74
+
75
+ prompt = f"""
76
+ Answer the question only using the context below.
77
+
78
+ Context:
79
+ {context}
80
+
81
+ Question:
82
+ {question}
83
+
84
+ Answer:
85
+ """
86
+
87
+ # Groq response
88
+ response = client.chat.completions.create(
89
+
90
+ model="llama-3.1-8b-instant",
91
+
92
+ messages=[
93
+ {
94
+ "role": "user",
95
+ "content": prompt
96
+ }
97
+ ],
98
+
99
+ temperature=0.2,
100
+ max_tokens=400
101
+ )
102
+
103
+ return response.choices[0].message.content
104
+
105
+
106
+ with gr.Blocks() as demo:
107
+
108
+ gr.Markdown("# PDF Q&A Chatbot")
109
+
110
+ pdf_input = gr.File(
111
+ label="Upload PDF",
112
+ file_types=[".pdf"]
113
+ )
114
+
115
+ process_button = gr.Button("Process PDF")
116
+
117
+ status_output = gr.Textbox(
118
+ label="Status"
119
+ )
120
+
121
+ process_button.click(
122
+ fn=process_pdf,
123
+ inputs=pdf_input,
124
+ outputs=status_output
125
+ )
126
+
127
+ question_input = gr.Textbox(
128
+ label="Ask Question",
129
+ placeholder="Ask anything from the PDF..."
130
+ )
131
+
132
+ ask_button = gr.Button("Get Answer")
133
+
134
+ answer_output = gr.Textbox(
135
+ lines=10,
136
+ label="Answer"
137
+ )
138
+
139
+ ask_button.click(
140
+ fn=ask_question,
141
+ inputs=question_input,
142
+ outputs=answer_output
143
+ )
144
+
145
+ demo.launch()