darisdzakwanhoesien commited on
Commit
69212be
·
verified ·
1 Parent(s): 6a101a8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
4
+
5
+ # List of your models (using repo IDs for easy loading)
6
+ MODELS = {
7
+ "econbert": "climatebert/econbert",
8
+ "controversy-classification": "climatebert/ClimateControversyBERT_classification",
9
+ "controversy-bert": "climatebert/ClimateControversyBert",
10
+ "netzero-reduction": "climatebert/netzero-reduction",
11
+ "transition-physical": "climatebert/transition-physical",
12
+ "renewable": "climatebert/renewable",
13
+ "climate-detector": "climatebert/distilroberta-base-climate-detector",
14
+ "climate-commitment": "climatebert/distilroberta-base-climate-commitment",
15
+ "climate-tcfd": "climatebert/distilroberta-base-climate-tcfd",
16
+ "climate-s": "climatebert/distilroberta-base-climate-s", # Adjust label if truncated
17
+ "climate-specificity": "climatebert/distilroberta-base-climate-specificity",
18
+ "climate-sentiment": "climatebert/distilroberta-base-climate-sentiment",
19
+ "environmental-claims": "climatebert/environmental-claims",
20
+ "climate-f": "climatebert/distilroberta-base-climate-f", # Adjust label if truncated
21
+ "climate-d-s": "climatebert/distilroberta-base-climate-d-s", # Adjust label if truncated
22
+ "climate-d": "climatebert/distilroberta-base-climate-d" # Adjust label if truncated
23
+ }
24
+
25
+ # Cache for loaded pipelines (lazy loading)
26
+ pipelines = {}
27
+
28
+ def load_model(model_key):
29
+ """Load pipeline for the selected model (text-classification assumed; adjust task if needed)."""
30
+ if model_key not in pipelines:
31
+ repo_id = MODELS[model_key]
32
+ device = 0 if torch.cuda.is_available() else -1 # GPU if available
33
+ pipelines[model_key] = pipeline(
34
+ "text-classification",
35
+ model=repo_id,
36
+ device=device,
37
+ torch_dtype=torch.float16 if device == 0 else None # Half-precision on GPU for memory savings
38
+ )
39
+ return pipelines[model_key]
40
+
41
+ def predict(model_key, text):
42
+ """Run inference on selected model."""
43
+ if not text.strip():
44
+ return "Please enter some text."
45
+
46
+ try:
47
+ model = load_model(model_key)
48
+ results = model(text)
49
+ # Format output (e.g., for classification: label + score)
50
+ formatted = "\n".join([f"{r['label']}: {r['score']:.2f}" for r in results])
51
+ return f"Predictions for '{text}':\n{formatted}"
52
+ except Exception as e:
53
+ return f"Error: {str(e)} (Check model card for task/input format)."
54
+
55
+ # Gradio interface
56
+ with gr.Blocks(title="ClimateBERT Multi-Model Demo") as demo:
57
+ gr.Markdown("# ClimateBERT Models Demo\nSelect a model and input text for climate-related analysis (e.g., sentiment, classification).")
58
+
59
+ with gr.Row():
60
+ model_dropdown = gr.Dropdown(
61
+ choices=list(MODELS.keys()),
62
+ label="Select Model",
63
+ value=list(MODELS.keys())[0]
64
+ )
65
+ text_input = gr.Textbox(
66
+ label="Input Text",
67
+ placeholder="E.g., 'Companies must reduce emissions to net zero by 2050.'",
68
+ lines=2
69
+ )
70
+
71
+ output = gr.Textbox(label="Output", lines=5)
72
+
73
+ predict_btn = gr.Button("Run Inference")
74
+ predict_btn.click(predict, inputs=[model_dropdown, text_input], outputs=output)
75
+
76
+ if __name__ == "__main__":
77
+ demo.launch(server_name="0.0.0.0", server_port=7860)