Merlintxu commited on
Commit
c7be05a
1 Parent(s): be336e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -46
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
- nlp = spacy.load('es_core_news_sm')
7
- text_generator = pipeline('text-generation', model='gpt2')
8
-
9
- pos_tags = ['ADJ', 'ADP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'NOUN', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNCT', 'SCONJ', 'SYM', 'VERB', 'X']
10
- pos_tags_es = ['ADJ', 'PREP', 'ADV', 'AUX', 'CONJ', 'DET', 'INTJ', 'SUST', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNCT', 'SCONJ', 'SYM', 'VERBO', 'X']
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
- # The game logic
31
- def game_flow(start_game, *args):
32
- if start_game == 'Empezar':
33
- sentence, _, _ = generate_and_analyze()
34
- return sentence, '', '', ''
35
- else:
36
- correct_answer = [tag for _, tag in sentence_state['tagged_words_es']]
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
- iface = gr.Interface(fn=game_flow,
45
- inputs=[gr.inputs.Textbox(label='Escribe "Empezar" para iniciar el juego')] +
46
- [gr.inputs.Dropdown(choices=pos_tags_es, label=f'Funci贸n de la palabra {i+1}') for i in range(5)],
47
- outputs=[gr.outputs.Textbox(label='Frase generada'),
48
- gr.outputs.Textbox(label='Funciones de las palabras'),
49
- gr.outputs.Textbox(label='Evaluaci贸n'),
50
- gr.outputs.Textbox(label='Correcci贸n')])
 
 
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()