Spaces:
Runtime error
Runtime error
import streamlit as st | |
import random | |
import spacy | |
import requests | |
from bs4 import BeautifulSoup | |
import re | |
import spacy | |
import language_tool_python | |
# Initialize LanguageTool | |
tool = language_tool_python.LanguageToolPublicAPI('en-US') | |
# Define the Streamlit app | |
st.title("NLP Testing and Scoring App") | |
# Web scraping and text cleaning | |
entity = "Canada" | |
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}", "Explain {entity}", "What do you know about {entity}"], | |
"ORG": ["What is {entity}?", "Tell me about {entity}", "Explain {entity}", "What do you know about {entity}"], | |
"GPE": ["Tell me about {entity}", "Explain {entity}", "What do you know about {entity}", "Describe {entity}", "Where is {entity}"], | |
"MONEY": ["How much is {entity}?", "Tell me the value of {entity}", "Explain the amount of {entity}"], | |
"DATE": ["Why was {entity} important?", "Explain what happened on {entity}"], | |
# 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 {entity} {topic}"]) | |
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, topic is not None) | |
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] | |
questions.extend(entity_questions) | |
return questions | |
# Example usage | |
data = ' '.join(paragraphs) | |
noOfQues = 5 | |
subjective_generator = SubjectiveTest(data, noOfQues) | |
question_list = subjective_generator.generate_test("") | |
questions = [] | |
for i, question in enumerate(question_list): | |
if "Explain" not in question and len(tool.check(question)) == 0 and grammar_sense(question) == "Make Sense": | |
questions.append(f"Question: {question}") | |
scores = [] | |
# Now, add a Streamlit UI for input and displaying scores | |
st.subheader("Answer the following questions:") | |
user_responses = {} | |
for i, question in enumerate(questions): | |
response = st.text_area(f"Question {i + 1}", key=f"response_{i}") | |
user_responses[f"response_{i}"] = response | |
if st.button("Calculate Scores"): | |
for i in questions: | |
res = user_responses[f"response_{i}"] | |
# Simulate the scoring process, replace with your actual scoring logic | |
score = random.randint(0, 100) | |
scores.append(score) | |
x = 0 | |
new_scores = [] | |
for i in scores: | |
if i == 'N/A': | |
scores.pop(x) | |
scores.append(85) | |
x = x + 1 | |
else: | |
x = x + 1 | |
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 | |
st.subheader(f'Your average score is {calculate_average(scores)}') | |