Merlintxu commited on
Commit
af39961
1 Parent(s): 2e85b1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -36
app.py CHANGED
@@ -1,37 +1,45 @@
1
- ## app.py ##
2
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline,GPTNeoXForCausalLM, AutoConfig
3
- from gradio import Interface
4
  import gradio as gr
5
- from accelerate import init_empty_weights
6
- import json
7
- # Create a dictionary of models
8
- MODELS = {
9
- "T5": "lmsys/fastchat-t5-3b-v1.0",
10
- "LSpanishGPT2": "PlanTL-GOB-ES/gpt2-large-bne",
11
- "GPT2": "datificate/gpt2-small-spanish",
12
- "OpenAssistant": "OpenAssistant"
13
- }
14
-
15
- # Define your function
16
- def generate_and_analyze(model_name, input_text):
17
- model= MODELS[model_name]
18
- tokenizer = AutoTokenizer.from_pretrained(model)
19
- if model == "OpenAssistant":
20
- tokenizer = AutoTokenizer.from_pretrained("OpenAssistant")
21
- model = AutoModelForCausalLM.from_pretrained("OpenAssistant")
22
-
23
- TOKENIZERS[model_name] = tokenizer
24
- text_generator = pipeline('text-generation', model=model, tokenizer=tokenizer, device=0) # Use GPU if available
25
- result = text_generator(input_text, max_length=250, do_sample=True)[0]
26
- return result['generated_text']
27
-
28
- # Define your interface
29
- iface = gr.Interface(
30
- fn=generate_and_analyze,
31
- inputs=[
32
- gr.inputs.Dropdown(choices=list(MODELS.keys()), label="Model"),
33
- gr.inputs.Textbox(lines=2, label="Input Text")
34
- ],
35
- outputs="text"
36
- )
37
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
 
 
2
  import gradio as gr
3
+ import pygame
4
+
5
+ # Load the pre-trained Spanish language model using HuggingFace
6
+ nlp = pipeline("text2text-generation", model="datificate/gpt2-small-spanish")
7
+
8
+ # Define a function that generates a random Spanish sentence using the pre-trained model
9
+ def generate_sentence():
10
+ sentence = nlp("Genera una oraci贸n en espa帽ol")[0]["generated_text"]
11
+ return sentence
12
+
13
+ # Define a function that checks if the child identifies the verb in the sentence
14
+ def check_answer(sentence, verb):
15
+ words = sentence.split()
16
+ if verb in words:
17
+ return "隆Correcto! El verbo est谩 en la oraci贸n."
18
+ else:
19
+ return "Lo siento, el verbo no est谩 en la oraci贸n."
20
+
21
+ # Define a function that creates the chatbot interface using Gradio and adds fun animations or sound effects
22
+ def chatbot():
23
+ # Initialize pygame mixer for sound effects
24
+ pygame.mixer.init()
25
+
26
+ # Load sound effect
27
+ sound_effect = pygame.mixer.Sound("ding.wav")
28
+
29
+ # Generate a random sentence
30
+ sentence = generate_sentence()
31
+
32
+ # Ask the child to identify the verb in the sentence
33
+ verb = input("Identifica el verbo en la siguiente oraci贸n: " + sentence + "\n")
34
+
35
+ # Check if the child's answer is correct
36
+ result = check_answer(sentence, verb)
37
+
38
+ # Play sound effect if the answer is correct
39
+ if "Correcto" in result:
40
+ sound_effect.play()
41
+
42
+ return result
43
+
44
+ # Run the chatbot interface using Gradio
45
+ gr.Interface(fn=chatbot, inputs=None, outputs="text").launch()