Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from io import BytesIO
|
|
| 5 |
from PyPDF2 import PdfReader
|
| 6 |
import docx
|
| 7 |
from textwrap import wrap
|
|
|
|
| 8 |
|
| 9 |
# Initialize Groq client
|
| 10 |
client = Groq(api_key=os.environ.get("Groq_Api_Key"))
|
|
@@ -27,11 +28,9 @@ def extract_text_from_word(doc_file):
|
|
| 27 |
def chunk_text(text, max_tokens=2000):
|
| 28 |
"""
|
| 29 |
Chunks text into smaller pieces that fit within the model's token limit.
|
| 30 |
-
|
| 31 |
Args:
|
| 32 |
text (str): The input text to be chunked.
|
| 33 |
max_tokens (int): The maximum number of tokens per chunk (default is 2000).
|
| 34 |
-
|
| 35 |
Returns:
|
| 36 |
list: A list of text chunks.
|
| 37 |
"""
|
|
@@ -67,11 +66,9 @@ def ask_question(text, question):
|
|
| 67 |
def generate_mcqs(text, num_questions):
|
| 68 |
"""
|
| 69 |
Generate multiple-choice questions from the given text.
|
| 70 |
-
|
| 71 |
Args:
|
| 72 |
text (str): The input text for generating MCQs.
|
| 73 |
num_questions (int): The number of MCQs to generate.
|
| 74 |
-
|
| 75 |
Returns:
|
| 76 |
list: A list of MCQs, or an empty list if parsing fails.
|
| 77 |
"""
|
|
@@ -84,14 +81,17 @@ def generate_mcqs(text, num_questions):
|
|
| 84 |
# Attempt to parse the response as JSON
|
| 85 |
try:
|
| 86 |
# The model should return a JSON-formatted string
|
| 87 |
-
mcqs =
|
| 88 |
if isinstance(mcqs, list):
|
| 89 |
return mcqs
|
| 90 |
else:
|
| 91 |
st.error("The generated MCQs are not in the expected format.")
|
| 92 |
return []
|
|
|
|
|
|
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
-
st.error(f"
|
| 95 |
return []
|
| 96 |
|
| 97 |
def adapt_lesson_for_grade(text, grade):
|
|
|
|
| 5 |
from PyPDF2 import PdfReader
|
| 6 |
import docx
|
| 7 |
from textwrap import wrap
|
| 8 |
+
import json
|
| 9 |
|
| 10 |
# Initialize Groq client
|
| 11 |
client = Groq(api_key=os.environ.get("Groq_Api_Key"))
|
|
|
|
| 28 |
def chunk_text(text, max_tokens=2000):
|
| 29 |
"""
|
| 30 |
Chunks text into smaller pieces that fit within the model's token limit.
|
|
|
|
| 31 |
Args:
|
| 32 |
text (str): The input text to be chunked.
|
| 33 |
max_tokens (int): The maximum number of tokens per chunk (default is 2000).
|
|
|
|
| 34 |
Returns:
|
| 35 |
list: A list of text chunks.
|
| 36 |
"""
|
|
|
|
| 66 |
def generate_mcqs(text, num_questions):
|
| 67 |
"""
|
| 68 |
Generate multiple-choice questions from the given text.
|
|
|
|
| 69 |
Args:
|
| 70 |
text (str): The input text for generating MCQs.
|
| 71 |
num_questions (int): The number of MCQs to generate.
|
|
|
|
| 72 |
Returns:
|
| 73 |
list: A list of MCQs, or an empty list if parsing fails.
|
| 74 |
"""
|
|
|
|
| 81 |
# Attempt to parse the response as JSON
|
| 82 |
try:
|
| 83 |
# The model should return a JSON-formatted string
|
| 84 |
+
mcqs = json.loads(response.choices[0].message.content)
|
| 85 |
if isinstance(mcqs, list):
|
| 86 |
return mcqs
|
| 87 |
else:
|
| 88 |
st.error("The generated MCQs are not in the expected format.")
|
| 89 |
return []
|
| 90 |
+
except json.JSONDecodeError as e:
|
| 91 |
+
st.error(f"Error decoding MCQs: {e}")
|
| 92 |
+
return []
|
| 93 |
except Exception as e:
|
| 94 |
+
st.error(f"Unexpected error: {e}")
|
| 95 |
return []
|
| 96 |
|
| 97 |
def adapt_lesson_for_grade(text, grade):
|