File size: 2,034 Bytes
898a5de
16f307d
 
 
c8113fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)