Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,27 +3,6 @@
|
|
| 3 |
|
| 4 |
# #### Gradio Comparing Transfer Learning Models
|
| 5 |
|
| 6 |
-
# In[1]:
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
import tensorflow as tf
|
| 10 |
-
print(tf.__version__)
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# In[2]:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
pip install gradio
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# In[3]:
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
pip install MarkupSafe
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# In[1]:
|
| 26 |
-
|
| 27 |
|
| 28 |
import gradio as gr
|
| 29 |
import tensorflow as tf
|
|
@@ -93,154 +72,5 @@ gr.Interface(
|
|
| 93 |
examples=sample_images).launch()
|
| 94 |
|
| 95 |
|
| 96 |
-
# In[6]:
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
pip install transformers
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
# In[6]:
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
import gradio as gr
|
| 106 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 107 |
-
|
| 108 |
-
# Load the models and tokenizers
|
| 109 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 110 |
-
|
| 111 |
-
tokenizer1 = AutoTokenizer.from_pretrained("textattack/bert-base-uncased-imdb")
|
| 112 |
-
tokenizer2 = AutoTokenizer.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")
|
| 113 |
-
model1 = AutoModelForSequenceClassification.from_pretrained("textattack/bert-base-uncased-imdb")
|
| 114 |
-
model2 = AutoModelForSequenceClassification.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
# Define the sentiment prediction functions
|
| 120 |
-
def predict_sentiment(text):
|
| 121 |
-
# Predict sentiment using model 1
|
| 122 |
-
inputs1 = tokenizer1.encode_plus(text, padding="longest", truncation=True, return_tensors="pt")
|
| 123 |
-
outputs1 = model1(**inputs1)
|
| 124 |
-
predicted_label1 = outputs1.logits.argmax().item()
|
| 125 |
-
sentiment1 = "Positive" if predicted_label1 == 1 else "Negative" if predicted_label1 == 0 else "Neutral"
|
| 126 |
-
|
| 127 |
-
# Predict sentiment using model 2
|
| 128 |
-
inputs2 = tokenizer2.encode_plus(text, padding="longest", truncation=True, return_tensors="pt")
|
| 129 |
-
outputs2 = model2(**inputs2)
|
| 130 |
-
predicted_label2 = outputs2.logits.argmax().item()
|
| 131 |
-
sentiment2 = "Positive" if predicted_label2 == 1 else "Negative" if predicted_label2 == 0 else "Neutral"
|
| 132 |
-
|
| 133 |
-
return sentiment1, sentiment2
|
| 134 |
-
|
| 135 |
-
# Create the Gradio interface
|
| 136 |
-
iface = gr.Interface(
|
| 137 |
-
fn=predict_sentiment,
|
| 138 |
-
inputs="text",
|
| 139 |
-
outputs=["text", "text"],
|
| 140 |
-
title="Sentiment Analysis (Model 1 vs Model 2)",
|
| 141 |
-
description="Compare sentiment predictions from two models.",
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
# Launch the interface
|
| 145 |
-
iface.launch()
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
# In[17]:
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
import gradio as gr
|
| 152 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 153 |
-
import torch
|
| 154 |
-
from torchvision import transforms
|
| 155 |
-
from io import BytesIO
|
| 156 |
-
from PIL import Image
|
| 157 |
-
|
| 158 |
-
# Define the available models and datasets
|
| 159 |
-
models = {
|
| 160 |
-
"Model 1": {
|
| 161 |
-
"model_name": "bert-base-uncased",
|
| 162 |
-
"tokenizer": None,
|
| 163 |
-
"model": None
|
| 164 |
-
},
|
| 165 |
-
"Model 2": {
|
| 166 |
-
"model_name": "distilbert-base-uncased",
|
| 167 |
-
"tokenizer": None,
|
| 168 |
-
"model": None
|
| 169 |
-
},
|
| 170 |
-
# Add more models as needed
|
| 171 |
-
}
|
| 172 |
-
|
| 173 |
-
datasets = {
|
| 174 |
-
"Dataset 1": {
|
| 175 |
-
"name": "imdb",
|
| 176 |
-
"split": "test",
|
| 177 |
-
"features": ["text"],
|
| 178 |
-
},
|
| 179 |
-
"Dataset 2": {
|
| 180 |
-
"name": "ag_news",
|
| 181 |
-
"split": "test",
|
| 182 |
-
"features": ["text"],
|
| 183 |
-
},
|
| 184 |
-
# Add more datasets as needed
|
| 185 |
-
}
|
| 186 |
-
|
| 187 |
-
# Load models
|
| 188 |
-
for model_key, model_info in models.items():
|
| 189 |
-
tokenizer = AutoTokenizer.from_pretrained(model_info["model_name"])
|
| 190 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_info["model_name"])
|
| 191 |
-
model_info["tokenizer"] = tokenizer
|
| 192 |
-
model_info["model"] = model
|
| 193 |
-
|
| 194 |
-
# Set the device to GPU if available
|
| 195 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 196 |
-
for model_info in models.values():
|
| 197 |
-
model_info["model"].to(device)
|
| 198 |
-
|
| 199 |
-
# Define the preprocessing function
|
| 200 |
-
def preprocess(image_file):
|
| 201 |
-
image = Image.open(BytesIO(image_file.read())).convert("RGB")
|
| 202 |
-
preprocess_transform = transforms.Compose([
|
| 203 |
-
transforms.Resize((224, 224)),
|
| 204 |
-
transforms.ToTensor(),
|
| 205 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 206 |
-
])
|
| 207 |
-
image = preprocess_transform(image)
|
| 208 |
-
image = image.unsqueeze(0)
|
| 209 |
-
return image.to(device)
|
| 210 |
-
|
| 211 |
-
# Define the prediction function
|
| 212 |
-
def predict(image_file, model_key):
|
| 213 |
-
model_info = models[model_key]
|
| 214 |
-
tokenizer = model_info["tokenizer"]
|
| 215 |
-
model = model_info["model"]
|
| 216 |
-
|
| 217 |
-
image = preprocess(image_file)
|
| 218 |
-
|
| 219 |
-
with torch.no_grad():
|
| 220 |
-
outputs = model(image)
|
| 221 |
-
|
| 222 |
-
predictions = outputs.logits.argmax(dim=1)
|
| 223 |
-
|
| 224 |
-
return predictions.item()
|
| 225 |
-
|
| 226 |
-
def classify_image(image, model_key):
|
| 227 |
-
image = Image.fromarray(image.astype('uint8'), 'RGB')
|
| 228 |
-
image_file = BytesIO()
|
| 229 |
-
image.save(image_file, format="JPEG")
|
| 230 |
-
prediction = predict(image_file=image_file, model_key=model_key)
|
| 231 |
-
return prediction
|
| 232 |
-
|
| 233 |
-
iface = gr.Interface(fn=classify_image,
|
| 234 |
-
inputs=["image", gr.inputs.Dropdown(list(models.keys()), label="Model")],
|
| 235 |
-
outputs="text",
|
| 236 |
-
title="Image Classification",
|
| 237 |
-
description="Classify images using Hugging Face models")
|
| 238 |
-
|
| 239 |
-
iface.launch()
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
# In[ ]:
|
| 243 |
-
|
| 244 |
-
|
| 245 |
|
| 246 |
|
|
|
|
| 3 |
|
| 4 |
# #### Gradio Comparing Transfer Learning Models
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
import tensorflow as tf
|
|
|
|
| 72 |
examples=sample_images).launch()
|
| 73 |
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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