Manikandan-Alagu commited on
Commit
1343b02
·
verified ·
1 Parent(s): 981230c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -56
app.py CHANGED
@@ -3,7 +3,7 @@ import json
3
  import pandas as pd
4
  import traceback
5
  import streamlit as st
6
- from src.mcqgenerator.utilis import read_file,get_table_data
7
  from src.mcqgenerator.logger import logging
8
  from src.mcqgenerator.mcqgenerator import generate_evaluate_chain
9
  from langchain_community.callbacks import get_openai_callback
@@ -18,66 +18,63 @@ with open('Response.json', 'r') as file:
18
  st.title("MCQs Creator Application")
19
 
20
  with st.form("user_inputs"):
21
- uploader_file=st.file_uploader("Upload a PDF or Txt file")
 
 
 
 
22
 
23
- mcq_count=st.text_input("No .of MCQS")
 
 
 
24
 
25
- subject=st.text_input("Insert subject", max_chars=20)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- tone=st.text_input("Complexity Level of Questions",max_chars=20, placeholder="Simple")
 
 
 
 
 
 
 
28
 
29
- button=st.form_submit_button("Create MCQs")
 
 
 
 
30
 
31
- if button and uploader_file is not None and mcq_count and subject and tone:
32
- with st.spinner("Loading...."):
33
- try:
34
- text=read_file(uploader_file)
 
 
35
 
36
- with get_openai_callback() as cb:
37
- response=generate_evaluate_chain(
38
- {
39
- "text": text,
40
- "number": mcq_count,
41
- "subject":subject,
42
- "tone": tone,
43
- "response_json": json.dumps(RESPONSE_JSON)
44
- }
45
- )
46
-
47
- except Exception as e:
48
- traceback.print_exception(type(e), e,e.__traceback__)
49
- st.error("ERROR")
50
 
51
- else:
52
- print(f"Total Tokens:{cb.total_tokens}")
53
- print(f"Prompt Tokens:{cb.prompt_tokens}")
54
- print(f"Completion Tokens:{cb.completion_tokens}")
55
- print(f"Total Cost:{cb.total_cost}")
56
- if isinstance(response,dict):
57
-
58
- quiz=response.get("quiz",None)
59
- if quiz is not None:
60
- table_data=get_table_data(quiz)
61
- if table_data is not None:
62
- df=pd.DataFrame(table_data)
63
- df.index=df.index+1
64
- st.table(df)
65
 
66
- st.text_area(label="Review",value=response["review"])
67
-
68
- # Download Buttons for CSV and PDF
69
- csv_filename = "generated_mcqs.csv"
70
- csv_link = f'<a href="data:file/csv;base64,{df.to_csv(index=False).encode("utf-8").decode()}" download="{csv_filename}">Download as CSV</a>'
71
- st.markdown(csv_link, unsafe_allow_html=True)
72
-
73
- pdf_filename = "generated_mcqs.pdf"
74
- pdf_link = f'<a href="data:application/pdf;base64,{df.to_html().encode("utf-8").decode()}" download="{pdf_filename}">Download as PDF</a>'
75
- st.markdown(pdf_link, unsafe_allow_html=True)
76
-
77
- else:
78
- st.error("Error in the table date")
79
-
80
- else:
81
- st.write(response)
82
-
83
-
 
3
  import pandas as pd
4
  import traceback
5
  import streamlit as st
6
+ from src.mcqgenerator.utilis import read_file, get_table_data
7
  from src.mcqgenerator.logger import logging
8
  from src.mcqgenerator.mcqgenerator import generate_evaluate_chain
9
  from langchain_community.callbacks import get_openai_callback
 
18
  st.title("MCQs Creator Application")
19
 
20
  with st.form("user_inputs"):
21
+ uploader_file = st.file_uploader("Upload a PDF or Txt file")
22
+ mcq_count = st.text_input("No. of MCQS")
23
+ subject = st.text_input("Insert subject", max_chars=20)
24
+ tone = st.text_input("Complexity Level of Questions", max_chars=20, placeholder="Simple")
25
+ button = st.form_submit_button("Create MCQs")
26
 
27
+ # Move these lines after the form submission
28
+ if button:
29
+ csv_filename = "generated_mcqs.csv"
30
+ pdf_filename = "generated_mcqs.pdf"
31
 
32
+ try:
33
+ text = read_file(uploader_file)
34
+ with get_openai_callback() as cb:
35
+ response = generate_evaluate_chain({
36
+ "text": text,
37
+ "number": mcq_count,
38
+ "subject": subject,
39
+ "tone": tone,
40
+ "response_json": json.dumps(RESPONSE_JSON)
41
+ })
42
+ except Exception as e:
43
+ traceback.print_exception(type(e), e, e.__traceback__)
44
+ st.error("ERROR")
45
+ else:
46
+ print(f"Total Tokens: {cb.total_tokens}")
47
+ print(f"Prompt Tokens: {cb.prompt_tokens}")
48
+ print(f"Completion Tokens: {cb.completion_tokens}")
49
+ print(f"Total Cost: {cb.total_cost}")
50
 
51
+ if isinstance(response, dict):
52
+ quiz = response.get("quiz", None)
53
+ if quiz is not None:
54
+ table_data = get_table_data(quiz)
55
+ if table_data is not None:
56
+ df = pd.DataFrame(table_data)
57
+ df.index = df.index + 1
58
+ st.table(df)
59
 
60
+ st.text_area(label="Review", value=response["review"])
61
+ else:
62
+ st.error("Error in the table data")
63
+ else:
64
+ st.write(response)
65
 
66
+ csv = st.download_button(
67
+ label="Download as CSV",
68
+ data=df.to_csv(index=False).encode("utf-8"),
69
+ file_name=csv_filename,
70
+ key="csv-download",
71
+ )
72
 
73
+ pdf = st.download_button(
74
+ label="Download as PDF",
75
+ data=df.to_html().encode("utf-8"),
76
+ file_name=pdf_filename,
77
+ key="pdf-download",
78
+ )
 
 
 
 
 
 
 
 
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80