franzemil commited on
Commit
da0adc8
·
1 Parent(s): 4a4cab1

Update app.py

Browse files

Add the inference

Files changed (1) hide show
  1. app.py +28 -1
app.py CHANGED
@@ -1,3 +1,30 @@
1
  import gradio as gr
 
2
 
3
- gr.Interface.load("models/franzemil/bolivianlm").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, GPT2Tokenizer
3
 
4
+
5
+ def generate_text(sentence, max_length=100):
6
+ model_path = "models/franzemil/bolivianlm"
7
+ model_name = "datificate/gpt2-small-spanish"
8
+
9
+ # Load the model and the tokenizer
10
+ model = AutoModelForCausalLM.from_pretrained(model_path)
11
+ tokenizer = GPT2Tokenizer.from_pretrained(model_name)
12
+
13
+ # Generate the ids using the tokenizer
14
+ ids = tokenizer.encode(sentence, return_tensors="pt")
15
+
16
+ # Use the model to generate text
17
+ outputs = model.generate(
18
+ ids,
19
+ do_sample=True,
20
+ max_length=max_length,
21
+ pad_token_id=model.config.eos_token_id,
22
+ top_k=50,
23
+ top_p=0.95,
24
+ )
25
+
26
+ # Decode the and return the string
27
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
28
+
29
+ demo = gr.Interface(fn=generate_text, inputs="text", outputs="text")
30
+ demo.launch()