DevBM commited on
Commit
395daa0
·
verified ·
1 Parent(s): eb6547a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -76
app.py CHANGED
@@ -1,77 +1,78 @@
1
- import streamlit as st
2
- import streamlit as st
3
- from pprint import pprint
4
- import subprocess
5
- cmd = ["python", "-m", "spacy", "download", "en_core_web_sm"]
6
- subprocess.run(cmd)
7
- from spacy.cli import download
8
- from Questgen import main
9
- from PyPDF2 import PdfReader
10
- from transformers import pipeline
11
- from PyPDF2 import PdfReader
12
- import nltk
13
- import pandas as pd
14
- nltk.download('punkt')
15
- # st.title(body='7 - Question Generation')
16
-
17
-
18
- def get_pdf_text(pdf_docs):
19
- text = ""
20
- for pdf in pdf_docs:
21
- pdf_reader = PdfReader(pdf)
22
- for page in pdf_reader.pages:
23
- text += page.extract_text()
24
- return text
25
-
26
-
27
- ########################################################
28
- # st.header(body='Proposition 1',divider='red')
29
-
30
- # if st.toggle(label='Show Proposition 1'):
31
- st.title('Generate Questions from PDFs')
32
- file = st.file_uploader(label='Upload',accept_multiple_files=True)
33
- pr = st.button(label='Process')
34
- if pr:
35
- # pr = st.button(label='Process')
36
- raw_text = get_pdf_text(file)
37
- # questions = []
38
- ge = main.QGen()
39
- payload = {
40
- 'input_text' : raw_text,
41
- # 'max_questions':2,
42
- }
43
- output = ge.predict_mcq(payload=payload)
44
- st.header(body='*Generated Questions are:*', divider='orange')
45
- for question in output['questions']:
46
- st.subheader(body=f":orange[Q{question['id']}:] {question['question_statement']}", divider='blue')
47
- st.markdown(f"A: {question['answer']}")
48
- c = 0
49
- for option in question['options']:
50
- # st.markdown(f"{c}")
51
- c+=1
52
- if c==1:
53
- st.markdown(f"B: {option}")
54
- elif c==2:
55
- st.markdown(f"C: {option}")
56
- elif c==3:
57
- st.markdown(f"D: {option}")
58
-
59
-
60
- if output is not None:
61
- # Convert the dictionary to a DataFrame
62
- df = pd.DataFrame(output['questions'])
63
-
64
- # Convert the options from lists to strings
65
- # df['options'] = df['options'].apply(lambda x: ','.join(x))
66
- df = df.drop(labels=['options_algorithm','extra_options','context','question_type'],axis=1)
67
- # Convert the DataFrame to CSV
68
- csv = df.to_csv(index=False).encode('utf-8')
69
- st.download_button(
70
- label='Download Data',
71
- data=csv,
72
- file_name='Generated MCQs.csv',
73
- mime='text/csv'
74
- )
75
-
76
- if st.toggle(label='Show Raw Output'):
 
77
  st.write(output)
 
1
+ import streamlit as st
2
+ import streamlit as st
3
+ from pprint import pprint
4
+ import subprocess
5
+ cmd = ["python", "-m", "spacy", "download", "en_core_web_sm"]
6
+ subprocess.run(cmd)
7
+ from spacy.cli import download
8
+ from Questgen import main, main2
9
+ from PyPDF2 import PdfReader
10
+ from transformers import pipeline
11
+ from PyPDF2 import PdfReader
12
+ import nltk
13
+ import pandas as pd
14
+ nltk.download('punkt')
15
+ # st.title(body='7 - Question Generation')
16
+
17
+
18
+ def get_pdf_text(pdf_docs):
19
+ text = ""
20
+ for pdf in pdf_docs:
21
+ pdf_reader = PdfReader(pdf)
22
+ for page in pdf_reader.pages:
23
+ text += page.extract_text()
24
+ return text
25
+
26
+
27
+ ########################################################
28
+ # st.header(body='Proposition 1',divider='red')
29
+
30
+ # if st.toggle(label='Show Proposition 1'):
31
+ st.title('Generate Questions from PDFs')
32
+ file = st.file_uploader(label='Upload',accept_multiple_files=True)
33
+ pr = st.button(label='Process')
34
+ if pr:
35
+ # pr = st.button(label='Process')
36
+ raw_text = get_pdf_text(file)
37
+ # questions = []
38
+ # ge = main.QGen()
39
+ ge = main2.QGen()
40
+ payload = {
41
+ 'input_text' : raw_text,
42
+ # 'max_questions':2,
43
+ }
44
+ output = ge.predict_mcq(payload=payload)
45
+ st.header(body='*Generated Questions are:*', divider='orange')
46
+ for question in output['questions']:
47
+ st.subheader(body=f":orange[Q{question['id']}:] {question['question_statement']}", divider='blue')
48
+ st.markdown(f"A: {question['answer']}")
49
+ c = 0
50
+ for option in question['options']:
51
+ # st.markdown(f"{c}")
52
+ c+=1
53
+ if c==1:
54
+ st.markdown(f"B: {option}")
55
+ elif c==2:
56
+ st.markdown(f"C: {option}")
57
+ elif c==3:
58
+ st.markdown(f"D: {option}")
59
+
60
+
61
+ if output is not None:
62
+ # Convert the dictionary to a DataFrame
63
+ df = pd.DataFrame(output['questions'])
64
+
65
+ # Convert the options from lists to strings
66
+ # df['options'] = df['options'].apply(lambda x: ','.join(x))
67
+ df = df.drop(labels=['options_algorithm','extra_options','context','question_type'],axis=1)
68
+ # Convert the DataFrame to CSV
69
+ csv = df.to_csv(index=False).encode('utf-8')
70
+ st.download_button(
71
+ label='Download Data',
72
+ data=csv,
73
+ file_name='Generated MCQs.csv',
74
+ mime='text/csv'
75
+ )
76
+
77
+ if st.toggle(label='Show Raw Output'):
78
  st.write(output)