Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,79 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
import logging
|
| 4 |
import os
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
from langchain_community.vectorstores import FAISS
|
| 8 |
-
from langchain_community.embeddings import OpenAIEmbeddings
|
| 9 |
-
from langchain_community.chat_models import ChatOpenAI
|
| 10 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 11 |
-
from streamlit.runtime.scriptrunner import add_script_run_ctx, get_script_run_ctx
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
logging.getLogger("streamlit").setLevel(logging.ERROR)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
add_script_run_ctx(ctx)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Configure Streamlit app
|
| 24 |
-
st.set_page_config(page_title="📚 Curriculum Assistant", layout="wide")
|
| 25 |
-
st.title("📚 Curriculum Assistant for Teachers")
|
| 26 |
-
|
| 27 |
-
def process_document(file):
|
| 28 |
-
"""Extracts text from syllabus file"""
|
| 29 |
text = ""
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
text += page.extract_text() + "\n"
|
| 33 |
return text
|
| 34 |
|
| 35 |
-
def
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
""
|
| 44 |
-
model = ChatOpenAI(model="gpt-4", temperature=0.7)
|
| 45 |
-
response = model.predict(f"Using the following syllabus context:\n{context}\nGenerate: {prompt}")
|
| 46 |
-
return response
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
if uploaded_file:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
if "messages" not in st.session_state:
|
| 58 |
-
st.session_state["messages"] = []
|
| 59 |
|
| 60 |
-
|
| 61 |
-
if user_input:
|
| 62 |
-
|
| 63 |
-
st.session_state["messages"].append(("User", user_input))
|
| 64 |
-
st.session_state["messages"].append(("Assistant", response))
|
| 65 |
|
| 66 |
-
for
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
def check_multithreading():
|
| 71 |
-
"""Ensures Streamlit functions run in the main thread"""
|
| 72 |
-
if threading.current_thread().name != "MainThread":
|
| 73 |
-
st.warning("⚠️ Streamlit functions should run in the main thread to avoid execution issues.")
|
| 74 |
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import tempfile
|
|
|
|
| 3 |
import os
|
| 4 |
+
import docx
|
| 5 |
+
import PyPDF2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
import openai # Or use HuggingFace's transformers/pipelines for open-source models
|
|
|
|
| 8 |
|
| 9 |
+
# Set your OpenAI API key (or replace with Hugging Face model and pipeline)
|
| 10 |
+
OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
|
|
|
|
| 11 |
|
| 12 |
+
def extract_text_from_pdf(file):
|
| 13 |
+
reader = PyPDF2.PdfReader(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
text = ""
|
| 15 |
+
for page in reader.pages:
|
| 16 |
+
text += page.extract_text() + "\n"
|
|
|
|
| 17 |
return text
|
| 18 |
|
| 19 |
+
def extract_text_from_docx(file):
|
| 20 |
+
doc = docx.Document(file)
|
| 21 |
+
text = "\n".join([para.text for para in doc.paragraphs])
|
| 22 |
+
return text
|
| 23 |
+
|
| 24 |
+
def generate_questions(text, instruction):
|
| 25 |
+
# You can replace this with a HuggingFace pipeline (e.g., text-generation with Llama3)
|
| 26 |
+
openai.api_key = OPENAI_API_KEY
|
| 27 |
+
prompt = (
|
| 28 |
+
f"Given the following content:\n\n{text}\n\n"
|
| 29 |
+
f"{instruction}\n"
|
| 30 |
+
"Provide the output in a clear, numbered list."
|
| 31 |
+
)
|
| 32 |
+
response = openai.ChatCompletion.create(
|
| 33 |
+
model="gpt-3.5-turbo", # or "gpt-4" if available
|
| 34 |
+
messages=[{"role": "system", "content": "You are a helpful assistant."},
|
| 35 |
+
{"role": "user", "content": prompt}],
|
| 36 |
+
max_tokens=800,
|
| 37 |
+
temperature=0.7,
|
| 38 |
+
)
|
| 39 |
+
return response.choices[0].message.content.strip()
|
| 40 |
+
|
| 41 |
+
st.title("Quiz & MCQ Generator Chatbot 🤖")
|
| 42 |
+
st.write("Upload a PDF or DOCX, then ask me to generate MCQs, questions, or a quiz!")
|
| 43 |
|
| 44 |
+
if "chat_history" not in st.session_state:
|
| 45 |
+
st.session_state["chat_history"] = []
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
uploaded_file = st.file_uploader("Upload PDF or DOCX", type=["pdf", "docx"])
|
| 48 |
+
file_text = ""
|
| 49 |
|
| 50 |
if uploaded_file:
|
| 51 |
+
if uploaded_file.type == "application/pdf":
|
| 52 |
+
file_text = extract_text_from_pdf(uploaded_file)
|
| 53 |
+
elif uploaded_file.type in ["application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword"]:
|
| 54 |
+
file_text = extract_text_from_docx(uploaded_file)
|
| 55 |
+
st.success("File uploaded and extracted!")
|
| 56 |
|
| 57 |
+
user_input = st.text_input("You:", key="chat_input")
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
if st.button("Send"):
|
| 60 |
+
if user_input:
|
| 61 |
+
st.session_state.chat_history.append(("user", user_input))
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# If user asks for MCQs, questions, quiz, etc., use document context
|
| 64 |
+
if any(keyword in user_input.lower() for keyword in ["mcq", "quiz", "question", "test"]):
|
| 65 |
+
if file_text:
|
| 66 |
+
answer = generate_questions(file_text, user_input)
|
| 67 |
+
else:
|
| 68 |
+
answer = "Please upload a PDF or DOCX file first!"
|
| 69 |
+
else:
|
| 70 |
+
# Generic chat (can also use LLM here)
|
| 71 |
+
answer = "Please ask me to generate MCQs, questions, or a quiz from your uploaded file!"
|
| 72 |
|
| 73 |
+
st.session_state.chat_history.append(("bot", answer))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
for sender, message in st.session_state.chat_history:
|
| 76 |
+
if sender == "user":
|
| 77 |
+
st.markdown(f"**You:** {message}")
|
| 78 |
+
else:
|
| 79 |
+
st.markdown(f"**Bot:** {message}")
|