Spaces:
Sleeping
Sleeping
File size: 3,752 Bytes
5248bb3 1343b02 5248bb3 38e13f3 5248bb3 9534df5 fccfa74 f958fdb 1343b02 5248bb3 fccfa74 ac0c50f f62d772 ac0c50f f62d772 ac0c50f f62d772 ac0c50f f62d772 ac0c50f f62d772 ac0c50f f62d772 ac0c50f f62d772 ac0c50f f62d772 ac0c50f fccfa74 f62d772 ac0c50f 902a2ad 9b029ab fccfa74 |
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 |
import os
import json
import pandas as pd
import traceback
import streamlit as st
from src.mcqgenerator.utilis import read_file, get_table_data
from src.mcqgenerator.logger import logging
from src.mcqgenerator.mcqgenerator import generate_evaluate_chain
from langchain_community.callbacks import get_openai_callback
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.chains import SequentialChain
with open('Response.json', 'r') as file:
RESPONSE_JSON = json.load(file)
st.title("MCQs Creator Application")
with st.form("user_inputs"):
uploader_file = st.file_uploader("Upload a PDF or Txt file")
mcq_count = st.text_input("No. of MCQS")
subject = st.text_input("Insert subject", max_chars=20)
tone = st.text_input("Complexity Level of Questions", max_chars=20, placeholder="Simple")
button = st.form_submit_button("Create MCQs")
df = None # Declare df at a broader scope
if button and uploader_file is not None and mcq_count and subject and tone:
with st.spinner("loading..."):
try:
text = read_file(uploader_file)
# Count tokens and the cost of the API call
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)
}
)
except Exception as e:
traceback.print_exception(type(e), e, e.__traceback__)
st.error("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}")
if isinstance(response, dict):
# Extract the 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 text box as well
st.text_area(label="Review", value=response["review"])
# Call the download function
else:
st.error("Error in the table data")
else:
st.write(response)
def download(df):
col1, col2 = st.columns(2)
if button:
if col1.button("Download CSV"):
st.download_button(data=df.to_csv(index=False).encode("utf-8"),
file_name="generated_mcqs.csv",
key="csv-download",
help="Click to download as CSV"
)
if col2.button("Download PDF"):
st.download_button(data=df.to_html().encode("utf-8"),
file_name="generated_mcqs.pdf",
key="pdf-download",
help="Click to download as PDF"
)
# Now you can use df outside the form if needed
if df is not None:
download(df)
|