albertmartinez commited on
Commit
2d05d8a
·
1 Parent(s): e8e36f1

Upgrade gradio

Browse files
Files changed (2) hide show
  1. app.py +124 -71
  2. requirements.txt +7 -3
app.py CHANGED
@@ -1,92 +1,145 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
 
 
3
 
4
- # Define the models
5
- model1 = pipeline("text-classification", model="albertmartinez/bert-sdg-classification")
6
- model2 = pipeline("text-classification", model="albertmartinez/bert-multilingual-sdg-classification")
7
- model3 = pipeline("text-classification", model="albertmartinez/distilbert-multilingual-sdg-classification")
8
- model4 = pipeline("text-classification", model="albertmartinez/xlm-roberta-large-sdg-classification")
9
 
 
 
 
 
 
 
 
 
 
10
 
11
- def classify_text(text, model):
12
- result = model(text, top_k=16, truncation=True, max_length=512)
13
- return {p["label"]: p["score"] for p in result}
 
14
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- def classify_all(text):
17
- return [
18
- {p["label"]: p["score"] for p in model1(text, top_k=16, truncation=True, max_length=512)},
19
- {p["label"]: p["score"] for p in model2(text, top_k=16, truncation=True, max_length=512)},
20
- {p["label"]: p["score"] for p in model3(text, top_k=16, truncation=True, max_length=512)},
21
- {p["label"]: p["score"] for p in model4(text, top_k=16, truncation=True, max_length=512)}
22
- ]
23
 
 
 
 
 
 
 
 
24
 
25
- ifaceall = gr.Interface(
26
- fn=classify_all,
27
- inputs=gr.Textbox(lines=2, label="Text", placeholder="Enter text here..."),
28
- outputs=[gr.Label(label="bert"), gr.Label(label="bert-multilingual"), gr.Label(label="distilbert-multilingual"),
29
- gr.Label(label="xlm-roberta-large")],
30
- title="SDG text classification",
31
- description="Enter a text and see the text classification result!",
32
- flagging_mode="never",
33
- api_name="classify_all"
34
- )
 
 
 
 
 
 
 
 
 
 
35
 
36
- # Interface for the first model
37
- iface1 = gr.Interface(
38
- fn=lambda text: classify_text(text, model1),
39
- inputs=gr.Textbox(lines=2, label="Text", placeholder="Enter text here..."),
40
- outputs=gr.Label(label="Top SDG Predicted"),
41
- title="BERT SDG classification",
42
- description="Enter a text and see the text classification result!",
43
- flagging_mode="never",
44
- api_name="classify_bert"
45
- )
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- # Interface for the second model
48
- iface2 = gr.Interface(
49
- fn=lambda text: classify_text(text, model2),
50
- inputs=gr.Textbox(lines=2, label="Text", placeholder="Enter text here..."),
51
- outputs=gr.Label(label="Top SDG Predicted"),
52
- title="BERT multilingual SDG classification",
53
- description="Enter a text and see the text classification result!",
54
- flagging_mode="never",
55
- api_name="classify_bert-multilingual"
56
- )
 
 
 
 
 
 
57
 
58
- # Interface for the three model
59
- iface3 = gr.Interface(
60
- fn=lambda text: classify_text(text, model3),
61
- inputs=gr.Textbox(lines=2, label="Text", placeholder="Enter text here..."),
62
- outputs=gr.Label(label="Top SDG Predicted"),
63
- title="DISTILBERT multilingual SDG classification",
64
- description="Enter a text and see the text classification result!",
 
 
 
 
 
 
 
 
 
 
65
  flagging_mode="never",
66
- api_name="classify_distilbert-multilingual"
67
  )
68
 
69
- # Interface for the four model
70
- iface4 = gr.Interface(
71
- fn=lambda text: classify_text(text, model4),
72
- inputs=gr.Textbox(lines=2, label="Text", placeholder="Enter text here..."),
73
- outputs=gr.Label(label="Top SDG Predicted"),
74
- title="XLM-ROBERTA-LARGE SDG classification",
75
- description="Enter a text and see the text classification result!",
76
- flagging_mode="never",
77
- api_name="classify_xlm-roberta-large"
78
- )
79
 
80
- with gr.Blocks() as demo:
81
- # Combine both interfaces into a tabbed interface
82
  gr.TabbedInterface(
83
- interface_list=[ifaceall, iface1, iface2, iface3, iface4],
84
- tab_names=["ALL", "bert-sdg-classification", "bert-multilingual-sdg-classification",
85
- "distilbert-multilingual-sdg-classification", "xlm-roberta-large-sdg-classification"],
86
- title="Sustainable Development Goals (SDG) Text Classifier App",
87
- theme='base'
88
  )
89
 
90
  if __name__ == "__main__":
91
- print(gr.__version__)
92
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ from typing import Dict, List, Union
4
+ import logging
5
+ from pydantic import BaseModel, Field
6
 
7
+ # Logging configuration
8
+ logging.basicConfig(level=logging.INFO)
9
+ logger = logging.getLogger(__name__)
 
 
10
 
11
+ # Constants
12
+ MAX_LENGTH = 512
13
+ TOP_K = 16
14
+ MODELS = {
15
+ "bert": "albertmartinez/bert-sdg-classification",
16
+ "bert-multilingual": "albertmartinez/bert-multilingual-sdg-classification",
17
+ "distilbert-multilingual": "albertmartinez/distilbert-multilingual-sdg-classification",
18
+ "xlm-roberta-large": "albertmartinez/xlm-roberta-large-sdg-classification"
19
+ }
20
 
21
+ class ClassificationResult(BaseModel):
22
+ """Model for classification results."""
23
+ label: str = Field(..., description="Classification label")
24
+ score: float = Field(..., description="Classification score")
25
 
26
+ def load_models() -> Dict[str, pipeline]:
27
+ """Load all required models."""
28
+ try:
29
+ return {
30
+ name: pipeline("text-classification", model=model_path)
31
+ for name, model_path in MODELS.items()
32
+ }
33
+ except Exception as e:
34
+ logger.error(f"Error loading models: {str(e)}")
35
+ raise
36
 
37
+ # Load models
38
+ models = load_models()
 
 
 
 
 
39
 
40
+ def validate_input(text: str) -> bool:
41
+ """Validate input text."""
42
+ if not text or not isinstance(text, str):
43
+ return False
44
+ if len(text.strip()) == 0:
45
+ return False
46
+ return True
47
 
48
+ def classify_text(text: str, model: pipeline) -> Dict[str, float]:
49
+ """
50
+ Classify text using the specified model.
51
+
52
+ Args:
53
+ text: Text to classify
54
+ model: Classification model
55
+
56
+ Returns:
57
+ Dictionary with labels and their scores
58
+ """
59
+ if not validate_input(text):
60
+ raise ValueError("Invalid text input")
61
+
62
+ try:
63
+ result = model(text, top_k=TOP_K, truncation=True, max_length=MAX_LENGTH)
64
+ return {p["label"]: p["score"] for p in result}
65
+ except Exception as e:
66
+ logger.error(f"Error in classification: {str(e)}")
67
+ raise
68
 
69
+ def classify_all(text: str) -> List[Dict[str, float]]:
70
+ """
71
+ Classify text using all available models.
72
+
73
+ Args:
74
+ text: Text to classify
75
+
76
+ Returns:
77
+ List of dictionaries with predictions from each model
78
+ """
79
+ if not validate_input(text):
80
+ raise ValueError("Invalid text input")
81
+
82
+ try:
83
+ return [
84
+ classify_text(text, model)
85
+ for model in models.values()
86
+ ]
87
+ except Exception as e:
88
+ logger.error(f"Error in multiple classification: {str(e)}")
89
+ raise
90
 
91
+ def create_interface(model_name: str, model: pipeline) -> gr.Interface:
92
+ """Create a Gradio interface for a specific model."""
93
+ return gr.Interface(
94
+ fn=lambda text: classify_text(text, model),
95
+ inputs=gr.Textbox(
96
+ lines=2,
97
+ label="Text",
98
+ placeholder="Enter text here..."
99
+ ),
100
+ outputs=gr.Label(label="Top SDG Predicted"),
101
+ title=f"{model_name.upper()} SDG classification",
102
+ description="Enter text and see the classification results!",
103
+ examples=[["To respond to these new challenges PSV has been encouraged to develop strong links with business and enterprises, to cover remote areas and to cater for less affluent students. It analyses the impact of the dual sector universities in Australia, short cycle higher education in Scotland, three sub-sectors of Norway's tertiary education and the emerging non-university higher education in Italy, as well as the vocational education and training in Spain. It discusses the issues of transition, participation and collaboration of different types of post-secondary education and analysis the impact of policy changes."]],
104
+ flagging_mode="never",
105
+ api_name=f"classify_{model_name}"
106
+ )
107
 
108
+ # Create main interface
109
+ ifaceall = gr.Interface(
110
+ fn=classify_all,
111
+ inputs=gr.Textbox(
112
+ lines=2,
113
+ label="Text",
114
+ placeholder="Enter text here..."
115
+ ),
116
+ outputs=[
117
+ gr.Label(label="bert"),
118
+ gr.Label(label="bert-multilingual"),
119
+ gr.Label(label="distilbert-multilingual"),
120
+ gr.Label(label="xlm-roberta-large")
121
+ ],
122
+ title="SDG text classification",
123
+ description="Enter text and see the classification results!",
124
+ examples=[["To respond to these new challenges PSV has been encouraged to develop strong links with business and enterprises, to cover remote areas and to cater for less affluent students. It analyses the impact of the dual sector universities in Australia, short cycle higher education in Scotland, three sub-sectors of Norway's tertiary education and the emerging non-university higher education in Italy, as well as the vocational education and training in Spain. It discusses the issues of transition, participation and collaboration of different types of post-secondary education and analysis the impact of policy changes."]],
125
  flagging_mode="never",
126
+ api_name="classify_all_models"
127
  )
128
 
129
+ # Create individual interfaces
130
+ interfaces = [ifaceall] + [
131
+ create_interface(name, model)
132
+ for name, model in models.items()
133
+ ]
 
 
 
 
 
134
 
135
+ # Create main interface with tabs
136
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
137
  gr.TabbedInterface(
138
+ interface_list=interfaces,
139
+ tab_names=["ALL"] + list(MODELS.keys()),
140
+ title="Sustainable Development Goals (SDG) Text Classifier App"
 
 
141
  )
142
 
143
  if __name__ == "__main__":
144
+ logger.info(f"Gradio version: {gr.__version__}")
145
  demo.launch()
requirements.txt CHANGED
@@ -1,3 +1,7 @@
1
- transformers
2
- torch
3
- gradio
 
 
 
 
 
1
+ gradio==5.33.1
2
+ transformers>=4.30.0
3
+ torch>=2.0.0
4
+ numpy>=1.24.0
5
+ pydantic>=2.7.4,<3.0.0
6
+ fastapi==0.115.9
7
+ websockets>=10.0,<12.0