Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,24 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
-
from huggingface_hub import login
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
HUGGINGFACE_TOKEN =
|
| 7 |
-
login(token=HUGGINGFACE_TOKEN)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 11 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 12 |
-
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True)
|
| 13 |
|
| 14 |
def generate_text(prompt):
|
| 15 |
-
# Tokenizar o texto de entrada
|
| 16 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 17 |
-
|
| 18 |
-
# Gerar a resposta com o modelo
|
| 19 |
outputs = model.generate(**inputs, max_length=200)
|
| 20 |
-
|
| 21 |
-
# Decodificar e retornar a resposta gerada
|
| 22 |
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
return generated_text
|
| 24 |
|
| 25 |
# Criar a interface Gradio
|
| 26 |
-
iface = gr.Interface(fn=generate_text,
|
| 27 |
-
inputs="text",
|
| 28 |
-
outputs="text",
|
| 29 |
-
live=True,
|
| 30 |
-
title="Geração de Texto com Mixtral")
|
| 31 |
|
|
|
|
| 32 |
if __name__ == "__main__":
|
| 33 |
iface.launch()
|
|
|
|
| 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()
|