Spaces:
Runtime error
Runtime error
import streamlit as st | |
from transformers import pipeline | |
import PyPDF2 | |
import requests | |
# Constants for Groq API | |
GROQ_API_URL = "https://api.groq.com/your_endpoint" # Replace with your Groq endpoint | |
GROQ_SECRET_KEY = "your_secret_key" # Replace with your secret key | |
HUGGINGFACE_MODEL = "deepset/bert-base-cased-squad2" # Choose your model | |
# Load Hugging Face model for question-answering | |
qa_pipeline = pipeline("question-answering", model=HUGGINGFACE_MODEL) | |
# Function to extract text from PDF | |
def extract_text_from_pdf(pdf_file): | |
reader = PyPDF2.PdfReader(pdf_file) | |
text = '' | |
for page in reader.pages: | |
text += page.extract_text() + '\n' | |
return text | |
# Function to run inference using Groq | |
def groq_inference(question, context): | |
headers = { | |
"Authorization": f"Bearer {GROQ_SECRET_KEY}", | |
"Content-Type": "application/json" | |
} | |
payload = { | |
"question": question, | |
"context": context | |
} | |
response = requests.post(GROQ_API_URL, headers=headers, json=payload) | |
if response.status_code == 200: | |
return response.json()['answer'] | |
else: | |
return "Error: Unable to get response from Groq." | |
# Streamlit UI | |
st.title("Document Chatbot") | |
st.write("Upload a PDF document to interact with it!") | |
# File uploader | |
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf") | |
if uploaded_file: | |
# Extract text from the uploaded PDF | |
document_text = extract_text_from_pdf(uploaded_file) | |
st.write("Document successfully uploaded and processed.") | |
# Chat interface | |
user_question = st.text_input("Ask a question about the document:") | |
if user_question: | |
# First try to get the answer from Hugging Face model | |
hf_answer = qa_pipeline(question=user_question, context=document_text)['answer'] | |
st.write("Answer from Hugging Face Model:", hf_answer) | |
# Then try to get the answer from Groq | |
groq_answer = groq_inference(user_question, document_text) | |
st.write("Answer from Groq:", groq_answer) | |