Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,28 @@
|
|
1 |
-
## app.py ##
|
2 |
-
import gradio as gr
|
3 |
-
import spacy
|
4 |
from transformers import pipeline
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
# Store the sentence state globally
|
13 |
-
sentence_state = {'sentence': '', 'tagged_words': [], 'tagged_words_es': []}
|
14 |
-
|
15 |
-
# Generate a sentence and analyze it
|
16 |
-
def generate_and_analyze():
|
17 |
-
result = text_generator('', max_length=10, do_sample=True, language='es')[0]
|
18 |
-
sentence = result['generated_text']
|
19 |
-
doc = nlp(sentence)
|
20 |
-
tagged_words = [(token.text, token.pos_) for token in doc]
|
21 |
-
tagged_words_es = [(token.text, pos_tags_es[pos_tags.index(token.pos_)]) for token in doc]
|
22 |
-
sentence_state['sentence'] = sentence
|
23 |
-
sentence_state['tagged_words'] = tagged_words
|
24 |
-
sentence_state['tagged_words_es'] = tagged_words_es
|
25 |
-
return sentence, tagged_words, tagged_words_es
|
26 |
-
|
27 |
-
# Initialize the game
|
28 |
-
generate_and_analyze()
|
29 |
|
30 |
-
#
|
31 |
-
def
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
user_answer = list(args)
|
38 |
-
if user_answer == correct_answer:
|
39 |
-
return sentence_state['sentence'], ' '.join([f'{word} ({tag})' for word, tag in sentence_state['tagged_words_es']]), '隆Correcto!', ''
|
40 |
-
else:
|
41 |
-
correction = ' '.join([f'{word} es {tag}' for word, tag in sentence_state['tagged_words_es']])
|
42 |
-
return sentence_state['sentence'], ' '.join([f'{word} ({tag})' for word, tag in sentence_state['tagged_words_es']]), 'Incorrecto.', 'La respuesta correcta es: ' + correction
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
51 |
iface.launch()
|
|
|
|
|
|
|
|
|
1 |
from transformers import pipeline
|
2 |
+
from gradio import Interface
|
3 |
+
import gradio as gr
|
4 |
|
5 |
+
# Create a dictionary of models
|
6 |
+
MODELS = {
|
7 |
+
"GPT-2": "gpt2",
|
8 |
+
"mGPT": "ai-forever/mGPT"
|
9 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Define your function
|
12 |
+
def generate_and_analyze(model_name, input_text):
|
13 |
+
# Load the model from the dictionary using the selected model name
|
14 |
+
model = MODELS[model_name]
|
15 |
+
text_generator = pipeline('text-generation', model=model)
|
16 |
+
result = text_generator(input_text, max_length=10, do_sample=True)[0]
|
17 |
+
return result['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Define your interface
|
20 |
+
iface = gr.Interface(
|
21 |
+
fn=generate_and_analyze,
|
22 |
+
inputs=[
|
23 |
+
gr.inputs.Dropdown(choices=list(MODELS.keys()), label="Model"),
|
24 |
+
gr.inputs.Textbox(lines=2, label="Input Text")
|
25 |
+
],
|
26 |
+
outputs="text"
|
27 |
+
)
|
28 |
iface.launch()
|