File size: 7,540 Bytes
5a44472
 
 
 
 
 
 
 
a485681
f0f53a6
 
6746bb1
 
740397e
6746bb1
f0f53a6
 
 
740397e
a485681
740397e
 
 
 
 
 
 
 
 
 
0f843c9
740397e
 
 
 
 
 
 
 
 
 
 
 
 
 
5a44472
740397e
 
 
 
f0f53a6
5a44472
 
 
 
 
9ca337b
 
a1f74de
9ca337b
5a44472
987bab3
 
 
 
5a44472
 
138fa66
5a44472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed5d2a9
5a44472
 
 
 
 
 
a5867f3
 
 
 
 
5a44472
 
 
 
 
 
 
22156ab
5a44472
 
 
 
 
 
 
 
fca80c7
762e183
5a44472
7d0c38a
 
 
 
 
5a44472
 
 
edc4138
5a44472
22156ab
 
789590c
 
5a44472
 
 
db212dc
 
 
5a44472
987bab3
 
edc4138
987bab3
f0f53a6
987bab3
 
9ca337b
762e183
b810080
a1f74de
 
 
 
 
 
 
762e183
 
 
 
cc14a13
 
762e183
cc14a13
 
 
 
762e183
 
8fbd44d
762e183
 
 
 
 
987bab3
762e183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc14a13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import streamlit as st
import random
import spacy
import requests
from bs4 import BeautifulSoup
import re
import spacy
import language_tool_python
import json
from gradio_client import Client

API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-mnli"
headers = {"Authorization": "Bearer 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"}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()

# Define the grammar_sense function
def grammar_sense(sentence):
    sense = query({
        "inputs": sentence,
        "parameters": {"candidate_labels": ["Make Sense", "Not Make Sense"]},
    })
    grammar = query({
        "inputs": sentence,
        "parameters": {"candidate_labels": ["Correct Grammar", "Incorrect Grammar"]},
    })
    objects = ["Sense", "Grammar"]
    ans = []
    for i in objects:
        if i == "Sense":
            response_data = json.loads(json.dumps(sense))
            labels = response_data['labels']
            scores = response_data['scores']
            index_of_highest_score = scores.index(max(scores))
            highest_score_label = labels[index_of_highest_score]
            ans.append(highest_score_label)
        else:
            response_data = json.loads(json.dumps(grammar))
            labels = response_data['labels']
            scores = response_data['scores']
            index_of_highest_score = scores.index(max(scores))
            highest_score_label = labels[index_of_highest_score]
            ans.append(highest_score_label)

    if not 'Not' in ans[0] and ans[1] == 'Correct Grammar':
        return True
    else:
        return False

# Initialize LanguageTool
tool = language_tool_python.LanguageToolPublicAPI('en-US')

# Define the Streamlit app
st.title("NLP Testing and Scoring App")

# Ask for the topic at the start
topic = st.text_input("Enter a topic:")

# Web scraping and text cleaning
entity = "Florida"  # You can replace this with the user's topic input
if topic:
    entity = topic  # Use the user's input as the entity

prefix = "https://wiki.kidzsearch.com/wiki/"
page = requests.get(f'{prefix}{entity}')
res = BeautifulSoup(page.content, 'html.parser')

text = [i.get_text() for i in res.find_all('p')]

cleaned_text = ' '.join(text)
cleaned_text = re.sub(r'[^a-zA-Z0-9.,]', ' ', cleaned_text)
paragraphs = [p.strip() for p in re.split(r'\n', cleaned_text) if p.strip()]

# Process text using SpaCy
nlp = spacy.load("en_core_web_sm")
doc = nlp(cleaned_text)

sentences = [sent.text for sent in doc.sents]

# Combine sentences into paragraphs
paragraphs = [f"{sentences[i]} {sentences[i + 1]}" if i + 1 < len(sentences) else sentences[i] for i in range(0, len(sentences), 2)]
class SubjectiveTest:

    def __init__(self, data, noOfQues):
        self.summary = data
        self.noOfQues = noOfQues
        self.nlp = spacy.load("en_core_web_sm")

    def adjust_question_pattern(self, entity_label, topic_placeholder=True):
        question_patterns = {
            "PERSON": ["Who is {entity}?", "Tell me about {entity}", "What do you know about {entity}"],
            "ORG": ["What is {entity}?", "Tell me about {entity}", "What do you know about {entity}"],
            "GPE": ["Tell me about {entity}", "What do you know about {entity}", "Where is {entity}"],
            "MONEY": ["How much is {entity}?", "Tell me the value of {entity}"],
            "DATE": ["Why was {entity} important?"],
            # Add more entity-label to question-pattern mappings as needed
        }

        if topic_placeholder:
            for key in question_patterns:
                question_patterns[key] = [pattern + " {topic}" for pattern in question_patterns[key]]

        return question_patterns.get(entity_label, "Explain")

    def generate_test(self, topic=None):
        doc = self.nlp(self.summary)
        question_answer_dict = dict()

        for sentence in doc.sents:
            for ent in sentence.ents:
                entity_label = ent.label_
                entity_text = ent.text
                question_patterns = self.adjust_question_pattern(entity_label, "")
                for pattern in question_patterns:
                    question = pattern.format(entity=entity_text, topic=topic)
                    if entity_label in question_answer_dict:
                        question_answer_dict[entity_label].append(question)
                    else:
                        question_answer_dict[entity_label] = [question]

        questions = []

        for entity_label, entity_questions in question_answer_dict.items():
            entity_questions = entity_questions[:self.noOfQues]
            if "Explain" in entity_questions:
                continue
            else:
                questions.extend(entity_questions)

        return questions

with st.form("quiz_form"):
    # Create a button to initiate quiz generation
    generate_quiz = st.form_submit_button("Generate Quiz")

if generate_quiz:
    st.write("Generating the quiz...")
    data = ' '.join(paragraphs)
    noOfQues = 5

    subjective_generator = SubjectiveTest(data, noOfQues)
    questions = subjective_generator.generate_test(topic)  # Use the user's input topic here

    # Filter out invalid and empty questions
    x = 0
    while x > len(questions):
        for i in questions:
            if len(i) == 1:
                questions.pop(x)
                x = x + 1
            else:
                x = x + 1
    # Ensure you have valid questions
    if not questions:
        st.write("No valid questions to process.")
    else:
        answers = {}  # Dictionary to store answers

        # Use the filtered questions in your code
        for i, question in enumerate(questions):
            res = st.text_input(f'Q{i + 1}: {question}')  # Get user input for each question
            answers[f'Q{i + 1}'] = res  # Store the user's answer

        scores = []
        client = Client("https://billbojangeles2000-zephyr-7b-alpha-chatbot-karki.hf.space/")

        question_list = subjective_generator.generate_test(topic)  # Define 'questions' here
        questions = []
        for i, question in enumerate(question_list):
            if (question != "") and (len(tool.check(question)) == 0) and (grammar_sense(question)):
                questions.append(f"Question: {question}")

        for i, question in enumerate(questions):
            res = answers[f'Q{i + 1}']
            if res:
                result = client.predict(
                    f'What would you rate this answer to the question: "{question}" as a percentage? Here is the answer: {res}. Make sure to write your answer as "Score" and then write your score of the response.',
                    0.9,
                    256,
                    0.9,
                    1.2,
                    api_name="/chat"
                )
                pattern = r'(\d+)%'
                match = re.search(pattern, result)
                if match:
                    score = int(match.group(1))
                    scores.append(score)
                else:
                    scores.append(85)

        def calculate_average(numbers):
            if not numbers:
                return 0  # Return 0 for an empty list to avoid division by zero.

            total = sum(numbers)
            average = total / len(numbers)
            return average

        # Calculate and display the average score
        average_score = calculate_average(scores)
        st.write(f'Your average score is {average_score}')