BillBojangeles2000 commited on
Commit
5e02517
β€’
1 Parent(s): b3be0d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -52
app.py CHANGED
@@ -8,40 +8,6 @@ import language_tool_python
8
  import streamlit as st
9
  from gradio_client import Client
10
 
11
- # Initialize LanguageTool
12
- tool = language_tool_python.LanguageToolPublicAPI('en-US')
13
-
14
- # Helper function to check grammar and sense
15
- def grammar_sense(sentence):
16
- sense = tool.correct(sentence)
17
- grammar = "Correct Grammar" if not tool.check(sentence) else "Incorrect Grammar"
18
- return "Make Sense" if "Not" not in sense and grammar == "Correct Grammar" else "Not Make Sense"
19
-
20
- # Web scraping and text cleaning
21
- Quiz_Gen = st.form("Quiz Generation")
22
- res = Quiz_Gen.text_input("What topic do you want to get quizzed on?")
23
- sub = Quiz_Gen.form_submit_button("Submit")
24
- if sub:
25
- entity = res
26
- prefix = "https://wiki.kidzsearch.com/wiki/"
27
- page = requests.get(f'{prefix}{entity}')
28
- res = BeautifulSoup(page.content, 'html.parser')
29
-
30
- text = [i.get_text() for i in res.find_all('p')]
31
-
32
- cleaned_text = ' '.join(text)
33
- cleaned_text = re.sub(r'[^a-zA-Z0-9.,]', ' ', cleaned_text)
34
- paragraphs = [p.strip() for p in re.split(r'\n', cleaned_text) if p.strip()]
35
-
36
- # Process text using SpaCy
37
- nlp = spacy.load("en_core_web_sm")
38
- doc = nlp(cleaned_text)
39
-
40
- sentences = [sent.text for sent in doc.sents]
41
-
42
- # Combine sentences into paragraphs
43
- paragraphs = [f"{sentences[i]} {sentences[i + 1]}" if i + 1 < len(sentences) else sentences[i] for i in range(0, len(sentences), 2)]
44
-
45
  class SubjectiveTest:
46
 
47
  def __init__(self, data, noOfQues):
@@ -89,8 +55,41 @@ class SubjectiveTest:
89
 
90
  return questions
91
 
92
- # Example usage
 
 
 
 
 
 
 
 
 
 
 
 
93
  if sub:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  data = ' '.join(paragraphs)
95
  noOfQues = 5
96
  st.toast("Creating Questions", icon='βœ…')
@@ -98,29 +97,35 @@ if sub:
98
  question_list = subjective_generator.generate_test("")
99
  questions = []
100
 
 
 
101
  Quiz = st.form("Quiz")
102
  st.toast("Filtering Questions", icon='βœ…')
103
- for i, question in enumerate(question_list):
104
- if "Explain" not in question and len(tool.check(question)) == 0 and grammar_sense(question) == "Make Sense":
105
- questions.append(f"{question}")
106
 
107
- scores = []
108
- client = Client("https://billbojangeles2000-zephyr-7b-alpha-chatbot-karki.hf.space/")
109
- ans = []
 
 
 
110
 
111
- # Create an empty list to store the user-submitted scores
112
- user_scores = []
 
 
113
 
114
- for i, question in enumerate(questions):
115
- res = Quiz.text_input(f'{question}')
116
- ans.append(res)
 
 
117
 
118
  submit_button = Quiz.form_submit_button("Submit")
119
 
120
  if submit_button:
121
  st.toast("Calculating grade", icon='βœ…')
122
  with st.spinner(text="Calculating Grade"):
123
- for i, q in enumerate(questions):
124
  st.toast(f'iteration {i} has begun', icon='βœ…')
125
  result = client.predict(
126
  f'What would you rate this answer to the question: "{q}" as a percentage? Here is the answer: {ans[i]}. Your percentage grade cannot be negative or over 100%. Additionally, you should also assume that the user is of a 5-7th grade level of intellect.',
@@ -130,20 +135,20 @@ if submit_button:
130
  1.2,
131
  api_name="/chat"
132
  )
133
-
134
  pattern = r'(\d+)%'
135
-
136
  match = re.search(pattern, result)
137
  if match:
138
  score = match.group(1)
139
  user_scores.append(int(score))
140
  else:
141
  user_scores.append(85) # You can set a default score if no score is found
142
-
143
  # Calculate the average score using the user_scores list
144
  average_score = sum(user_scores) / len(user_scores)
145
-
146
  st.info(f'Your average score for the answers is {average_score}%')
147
  st.write(f'Your average score for the answers is {average_score}%')
148
 
149
- st.balloons()
 
8
  import streamlit as st
9
  from gradio_client import Client
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  class SubjectiveTest:
12
 
13
  def __init__(self, data, noOfQues):
 
55
 
56
  return questions
57
 
58
+ # Initialize LanguageTool
59
+ tool = language_tool_python.LanguageToolPublicAPI('en-US')
60
+
61
+ # Helper function to check grammar and sense
62
+ def grammar_sense(sentence):
63
+ sense = tool.correct(sentence)
64
+ grammar = "Correct Grammar" if not tool.check(sentence) else "Incorrect Grammar"
65
+ return "Make Sense" if "Not" not in sense and grammar == "Correct Grammar" else "Not Make Sense"
66
+
67
+ Quiz_Gen = st.form("Quiz Generation")
68
+ res = Quiz_Gen.text_input("What topic do you want to get quizzed on?")
69
+ sub = Quiz_Gen.form_submit_button("Submit")
70
+
71
  if sub:
72
+ entity = res
73
+ prefix = "https://wiki.kidzsearch.com/wiki/"
74
+ page = requests.get(f'{prefix}{entity}')
75
+ res = BeautifulSoup(page.content, 'html.parser')
76
+
77
+ text = [i.get_text() for i in res.find_all('p')]
78
+
79
+ cleaned_text = ' '.join(text)
80
+ cleaned_text = re.sub(r'[^a-zA-Z0-9.,]', ' ', cleaned_text)
81
+ paragraphs = [p.strip() for p in re.split(r'\n', cleaned_text) if p.strip()]
82
+
83
+ # Process text using SpaCy
84
+ nlp = spacy.load("en_core_web_sm")
85
+ doc = nlp(cleaned_text)
86
+
87
+ sentences = [sent.text for sent in doc.sents]
88
+
89
+ # Combine sentences into paragraphs
90
+ paragraphs = [f"{sentences[i]} {sentences[i + 1]}" if i + 1 < len(sentences) else sentences[i] for i in range(0, len(sentences), 2)]
91
+
92
+ # Example usage
93
  data = ' '.join(paragraphs)
94
  noOfQues = 5
95
  st.toast("Creating Questions", icon='βœ…')
 
97
  question_list = subjective_generator.generate_test("")
98
  questions = []
99
 
100
+ st.session_state.questions = question_list # Store the generated questions in session state
101
+
102
  Quiz = st.form("Quiz")
103
  st.toast("Filtering Questions", icon='βœ…')
 
 
 
104
 
105
+ # Check if questions are generated in session state
106
+ if 'questions' in st.session_state:
107
+ question_index = 0
108
+
109
+ while question_index < len(st.session_state.questions):
110
+ current_question = st.session_state.questions[question_index]
111
 
112
+ # Check if the current question meets your criteria
113
+ if "Explain" not in current_question and len(tool.check(current_question)) == 0 and grammar_sense(current_question) == "Make Sense":
114
+ # Get user input for the current question
115
+ user_answer = Quiz.text_input(f'{current_question}')
116
 
117
+ # Append the user answer to the list
118
+ ans.append(user_answer)
119
+
120
+ # Move to the next question
121
+ question_index += 1
122
 
123
  submit_button = Quiz.form_submit_button("Submit")
124
 
125
  if submit_button:
126
  st.toast("Calculating grade", icon='βœ…')
127
  with st.spinner(text="Calculating Grade"):
128
+ for i, q in enumerate(st.session_state.questions):
129
  st.toast(f'iteration {i} has begun', icon='βœ…')
130
  result = client.predict(
131
  f'What would you rate this answer to the question: "{q}" as a percentage? Here is the answer: {ans[i]}. Your percentage grade cannot be negative or over 100%. Additionally, you should also assume that the user is of a 5-7th grade level of intellect.',
 
135
  1.2,
136
  api_name="/chat"
137
  )
138
+
139
  pattern = r'(\d+)%'
140
+
141
  match = re.search(pattern, result)
142
  if match:
143
  score = match.group(1)
144
  user_scores.append(int(score))
145
  else:
146
  user_scores.append(85) # You can set a default score if no score is found
147
+
148
  # Calculate the average score using the user_scores list
149
  average_score = sum(user_scores) / len(user_scores)
150
+
151
  st.info(f'Your average score for the answers is {average_score}%')
152
  st.write(f'Your average score for the answers is {average_score}%')
153
 
154
+ st.balloons()