Spaces:
Sleeping
Sleeping
import pdfplumber | |
import streamlit as st | |
from groq import Groq | |
# Replace with your actual API key | |
API_KEY = "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" | |
# Initialize the Groq client with the API key | |
client = Groq(api_key=API_KEY) | |
# Function to extract text from the PDF | |
def extract_text_from_pdf(pdf_path): | |
with pdfplumber.open(pdf_path) as pdf: | |
full_text = "" | |
for page in pdf.pages: | |
full_text += page.extract_text() | |
return full_text | |
# Function to search for relevant information based on the query | |
def search_relevant_info(query, text): | |
lower_text = text.lower() | |
lower_query = query.lower() | |
if lower_query in lower_text: | |
start = lower_text.find(lower_query) | |
end = start + 1000 # Extracting a portion of the text (adjustable) | |
return text[start:end] | |
else: | |
return "Sorry, I couldn't find any relevant information." | |
# Function to generate response from Groq API | |
def generate_response_with_retrieved_info(query, retrieved_text): | |
chat_completion = client.chat.completions.create( | |
messages=[ | |
{ | |
"role": "user", | |
"content": f"Query: {query}\nContext: {retrieved_text}", | |
} | |
], | |
model="llama3-8b-8192", | |
) | |
return chat_completion.choices[0].message.content | |
# Main chatbot function | |
def chatbot(query, pdf_path): | |
# Step 1: Extract text from the PDF | |
extracted_text = extract_text_from_pdf(pdf_path) | |
# Step 2: Search for relevant information based on the query | |
retrieved_text = search_relevant_info(query, extracted_text) | |
# Step 3: Generate a response using the Groq API | |
response = generate_response_with_retrieved_info(query, retrieved_text) | |
return response | |
# Streamlit UI | |
def main(): | |
st.title("University Information Chatbot") | |
# Upload PDF | |
pdf_file = st.file_uploader("Upload the University Information PDF", type=["pdf"]) | |
if pdf_file: | |
# Text input for user query | |
query = st.text_input("Ask a question about the university:") | |
if query: | |
# Process the query | |
with st.spinner("Searching for relevant information..."): | |
response = chatbot(query, pdf_file) | |
st.write("Response:", response) | |
if __name__ == "__main__": | |
main() | |