Spaces:
Runtime error
Runtime error
import gradio as gr | |
''' | |
import nltk | |
import simplemma | |
from nltk.tokenize import word_tokenize | |
from nltk.tokenize import sent_tokenize | |
from nltk.probability import FreqDist | |
from simplemma import text_lemmatizer | |
nltk.download('punkt') | |
''' | |
def sentence(cerca_una_parola): | |
return f"""La parola da cercare è {cerca_una_parola}}""" | |
def upper(cerca_una_parola): | |
return cerca_una_parola.upper() | |
demo = fr.Textbox(fn=sentence, inputs='text', outputs='text') | |
demo.launch() | |
demo2 = fr.Textbox(fn=upper, inputs='text', outputs='text') | |
demo2.launch() | |
''' | |
demo = gr.Interface( | |
sentence_builder, | |
[ | |
gr.Textbox(), | |
gr.Radio(["park", "zoo", "road"]), | |
gr.CheckboxGroup(["ran", "swam", "ate", "slept"]), | |
gr.Checkbox(label="Is it the morning?"), | |
], | |
"text", | |
examples=[ | |
["cats", "park", ["ran", "swam"], True], | |
["dog", "zoo", ["ate", "swam"], False], | |
["bird", "road", ["ran"], False], | |
["cat", "zoo", ["ate"], True], | |
], | |
) | |
''' | |
''' | |
def update(name): | |
return f"Welcome to Gradio, {name}!" | |
def search_engine_bot(target): | |
result = [] | |
for i,sent in enumerate(sentences_lower): | |
if target.lower() in sent: | |
result.append(sentences[i]) | |
if len(result) == 0: | |
return (f"Non ho trovato la parola '{target}' nei testi.\n", result) | |
else: | |
return (f"""Ho trovato {len(result)} {"frasi" if len(result) > 1 else "frase"} in cui è presente la parola {target}.\n""", result) | |
with gr.Blocks() as demo: | |
gr.Markdown("Inerisci la parola da cercare.") | |
with gr.Row(): | |
target = gr.Textbox(placeholder="What is your name?") | |
out = gr.Textbox() | |
btn = gr.Button("Run") | |
btn.click(fn=search_engine_bot, inputs=target, outputs=out) | |
demo.launch() | |
file = "text.txt" | |
def get_lists(file): | |
with open(file, 'r', encoding='utf-8') as f: | |
text = f.read() | |
word_tokenized_text = word_tokenize(text, language='italian') | |
word_tokenized_text_lower = [word.lower() for word in word_tokenized_text] | |
sent_tokenized_text = sent_tokenize(text, language='italian') | |
sent_tokenized_text_lower = [sent.lower() for sent in sent_tokenized_text] | |
return word_tokenized_text, word_tokenized_text_lower, sent_tokenized_text, sent_tokenized_text_lower | |
words, words_lower, SENTENCES, SENTENCES_LOWER = get_lists(file) | |
def num_sentences(show=''): | |
try: | |
number = int(show) | |
return number | |
except: | |
number = '' | |
return number | |
def show_results(result, number): | |
display = [] | |
try: | |
for num,sent in enumerate(result[1][:int(number)]): | |
display.append(f"{num+1}: {sent}\n") | |
return display | |
except: | |
for num,sent in enumerate(result): | |
display.append(f"{num+1}: {sent}") | |
return display | |
def search_engine(parola_da_cercare): | |
sentences_lower = sentences_lower | |
sentences = sentences | |
target= parola_da_cercare #input("Inserisci una o più parole da cercare.\n") | |
#target= input("Inserisci un'altra parola da cercare.") #input("Inserisci un'altra parola da cercare.\n") | |
result = [] | |
for i,sent in enumerate(sentences_lower): | |
if target.lower() in sent: | |
result.append(sentences[i]) | |
if len(result) == 0: | |
return(f"Non ho trovato la parola '{target}' nei testi.\n") | |
#search_engine(round=True) | |
else: | |
return(f"""Ho trovato {len(result)} {"frasi" if len(result) > 1 else "frase"} in cui è presente la parola {target}.\n""") | |
show = gr.Number(label="Quante frasi vuoi vedere? Scrivi un numero oppure 'tutte', se vuoi vederle tutte :-) \n", show_label=True) | |
try: | |
for num,sent in enumerate(result[:int(show)]): | |
print(f"{num+1}: {sent}") | |
except: | |
for num,sent in enumerate(result): | |
print(f"{num+1}: {sent}") | |
def sentence_builder(cerca_una_parola, place, activity_list, morning): | |
return f"""The {cerca_una_parola}s went to the {place} where they {" and ".join(activity_list)} until the {"morning" if morning else "night"}""" | |
demo = gr.Interface(search_engine, inputs="text", outputs="text") | |
demo = gr.Interface( | |
sentence_builder, | |
[ | |
gr.Textbox(), | |
gr.Radio(["park", "zoo", "road"]), | |
gr.CheckboxGroup(["ran", "swam", "ate", "slept"]), | |
gr.Checkbox(label="Is it the morning?"), | |
], | |
"text", | |
examples=[ | |
["cats", "park", ["ran", "swam"], True], | |
["dog", "zoo", ["ate", "swam"], False], | |
["bird", "road", ["ran"], False], | |
["cat", "zoo", ["ate"], True], | |
], | |
) | |
if __name__ == "__main__": | |
demo.launch() | |
''' |