Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import fitz # PyMuPDF
|
| 3 |
-
from transformers import
|
| 4 |
|
| 5 |
# Function to extract text from PDF
|
| 6 |
def extract_text_from_pdf(pdf_file):
|
|
@@ -13,11 +13,18 @@ def extract_text_from_pdf(pdf_file):
|
|
| 13 |
|
| 14 |
# Function to generate MCQs using Hugging Face model
|
| 15 |
def generate_mcqs(text, num_questions=5):
|
| 16 |
-
#
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
return mcqs
|
| 23 |
|
|
@@ -37,4 +44,4 @@ if uploaded_file is not None:
|
|
| 37 |
|
| 38 |
st.write("Generated MCQs:")
|
| 39 |
for idx, mcq in enumerate(mcqs):
|
| 40 |
-
st.write(f"{idx+1}. {mcq
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import fitz # PyMuPDF
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
|
| 5 |
# Function to extract text from PDF
|
| 6 |
def extract_text_from_pdf(pdf_file):
|
|
|
|
| 13 |
|
| 14 |
# Function to generate MCQs using Hugging Face model
|
| 15 |
def generate_mcqs(text, num_questions=5):
|
| 16 |
+
# Load HuggingFace model
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2-large")
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2-large")
|
| 19 |
|
| 20 |
+
# Tokenize the input text
|
| 21 |
+
inputs = tokenizer(text, return_tensors="pt", max_length=1024, truncation=True)
|
| 22 |
+
|
| 23 |
+
# Generate MCQs by providing the text as input to the model
|
| 24 |
+
outputs = model.generate(**inputs, max_length=100, num_return_sequences=num_questions, temperature=0.7)
|
| 25 |
+
|
| 26 |
+
# Decode the generated sequences
|
| 27 |
+
mcqs = [tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
|
| 28 |
|
| 29 |
return mcqs
|
| 30 |
|
|
|
|
| 44 |
|
| 45 |
st.write("Generated MCQs:")
|
| 46 |
for idx, mcq in enumerate(mcqs):
|
| 47 |
+
st.write(f"{idx+1}. {mcq}")
|