13ze commited on
Commit
fe16c68
·
verified ·
1 Parent(s): 45a4184

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -16
app.py CHANGED
@@ -1,24 +1,38 @@
1
- import os
2
  import gradio as gr
3
- from transformers import AutoTokenizer, AutoModelForCausalLM
 
4
 
5
- # Obter o token de autenticação a partir do Secret
6
- HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
7
 
8
- # Definir o modelo e o tokenizador
9
- model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
10
- tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=HUGGINGFACE_TOKEN, trust_remote_code=True)
11
- model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=HUGGINGFACE_TOKEN, trust_remote_code=True)
12
 
13
- def generate_text(prompt):
14
- inputs = tokenizer(prompt, return_tensors="pt")
15
- outputs = model.generate(**inputs, max_length=200)
16
- generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
17
- return generated_text
 
 
 
 
 
 
 
 
 
 
18
 
19
- # Criar a interface Gradio
20
- iface = gr.Interface(fn=generate_text, inputs="text", outputs="text", live=True, title="Geração de Texto com Mixtral")
 
 
 
 
 
 
21
 
22
- # Executar o app
23
  if __name__ == "__main__":
24
  iface.launch()
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
 
5
+ checkpoint = "jinaai/reader-lm-0.5b"
6
+ device = "cuda" if torch.cuda.is_available() else "cpu"
7
 
8
+ # Carrega modelo e tokenizer
9
+ tokenizer = AutoTokenizer.from_pretrained(checkpoint)
10
+ model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
 
11
 
12
+ def process_html(html_content):
13
+ messages = [{"role": "user", "content": html_content}]
14
+ input_text = tokenizer.apply_chat_template(messages, tokenize=False)
15
+
16
+ inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
17
+ outputs = model.generate(
18
+ inputs,
19
+ max_new_tokens=1024,
20
+ temperature=0,
21
+ do_sample=False,
22
+ repetition_penalty=1.08
23
+ )
24
+
25
+ decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
26
+ return decoded_output
27
 
28
+ # Interface Gradio
29
+ iface = gr.Interface(
30
+ fn=process_html,
31
+ inputs=gr.Textbox(lines=10, placeholder="Insira o conteúdo HTML aqui...", label="HTML"),
32
+ outputs=gr.Textbox(label="Resposta do modelo"),
33
+ title="HTML Reader (jinaai/reader-lm-0.5b)",
34
+ description="Insira um conteúdo HTML e veja como o modelo interpreta o conteúdo."
35
+ )
36
 
 
37
  if __name__ == "__main__":
38
  iface.launch()