-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamlitApp.py
99 lines (83 loc) · 3.52 KB
/
StreamlitApp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
import json
import traceback
import pandas as pd
from dotenv import load_dotenv
import streamlit as st
from src.mcq_generator.utils import read_file, get_mcq_data
from langchain_community.callbacks import get_openai_callback
from src.mcq_generator.MCQ_Generator import evaluation_chain
from src.mcq_generator.logger import logging
from src.mcq_generator.download import download_pdf
# loading the json file
with open ('response.json', 'r') as file:
data = file.read()
RESPONSE_JSON = json.loads(data)
# creating title for the app
st.title("MCQ Generator")
# Creating form
with st.form ("user_inputs"):
# upload file
uploaded_file= st.file_uploader("Upload a pdf or txt file")
# Input fields
mcq_count = st.number_input("Number of MCQs ", min_value=3, max_value= 50)
# Subject
subject= st.text_input("Enter Subject", max_chars=20)
st.session_state['subject'] = subject
# Quiz Tone
tone= st.selectbox("Difficulty Level", ["Simple", "Medium", "Hard"])
# Adding Button
button = st.form_submit_button("Generate MCQs")
# Checking if button is clicked and all fields have input
if button and uploaded_file is not None and mcq_count and subject and tone:
with st.spinner("generating..."):
try:
text = read_file(uploaded_file)
# Counting tokens and Cost of API call
with get_openai_callback() as cb:
response = evaluation_chain(
{
'text' : text,
'number' : mcq_count,
"subject" : subject,
'tone' : tone,
'response_json' : json.dumps(RESPONSE_JSON)
}
)
# st.write(response)
except Exception as e:
traceback.print_exception(type(e), e,e.__traceback__)
st.write("Error")
else:
print(f"Total Tokens : {cb.total_tokens}")
print(f"Prompt Tokens : {cb.prompt_tokens}")
print(f"Completion Tokens : {cb.completion_tokens}")
print(f"Total Cost : {cb.total_cost}")
# checks whether response is a instance of dictionary or not
if isinstance(response, dict):
# extracting mcq data from the response
mcq = response.get('mcq', None)
if mcq is not None:
mcq_data = get_mcq_data(mcq)
if mcq_data is not None:
df = pd.DataFrame(mcq_data)
df.index = df.index+1
st.table(df)
st.session_state['df'] = df
# display the review in a text box
# st.text_area(label = "Review", value = response['review'])
else:
st.write("Error in the mcq_data")
else:
st.write(response)
# Adding download button to download the generated mcq in the pdf form
if 'df' in st.session_state:
buffer = download_pdf(st.session_state['df'], f"{st.session_state['subject']}_mcq.pdf")
st.download_button(
label="Download",
data=buffer,
file_name=f"{st.session_state['subject']}_mcq.pdf",
mime="application/pdf"
)
else:
st.write("No MCQs to download")