Manikandan-Alagu commited on
Commit
f62d772
·
verified ·
1 Parent(s): f958fdb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -48
app.py CHANGED
@@ -23,59 +23,68 @@ with st.form("user_inputs"):
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
- if button:
67
- csv = st.download_button(
68
- label="Download as CSV",
69
- data=df.to_csv(index=False).encode("utf-8"),
70
- file_name=csv_filename,
71
- key="csv-download",
72
- )
73
-
74
- pdf = st.download_button(
75
- label="Download as PDF",
76
- data=df.to_html().encode("utf-8"),
77
- file_name=pdf_filename,
78
- key="pdf-download",
79
- )
 
 
 
 
80
 
81
 
 
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
+ download()
27
 
28
+ if button and uploaded_file is not None and mcq_count and subject and tone:
29
+ with st.spinner("loading..."):
30
+ try:
31
+ text=read_file(uploaded_file)
32
+ #Count tokens and the cost of API call
33
+ with get_openai_callback() as cb:
34
+ response=generate_evaluate_chain(
35
+ {
36
+ "text": text,
37
+ "number": mcq_count,
38
+ "subject":subject,
39
+ "tone": tone,
40
+ "response_json": json.dumps(RESPONSE_JSON)
41
+ }
42
+ )
43
+ #st.write(response)
44
 
45
+ except Exception as e:
46
+ traceback.print_exception(type(e), e, e.__traceback__)
47
+ st.error("Error")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
+ else:
50
+ print(f"Total Tokens:{cb.total_tokens}")
51
+ print(f"Prompt Tokens:{cb.prompt_tokens}")
52
+ print(f"Completion Tokens:{cb.completion_tokens}")
53
+ print(f"Total Cost:{cb.total_cost}")
54
+ if isinstance(response, dict):
55
+ #Extract the quiz data from the response
56
+ quiz=response.get("quiz", None)
57
+ if quiz is not None:
58
+ table_data=get_table_data(quiz)
59
+ if table_data is not None:
60
+ df=pd.DataFrame(table_data)
61
+ df.index=df.index+1
62
+ st.table(df)
63
+ #Display the review in atext box as well
64
+ st.text_area(label="Review", value=response["review"])
65
+ else:
66
+ st.error("Error in the table data")
67
 
 
 
 
68
  else:
69
  st.write(response)
70
+
71
+ def download():
72
+ if button:
73
+ csv_filename = "generated_mcqs.csv"
74
+ pdf_filename = "generated_mcqs.pdf"
75
+
76
+ csv = st.download_button(
77
+ label="Download as CSV",
78
+ data=df.to_csv(index=False).encode("utf-8"),
79
+ file_name=csv_filename,
80
+ key="csv-download",
81
+ )
82
+
83
+ pdf = st.download_button(
84
+ label="Download as PDF",
85
+ data=df.to_html().encode("utf-8"),
86
+ file_name=pdf_filename,
87
+ key="pdf-download",
88
+ )
89
 
90