Merlintxu commited on
Commit
aab4852
·
1 Parent(s): 0344802

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -23
app.py CHANGED
@@ -1,27 +1,24 @@
1
  ## app.py ##
2
  import gradio as gr
3
  import spacy
4
- import random
5
  from transformers import pipeline
 
 
6
 
7
  nlp = spacy.load('es_core_news_sm')
8
  text_generator = pipeline('text-generation', model='gpt2')
9
 
10
  pos_tags = ['ADJ', 'ADP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'NOUN', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNCT', 'SCONJ', 'SYM', 'VERB', 'X']
11
- pos_tags_es = ['ADJ', 'ADP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'NOMBRE', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNT', 'SCONJ', 'SYM', 'VERBO', 'X'] # Spanish version
12
- pos_explanation = { # Add explanations for each POS tag
13
- # Add your own explanations here
14
- }
15
 
16
  sentence_state = {'sentence': '', 'tagged_words': []}
17
 
18
  def generate_sentence():
19
- result = text_generator('Erase una vez', max_length=10)[0]
20
  sentence = result['generated_text']
21
  tagged_words = analyze_sentence(sentence)
22
  sentence_state['sentence'] = sentence
23
  sentence_state['tagged_words'] = tagged_words
24
- return sentence, random.sample(tagged_words, 5) # Only select 5 words to classify
25
 
26
  def analyze_sentence(sentence):
27
  doc = nlp(sentence)
@@ -31,23 +28,20 @@ def check_answer(*args):
31
  correct_answer = [tag for word, tag in sentence_state['tagged_words']]
32
  user_answer = list(args)
33
  if user_answer == correct_answer:
34
- return '¡Correcto!'
35
  else:
36
- return 'Incorrecto. La respuesta correcta es: ' + str(correct_answer) + '. ' + explain(correct_answer)
37
-
38
- def explain(tags):
39
- return ' '.join(pos_explanation[tag] for tag in tags)
40
 
41
  def game_flow(start_game):
42
- if start_game == 'Iniciar':
43
  sentence, words = generate_sentence()
44
- answer = check_answer(*gr.inputs[1:]) # Skip the first input
45
- return sentence, [word for word, _ in words], answer
46
-
47
- iface = gr.Interface(fn=game_flow,
48
- inputs=[gr.inputs.Textbox(label='Escribe "Iniciar" para generar una frase')] +
49
- [gr.inputs.Dropdown(choices=pos_tags_es, label=f'Palabra {i+1}') for i in range(5)], # Only 5 words to classify
50
- outputs=[gr.outputs.Textbox(label='Frase'),
51
- gr.outputs.Textbox(label='Palabras'),
52
- gr.outputs.Textbox(label='Resultado')])
53
- iface
 
1
  ## app.py ##
2
  import gradio as gr
3
  import spacy
 
4
  from transformers import pipeline
5
+ from gradio import Interface
6
+ from gradio.components import Textbox, Dropdown
7
 
8
  nlp = spacy.load('es_core_news_sm')
9
  text_generator = pipeline('text-generation', model='gpt2')
10
 
11
  pos_tags = ['ADJ', 'ADP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'NOUN', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNCT', 'SCONJ', 'SYM', 'VERB', 'X']
 
 
 
 
12
 
13
  sentence_state = {'sentence': '', 'tagged_words': []}
14
 
15
  def generate_sentence():
16
+ result = text_generator('', max_length=10)[0] # Limiting max_length for simplicity
17
  sentence = result['generated_text']
18
  tagged_words = analyze_sentence(sentence)
19
  sentence_state['sentence'] = sentence
20
  sentence_state['tagged_words'] = tagged_words
21
+ return sentence, [word for word, _ in tagged_words]
22
 
23
  def analyze_sentence(sentence):
24
  doc = nlp(sentence)
 
28
  correct_answer = [tag for word, tag in sentence_state['tagged_words']]
29
  user_answer = list(args)
30
  if user_answer == correct_answer:
31
+ return 'Correcto!'
32
  else:
33
+ return 'Incorrecto. La respuesta correcta es: ' + str(correct_answer)
 
 
 
34
 
35
  def game_flow(start_game):
36
+ if start_game == 'Start':
37
  sentence, words = generate_sentence()
38
+ answer = check_answer(*gr.inputs)
39
+ return sentence, words, answer
40
+
41
+ iface = Interface(fn=game_flow,
42
+ inputs=[Textbox(label='Type "Start" to generate sentence')] +
43
+ [Dropdown(choices=pos_tags, label=f'Word {i+1}') for i in range(10)], # Assumes sentences of 10 words
44
+ outputs=[Textbox(label='Sentence'),
45
+ Textbox(label='Words'),
46
+ Textbox(label='Result')])
47
+ iface.launch()