amasood commited on
Commit
d26e6b0
·
verified ·
1 Parent(s): 5fe7f9b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import PyPDF2
3
+ from groq import Groq
4
+ import os
5
+
6
+ # Set up Groq API key
7
+ os.environ["GROQ_API_KEY"] = "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"
8
+
9
+ # Initialize Groq client
10
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
11
+
12
+ # Function to extract PDF content
13
+ def extract_pdf_content(pdf_file):
14
+ pdf_text = ""
15
+ reader = PyPDF2.PdfReader(pdf_file)
16
+ for page in reader.pages:
17
+ pdf_text += page.extract_text()
18
+ return pdf_text
19
+
20
+ # Function to chunk text
21
+ def chunk_text(text, chunk_size=1000, overlap=200):
22
+ chunks = []
23
+ start = 0
24
+ while start < len(text):
25
+ end = start + chunk_size
26
+ chunk = text[start:end]
27
+ chunks.append(chunk)
28
+ start += chunk_size - overlap
29
+ return chunks
30
+
31
+ # Function to find relevant chunks
32
+ def find_relevant_chunks(chunks, query, num_chunks=3):
33
+ return chunks[:num_chunks] # Simple retrieval
34
+
35
+ # Chatbot function
36
+ def chatbot_response(user_query, chunks):
37
+ relevant_chunks = find_relevant_chunks(chunks, user_query)
38
+ combined_context = "\n\n".join(relevant_chunks)
39
+ context = f"PDF Content:\n{combined_context}\n\nUser Query: {user_query}"
40
+ chat_completion = client.chat.completions.create(
41
+ messages=[{"role": "user", "content": context}],
42
+ model="llama-3.3-70b-versatile",
43
+ )
44
+ return chat_completion.choices[0].message.content
45
+
46
+ # Streamlit UI
47
+ st.title("PDF Query Chatbot")
48
+ st.write("Upload a PDF and