peterkros commited on
Commit
bc462e3
·
verified ·
1 Parent(s): caa1584

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -17
app.py CHANGED
@@ -1,29 +1,62 @@
1
- import gradio as gr
2
- from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
3
- import torch
 
 
 
 
4
 
5
  # Load model and tokenizer
6
  model_name = "peterkros/immunization-classification-model"
7
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
8
- tokenizer = AutoTokenizer.from_pretrained(model_name)
 
 
 
 
 
 
9
 
10
  # Define the pipeline
11
- classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
 
 
 
 
 
 
12
 
13
  def classify_text(text):
14
- # Get predictions
15
- predictions = classifier(text)
16
- return predictions
 
 
 
 
 
17
 
18
  # Create Gradio interface
19
- iface = gr.Interface(
20
- fn=classify_text,
21
- inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
22
- outputs=gr.JSON(),
23
- title="Text Classification with DistilBERT",
24
- description="Enter text to classify it using a DistilBERT model trained for text classification."
25
- )
 
 
 
 
 
 
26
 
27
  # Launch the app
28
  if __name__ == "__main__":
29
- iface.launch()
 
 
 
 
 
 
 
1
+ import logging
2
+
3
+ # Set up logging
4
+ logging.basicConfig(level=logging.INFO)
5
+ logger = logging.getLogger(__name__)
6
+
7
+ logger.info("Starting the script")
8
 
9
  # Load model and tokenizer
10
  model_name = "peterkros/immunization-classification-model"
11
+ try:
12
+ logger.info(f"Loading model from {model_name}")
13
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
14
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
15
+ logger.info("Model and tokenizer loaded successfully")
16
+ except Exception as e:
17
+ logger.error(f"Error loading model and tokenizer: {e}")
18
+ raise e
19
 
20
  # Define the pipeline
21
+ try:
22
+ logger.info("Setting up the pipeline")
23
+ classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
24
+ logger.info("Pipeline set up successfully")
25
+ except Exception as e:
26
+ logger.error(f"Error setting up the pipeline: {e}")
27
+ raise e
28
 
29
  def classify_text(text):
30
+ try:
31
+ logger.info(f"Classifying text: {text}")
32
+ predictions = classifier(text)
33
+ logger.info(f"Predictions: {predictions}")
34
+ return predictions
35
+ except Exception as e:
36
+ logger.error(f"Error classifying text: {e}")
37
+ return {"error": str(e)}
38
 
39
  # Create Gradio interface
40
+ try:
41
+ logger.info("Setting up Gradio interface")
42
+ iface = gr.Interface(
43
+ fn=classify_text,
44
+ inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
45
+ outputs=gr.JSON(),
46
+ title="Text Classification with DistilBERT",
47
+ description="Enter text to classify it using a DistilBERT model trained for text classification."
48
+ )
49
+ logger.info("Gradio interface set up successfully")
50
+ except Exception as e:
51
+ logger.error(f"Error setting up Gradio interface: {e}")
52
+ raise e
53
 
54
  # Launch the app
55
  if __name__ == "__main__":
56
+ try:
57
+ logger.info("Launching Gradio interface")
58
+ iface.launch()
59
+ logger.info("Gradio interface launched successfully")
60
+ except Exception as e:
61
+ logger.error(f"Error launching Gradio interface: {e}")
62
+ raise e