Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,19 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from groq import Groq
|
| 5 |
-
from groq_client import GroqAPI
|
| 6 |
import faiss
|
| 7 |
-
import fitz # PyMuPDF
|
| 8 |
|
| 9 |
# Set up Groq API client
|
| 10 |
groq_client = Groq(api_key="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")
|
| 11 |
|
| 12 |
-
|
| 13 |
# Function to extract text from PDF
|
| 14 |
def extract_pdf_content(pdf_file):
|
| 15 |
-
|
| 16 |
content = ""
|
| 17 |
-
for page in
|
| 18 |
-
content += page.
|
| 19 |
return content
|
| 20 |
|
| 21 |
# Function to split content into chunks
|
|
@@ -24,7 +22,7 @@ def chunk_text(text, chunk_size=500):
|
|
| 24 |
return [" ".join(words[i:i + chunk_size]) for i in range(0, len(words), chunk_size)]
|
| 25 |
|
| 26 |
# Function to compute embeddings using Groq's Llama3-70B-8192 model
|
| 27 |
-
def compute_embeddings(
|
| 28 |
embeddings = []
|
| 29 |
for chunk in text_chunks:
|
| 30 |
response = groq_client.chat.completions.create(
|
|
@@ -47,7 +45,7 @@ def search_faiss_index(index, query_embedding, text_chunks, top_k=3):
|
|
| 47 |
return [(text_chunks[idx], distances[0][i]) for i, idx in enumerate(indices[0])]
|
| 48 |
|
| 49 |
# Function to generate professional content using Groq's Llama3-70B-8192 model
|
| 50 |
-
def generate_professional_content_groq(
|
| 51 |
response = groq_client.chat.completions.create(
|
| 52 |
messages=[{"role": "user", "content": f"Explain '{topic}' in bullet points, highlighting key concepts, examples, and applications for electrical engineering students."}],
|
| 53 |
model="llama3-70b-8192"
|
|
@@ -55,7 +53,7 @@ def generate_professional_content_groq(groq_client, topic):
|
|
| 55 |
return response['choices'][0]['message']['content'].strip()
|
| 56 |
|
| 57 |
# Function to compute query embedding using Groq's Llama3-70B-8192 model
|
| 58 |
-
def compute_query_embedding(
|
| 59 |
response = groq_client.chat.completions.create(
|
| 60 |
messages=[{"role": "user", "content": query}],
|
| 61 |
model="llama3-70b-8192"
|
|
@@ -70,9 +68,6 @@ st.sidebar.header("AI-Based Tutor with Vector Search")
|
|
| 70 |
uploaded_file = st.sidebar.file_uploader("Upload Study Material (PDF)", type=["pdf"])
|
| 71 |
topic = st.sidebar.text_input("Enter a topic (e.g., Newton's Third Law)")
|
| 72 |
|
| 73 |
-
# Initialize Groq client
|
| 74 |
-
groq_client = get_groq_client()
|
| 75 |
-
|
| 76 |
if uploaded_file:
|
| 77 |
# Extract and process file content
|
| 78 |
content = extract_pdf_content(uploaded_file)
|
|
@@ -80,7 +75,7 @@ if uploaded_file:
|
|
| 80 |
|
| 81 |
# Chunk and compute embeddings
|
| 82 |
chunks = chunk_text(content)
|
| 83 |
-
embeddings = compute_embeddings(
|
| 84 |
|
| 85 |
# Build FAISS index
|
| 86 |
index = build_faiss_index(embeddings)
|
|
@@ -94,7 +89,7 @@ if st.button("Generate Study Material"):
|
|
| 94 |
st.header(f"Study Material: {topic}")
|
| 95 |
|
| 96 |
# Compute query embedding
|
| 97 |
-
query_embedding = compute_query_embedding(
|
| 98 |
|
| 99 |
# Search FAISS index
|
| 100 |
if uploaded_file:
|
|
@@ -106,7 +101,7 @@ if st.button("Generate Study Material"):
|
|
| 106 |
st.warning("No file uploaded. Generating AI-based content instead.")
|
| 107 |
|
| 108 |
# Generate content using Groq's Llama3-70B-8192 model
|
| 109 |
-
ai_content = generate_professional_content_groq(
|
| 110 |
st.write("**AI-Generated Content (Groq - Llama3-70B-8192):**")
|
| 111 |
st.write(ai_content)
|
| 112 |
else:
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
+
from PyPDF2 import PdfReader
|
| 4 |
import numpy as np
|
| 5 |
from groq import Groq
|
|
|
|
| 6 |
import faiss
|
|
|
|
| 7 |
|
| 8 |
# Set up Groq API client
|
| 9 |
groq_client = Groq(api_key="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")
|
| 10 |
|
|
|
|
| 11 |
# Function to extract text from PDF
|
| 12 |
def extract_pdf_content(pdf_file):
|
| 13 |
+
reader = PdfReader(pdf_file)
|
| 14 |
content = ""
|
| 15 |
+
for page in reader.pages:
|
| 16 |
+
content += page.extract_text()
|
| 17 |
return content
|
| 18 |
|
| 19 |
# Function to split content into chunks
|
|
|
|
| 22 |
return [" ".join(words[i:i + chunk_size]) for i in range(0, len(words), chunk_size)]
|
| 23 |
|
| 24 |
# Function to compute embeddings using Groq's Llama3-70B-8192 model
|
| 25 |
+
def compute_embeddings(text_chunks):
|
| 26 |
embeddings = []
|
| 27 |
for chunk in text_chunks:
|
| 28 |
response = groq_client.chat.completions.create(
|
|
|
|
| 45 |
return [(text_chunks[idx], distances[0][i]) for i, idx in enumerate(indices[0])]
|
| 46 |
|
| 47 |
# Function to generate professional content using Groq's Llama3-70B-8192 model
|
| 48 |
+
def generate_professional_content_groq(topic):
|
| 49 |
response = groq_client.chat.completions.create(
|
| 50 |
messages=[{"role": "user", "content": f"Explain '{topic}' in bullet points, highlighting key concepts, examples, and applications for electrical engineering students."}],
|
| 51 |
model="llama3-70b-8192"
|
|
|
|
| 53 |
return response['choices'][0]['message']['content'].strip()
|
| 54 |
|
| 55 |
# Function to compute query embedding using Groq's Llama3-70B-8192 model
|
| 56 |
+
def compute_query_embedding(query):
|
| 57 |
response = groq_client.chat.completions.create(
|
| 58 |
messages=[{"role": "user", "content": query}],
|
| 59 |
model="llama3-70b-8192"
|
|
|
|
| 68 |
uploaded_file = st.sidebar.file_uploader("Upload Study Material (PDF)", type=["pdf"])
|
| 69 |
topic = st.sidebar.text_input("Enter a topic (e.g., Newton's Third Law)")
|
| 70 |
|
|
|
|
|
|
|
|
|
|
| 71 |
if uploaded_file:
|
| 72 |
# Extract and process file content
|
| 73 |
content = extract_pdf_content(uploaded_file)
|
|
|
|
| 75 |
|
| 76 |
# Chunk and compute embeddings
|
| 77 |
chunks = chunk_text(content)
|
| 78 |
+
embeddings = compute_embeddings(chunks)
|
| 79 |
|
| 80 |
# Build FAISS index
|
| 81 |
index = build_faiss_index(embeddings)
|
|
|
|
| 89 |
st.header(f"Study Material: {topic}")
|
| 90 |
|
| 91 |
# Compute query embedding
|
| 92 |
+
query_embedding = compute_query_embedding(topic)
|
| 93 |
|
| 94 |
# Search FAISS index
|
| 95 |
if uploaded_file:
|
|
|
|
| 101 |
st.warning("No file uploaded. Generating AI-based content instead.")
|
| 102 |
|
| 103 |
# Generate content using Groq's Llama3-70B-8192 model
|
| 104 |
+
ai_content = generate_professional_content_groq(topic)
|
| 105 |
st.write("**AI-Generated Content (Groq - Llama3-70B-8192):**")
|
| 106 |
st.write(ai_content)
|
| 107 |
else:
|