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