Spaces:
Sleeping
Sleeping
import os | |
import json | |
import pandas as pd | |
import traceback | |
import streamlit as st | |
from utils import get_table_data | |
from mcqgen import generate_evaluate_chain | |
from transformers import GPT2LMHeadModel, GPT2Tokenizer | |
# Load the JSON file | |
with open('response.json', 'r', encoding="utf-8") as file: | |
RESPONSE_JSON = json.load(file) | |
# Create a title | |
st.title("MCQ Creator Application with OpenAI's GPT-2") | |
# Create a form using st.form | |
with st.form("user_inputs"): | |
uploaded_file = st.file_uploader("Upload a PDF file", type="pdf") | |
mcq_count = st.number_input("No. of MCQ", min_value=3, max_value=50, value=5) | |
tone = st.text_input("Complexity level of Questions", max_chars=20, value="simple") | |
button = st.form_submit_button("Create MCQs") | |
if button and uploaded_file is not None and mcq_count and tone: | |
with st.spinner("Loading..."): | |
try: | |
text = uploaded_file.read().decode("utf-8") | |
response = generate_evaluate_chain({ | |
"text": text, | |
"number": mcq_count, | |
"tone": tone, | |
"response_json": json.dumps(RESPONSE_JSON) | |
}) | |
except Exception as e: | |
traceback.print_exception(type(e), e, e.__traceback__) | |
st.error("Error") | |
if isinstance(response, dict): | |
# Extract quiz data from the response | |
quiz = response.get("quiz", None) | |
if quiz is not None: | |
table_data = get_table_data(quiz) | |
if table_data is not None: | |
df = pd.DataFrame(table_data) | |
df.index = df.index + 1 | |
st.table(df) | |
# Display the review in a textbox as well | |
st.text_area(label="Review", value=response["review"]) | |
else: | |
st.error("Error in the table data") | |
else: | |
st.write(response) | |