Gilvan commited on
Commit
e62171b
·
verified ·
1 Parent(s): 3759c11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -11
app.py CHANGED
@@ -1,10 +1,21 @@
 
 
1
  import torch
2
  from transformers import pipeline
3
  import gradio as gr
4
 
5
- # Carregar o modelo
 
 
 
 
 
 
 
 
6
  model_id = "meta-llama/Llama-3.2-1B-Instruct"
7
 
 
8
  pipe = pipeline(
9
  "text-classification",
10
  model=model_id,
@@ -12,22 +23,22 @@ pipe = pipeline(
12
  device_map="auto"
13
  )
14
 
15
- # Definir os rótulos das classes
16
  pipe.model.config.id2label = {0: 'greeting', 1: 'farewell', 2: 'other'}
17
 
18
- # Função para usar o modelo e retornar o resultado
19
  def classify_text(text):
20
  result = pipe(text)
21
  return result[0]['label']
22
 
23
- # Interface Gradio
24
  iface = gr.Interface(
25
- fn=classify_text, # Função que será chamada
26
- inputs=gr.Textbox(label="Texto"), # Caixa de texto para o usuário inserir a entrada
27
- outputs=gr.Label(label="Classificação"), # Exibição da classificação
28
- title="Classificador de Texto", # Título do app
29
- description="Este modelo classifica o texto em três categorias: 'greeting', 'farewell', e 'other'."
30
  )
31
 
32
- # Iniciar o app Gradio
33
- iface.launch()
 
1
+ import os
2
+ from huggingface_hub import login
3
  import torch
4
  from transformers import pipeline
5
  import gradio as gr
6
 
7
+ # Set the Hugging Face token from the environment variable
8
+ hf_token = os.getenv("HF_TOKEN")
9
+ if hf_token is None:
10
+ raise ValueError("Hugging Face token is not set in the environment variable.")
11
+
12
+ # Log in to Hugging Face with the token
13
+ login(token=hf_token)
14
+
15
+ # Define the model ID
16
  model_id = "meta-llama/Llama-3.2-1B-Instruct"
17
 
18
+ # Load the pipeline with the model
19
  pipe = pipeline(
20
  "text-classification",
21
  model=model_id,
 
23
  device_map="auto"
24
  )
25
 
26
+ # Define custom labels for classification
27
  pipe.model.config.id2label = {0: 'greeting', 1: 'farewell', 2: 'other'}
28
 
29
+ # Function to classify input text
30
  def classify_text(text):
31
  result = pipe(text)
32
  return result[0]['label']
33
 
34
+ # Create Gradio interface
35
  iface = gr.Interface(
36
+ fn=classify_text, # Function to be called
37
+ inputs=gr.Textbox(label="Enter Text"), # Textbox input for user
38
+ outputs=gr.Label(label="Classification"), # Output label showing classification
39
+ title="Text Classifier", # Title of the app
40
+ description="Classify your text as 'greeting', 'farewell', or 'other'." # Description of the app
41
  )
42
 
43
+ # Launch the Gradio app
44
+ iface.launch()