shukdevdatta123's picture
Update app.py
291f44e verified
raw
history blame
5.7 kB
import streamlit as st
import PyPDF2
import openai
import faiss
import os
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
# Function to extract text from a PDF file
def extract_text_from_pdf(pdf_file):
reader = PyPDF2.PdfReader(pdf_file)
text = ""
for page in reader.pages:
text += page.extract_text()
return text
# Function to generate embeddings for a piece of text
def get_embeddings(text, model="text-embedding-ada-002"):
response = openai.Embedding.create(input=[text], model=model)
return response['data'][0]['embedding']
# Function to search for similar content
def search_similar(query_embedding, index, stored_texts, top_k=3):
distances, indices = index.search(np.array([query_embedding]), top_k)
results = [(stored_texts[i], distances[0][idx]) for idx, i in enumerate(indices[0])]
return results
# Function to generate HTML with nice styling
def generate_html(response_content):
html_template = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Course Query Response</title>
<style>
body {{
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f9;
color: #333;
}}
.container {{
width: 80%;
margin: 30px auto;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}}
h1 {{
color: #2C3E50;
font-size: 2em;
text-align: center;
}}
.response {{
background-color: #ecf0f1;
border-left: 5px solid #3498db;
padding: 20px;
font-size: 1.2em;
margin-top: 20px;
border-radius: 5px;
}}
footer {{
text-align: center;
margin-top: 30px;
font-size: 0.9em;
color: #7f8c8d;
}}
</style>
</head>
<body>
<div class="container">
<h1>Course Query Response</h1>
<div class="response">
<h3>Answer:</h3>
<p>{response_content}</p>
</div>
<footer>
<p>Generated by Course Query Assistant</p>
</footer>
</div>
</body>
</html>
"""
return html_template
# Streamlit app starts here
st.title("Course Query Assistant")
# Input OpenAI API key
openai_api_key = st.text_input("Enter your OpenAI API key:", type="password")
if openai_api_key:
openai.api_key = openai_api_key
# Upload course materials
uploaded_files = st.file_uploader("Upload Course Materials (PDFs)", type=["pdf"], accept_multiple_files=True)
if uploaded_files:
st.write("Processing uploaded course materials...")
# Extract text and generate embeddings for all uploaded PDFs
course_texts = []
for uploaded_file in uploaded_files:
text = extract_text_from_pdf(uploaded_file)
course_texts.append(text)
# Combine all course materials into one large text
combined_text = " ".join(course_texts)
# Split combined text into smaller chunks for embedding (max tokens ~1000)
chunks = [combined_text[i:i+1000] for i in range(0, len(combined_text), 1000)]
# Generate embeddings for all chunks
embeddings = [get_embeddings(chunk) for chunk in chunks]
# Convert the list of embeddings into a NumPy array (shape: [num_chunks, embedding_size])
embeddings_np = np.array(embeddings).astype("float32")
# Create a FAISS index for similarity search
index = faiss.IndexFlatL2(len(embeddings_np[0])) # Use the length of the embedding vectors for the dimension
index.add(embeddings_np)
st.write("Course materials have been processed and indexed.")
# User query
query = st.text_input("Enter your question about the course materials:")
if query:
# Generate embedding for the query
query_embedding = get_embeddings(query)
# Search for similar chunks in the FAISS index
results = search_similar(query_embedding, index, chunks)
# Create the context for the GPT prompt
context = "\n".join([result[0] for result in results])
modified_prompt = f"Context: {context}\n\nQuestion: {query}\n\nProvide a detailed answer based on the context."
# Get the GPT-3.5-turbo response
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": modified_prompt}]
)
# Get the response content
response_content = response['choices'][0]['message']['content']
# Display the response in Streamlit
st.write("### Intelligent Reply:")
st.write(response_content)
# Generate HTML content
html_content = generate_html(response_content)
# Provide the download button for the HTML file
st.download_button(
label="Download Response as HTML",
data=html_content,
file_name="course_query_response.html",
mime="text/html"
)