Spaces:
Sleeping
Sleeping
import os | |
import json | |
import pandas as pd | |
import traceback | |
import streamlit as st | |
from dotenv import load_dotenv | |
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 Langchain") | |
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") | |
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) | |
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): | |
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) | |
st.text_area(label="Review",value=response["review"]) | |
else: | |
st.error("Error in the table date") | |
else: | |
st.write(response) | |