-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit.py
158 lines (136 loc) · 5.46 KB
/
streamlit.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Importing libraries
import streamlit as st
import pandas as pd
import json
import traceback
import os
from io import BytesIO
from fpdf import FPDF
from langchain.callbacks.manager import get_openai_callback
from src.mcqgenerater.logger import logger
from src.mcqgenerater.utils import read_file, get_table_data
from src.mcqgenerater.MCQgenerater import generate_evaluate_chain
from dotenv import load_dotenv
# Read JSON file
file_path = r"E:\Generative Ai Project\response.json"
try:
with open(file_path, 'r') as file:
response_json = json.load(file)
except json.JSONDecodeError as e:
st.error("The JSON file is malformed: " + str(e))
logger.error("The JSON file is malformed: " + str(e))
st.stop()
# Create title for Streamlit app
st.set_page_config(page_title="MCQ Generator 🤖📚", layout="wide")
st.title("MCQ Generator Application With Langchain 🤖📚")
# Add custom CSS
st.markdown(
"""
<style>
h1, h2, h3, h4, h5, h6 {
color: #4CAF50;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.download-button {
display: flex;
justify-content: center;
margin-top: 20px;
}
.output-container {
background-color: #f1f1f1;
padding: 20px;
border-radius: 4px;
}
</style>
""",
unsafe_allow_html=True
)
# Create a form using st.form
with st.form("user_inputs"):
# Upload File
uploaded_file = st.file_uploader("Upload a PDF, Text, DOCX, HTML File")
# Input fields
mcq_count = st.number_input("Enter number of MCQs", min_value=1, max_value=50)
# Text input
subject = st.text_input("Enter Subject", max_chars=20)
# Tone
tone = st.text_input("Complexity Level of Questions", max_chars=20, placeholder="Simple")
# Submit button
button = st.form_submit_button("Generate MCQs")
# Check if button is clicked and all fields are filled
if button and uploaded_file is not None and mcq_count and subject and tone:
with st.spinner("Generating MCQs..."):
try:
logger.info("Received user inputs and file upload.")
text = read_file(uploaded_file)
if text is None:
st.error("Unsupported file format.")
logger.error("Unsupported file format.")
st.stop()
# Count tokens and cost of operation
with get_openai_callback() as cb:
response = generate_evaluate_chain(
{
"text": text,
"number": mcq_count,
"subject": subject,
"tone": tone,
"response_json": json.dumps(response_json)
}
)
logger.info(f"Received response: {response}")
quiz = response.get("quiz", None)
if quiz:
table_data = get_table_data(quiz)
if table_data is not None:
st.markdown("## Generated MCQs 🎉")
st.write("Here are the generated MCQs:")
st.markdown("---")
st.dataframe(table_data)
# Download options
def convert_to_pdf(df):
pdf = FPDF()
pdf.add_page()
pdf.set_auto_page_break(auto=True, margin=15)
pdf.set_font("Arial", size=12)
for index, row in df.iterrows():
pdf.multi_cell(0, 10, f"{index+1}. {row['MCQ']}\nChoices: {row['Choices']}\nCorrect: {row['Correct']}\n")
pdf_output = BytesIO()
pdf_output.write(pdf.output(dest='S').encode('latin1'))
pdf_output.seek(0)
return pdf_output
pdf_data = convert_to_pdf(table_data)
with st.container():
st.markdown("### Download Options 📥")
st.write("Choose your preferred format:")
download_button = st.download_button(
label="Download as PDF",
data=pdf_data,
file_name="quiz.pdf",
mime="application/pdf"
)
logger.info("MCQs displayed and download options provided.")
else:
st.error("Error in table data")
logger.error("Error in table data")
else:
st.write("No quiz data found in response.")
logger.info("No quiz data found in response.")
except Exception as e:
st.error("Error: " + str(e))
logger.error(f"Error generating MCQs: {e}")
else:
logger.info(f"Total Tokens: {cb.total_tokens}")
logger.info(f"Prompt Tokens: {cb.prompt_tokens}")
logger.info(f"Completion Tokens: {cb.completion_tokens}")
logger.info(f"Total Cost: {cb.total_cost}")