Spaces:
Sleeping
Sleeping
File size: 2,732 Bytes
22a9f08 74eb3c9 22a9f08 74eb3c9 22a9f08 |
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 |
import streamlit as st
import PyPDF2
from groq import Groq
def extract_text_from_pdf(pdf_file):
"""
Extracts text from an uploaded PDF file and returns it as a single string.
"""
text = ""
pdf_reader = PyPDF2.PdfReader(pdf_file)
for page in pdf_reader.pages:
text += page.extract_text()
return text
def ask_question_from_pdf(pdf_text, user_question, api_key):
"""
Uses the Groq model to answer a question based on the provided PDF text.
"""
client = Groq(api_key=api_key) # Initialize Groq client with the API key.
# Truncate the PDF text if it exceeds the model's input limit.
max_input_tokens = 2048 # Model input token limit.
truncated_pdf_text = pdf_text[:max_input_tokens]
# Stream the response for the user's question.
stream = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a helpful assistant. Use the provided PDF content to answer the user's question."
},
{
"role": "user",
"content": f"PDF content:\n{truncated_pdf_text}\n\nQuestion: {user_question}"
}
],
model="llama-3.3-70b-versatile",
temperature=0.5,
max_completion_tokens=512,
top_p=1,
stream=True,
)
# Collect the streamed response.
answer = ""
for chunk in stream:
if chunk.choices[0].delta.content: # Ensure content is not None
answer += chunk.choices[0].delta.content
return answer
# Streamlit app
st.title("PDF Question Answering with Groq")
st.write("Upload a PDF file and ask questions about its content.")
# Input for Groq API key
api_key = st.text_input("Enter your Groq API key", type="password")
# File uploader
uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
if uploaded_file is not None:
pdf_text = extract_text_from_pdf(uploaded_file)
st.write("PDF content successfully extracted!")
# Question input
user_question = st.text_input("Ask a question about the PDF content")
if st.button("Get Answer") and user_question and api_key:
with st.spinner("Fetching the answer..."):
try:
answer = ask_question_from_pdf(pdf_text, user_question, api_key)
st.success("Answer fetched!")
st.write("**Answer:**", answer)
except Exception as e:
st.error(f"An error occurred: {e}")
elif not api_key:
st.error("Please provide a valid Groq API key.")
elif not user_question:
st.error("Please enter a question.")
st.write("Note: The Groq API key is required for the app to function.")
|