Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import PyPDF2
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
def extract_text_from_pdf(pdf_file):
|
| 6 |
+
"""
|
| 7 |
+
Extracts text from an uploaded PDF file and returns it as a single string.
|
| 8 |
+
"""
|
| 9 |
+
text = ""
|
| 10 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
| 11 |
+
for page in pdf_reader.pages:
|
| 12 |
+
text += page.extract_text()
|
| 13 |
+
return text
|
| 14 |
+
|
| 15 |
+
def ask_question_from_pdf(pdf_text, user_question, api_key):
|
| 16 |
+
"""
|
| 17 |
+
Uses the Groq model to answer a question based on the provided PDF text.
|
| 18 |
+
"""
|
| 19 |
+
client = Groq(api_key=api_key) # Initialize Groq client with the API key.
|
| 20 |
+
|
| 21 |
+
# Truncate the PDF text if it exceeds the model's input limit.
|
| 22 |
+
max_input_tokens = 2048 # Model input token limit.
|
| 23 |
+
truncated_pdf_text = pdf_text[:max_input_tokens]
|
| 24 |
+
|
| 25 |
+
# Stream the response for the user's question.
|
| 26 |
+
stream = client.chat.completions.create(
|
| 27 |
+
messages=[
|
| 28 |
+
{
|
| 29 |
+
"role": "system",
|
| 30 |
+
"content": "You are a helpful assistant. Use the provided PDF content to answer the user's question."
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"role": "user",
|
| 34 |
+
"content": f"PDF content:\n{truncated_pdf_text}\n\nQuestion: {user_question}"
|
| 35 |
+
}
|
| 36 |
+
],
|
| 37 |
+
model="llama-3.3-70b-versatile",
|
| 38 |
+
temperature=0.5,
|
| 39 |
+
max_completion_tokens=512,
|
| 40 |
+
top_p=1,
|
| 41 |
+
stream=True,
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Collect the streamed response.
|
| 45 |
+
answer = ""
|
| 46 |
+
for chunk in stream:
|
| 47 |
+
answer += chunk.choices[0].delta.content
|
| 48 |
+
return answer
|
| 49 |
+
|
| 50 |
+
# Streamlit app
|
| 51 |
+
st.title("PDF Question Answering with Groq")
|
| 52 |
+
st.write("Upload a PDF file and ask questions about its content.")
|
| 53 |
+
|
| 54 |
+
# Input for Groq API key
|
| 55 |
+
api_key = st.text_input("Enter your Groq API key", type="password")
|
| 56 |
+
|
| 57 |
+
# File uploader
|
| 58 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
|
| 59 |
+
|
| 60 |
+
if uploaded_file is not None:
|
| 61 |
+
pdf_text = extract_text_from_pdf(uploaded_file)
|
| 62 |
+
st.write("PDF content successfully extracted!")
|
| 63 |
+
|
| 64 |
+
# Question input
|
| 65 |
+
user_question = st.text_input("Ask a question about the PDF content")
|
| 66 |
+
|
| 67 |
+
if st.button("Get Answer") and user_question and api_key:
|
| 68 |
+
with st.spinner("Fetching the answer..."):
|
| 69 |
+
answer = ask_question_from_pdf(pdf_text, user_question, api_key)
|
| 70 |
+
st.success("Answer fetched!")
|
| 71 |
+
st.write("**Answer:**", answer)
|
| 72 |
+
elif not api_key:
|
| 73 |
+
st.error("Please provide a valid Groq API key.")
|
| 74 |
+
elif not user_question:
|
| 75 |
+
st.error("Please enter a question.")
|
| 76 |
+
|
| 77 |
+
st.write("Note: The Groq API key is required for the app to function.")
|