Zolisa commited on
Commit
eeaeeec
·
verified ·
1 Parent(s): 988f5b2

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torchvision.transforms as transforms
4
+ from PIL import Image
5
+ import gradio as gr
6
+ import sys
7
+ import os
8
+
9
+ # Add current directory to path for local imports
10
+ sys.path.append(os.path.abspath('.'))
11
+
12
+ from models.simple_nn import SimpleCNN
13
+ from models.transformer_imdb import TransformerClassifier
14
+
15
+ # Constants
16
+ DEVICE = torch.device('cpu')
17
+ MNIST_PATH = 'models/cnn_model.pth'
18
+ FASHION_PATH = 'models/fashion_cnn_model.pth'
19
+ IMDB_PATH = 'models/transformer_imdb.pth'
20
+ VOCAB_SIZE = 100684
21
+
22
+ # 1. Helper functions to load models
23
+ def load_image_model(model_type):
24
+ model = SimpleCNN().to(DEVICE)
25
+ path = MNIST_PATH if 'MNIST' in model_type else FASHION_PATH
26
+ if os.path.exists(path):
27
+ model.load_state_dict(torch.load(path, map_location=DEVICE))
28
+ model.eval()
29
+ return model
30
+
31
+ def load_text_model():
32
+ model = TransformerClassifier(vocab_size=VOCAB_SIZE).to(DEVICE)
33
+ if os.path.exists(IMDB_PATH):
34
+ model.load_state_dict(torch.load(IMDB_PATH, map_location=DEVICE))
35
+ model.eval()
36
+ return model
37
+
38
+ # 2. Prediction functions
39
+ def predict_image(image, model_type):
40
+ if image is None: return "No image uploaded."
41
+ model = load_image_model(model_type)
42
+ transform = transforms.Compose([
43
+ transforms.Grayscale(num_output_channels=1),
44
+ transforms.Resize((28, 28)),
45
+ transforms.ToTensor(),
46
+ transforms.Normalize((0.1307,), (0.3081,))
47
+ ])
48
+ input_tensor = transform(image).unsqueeze(0).to(DEVICE)
49
+ with torch.no_grad():
50
+ output = model(input_tensor)
51
+ idx = output.argmax(dim=1).item()
52
+
53
+ if 'Digits' in model_type:
54
+ return f"Predicted Digit: {idx}"
55
+ else:
56
+ labels = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
57
+ return f"Predicted Clothing: {labels[idx]}"
58
+
59
+ def predict_sentiment(text):
60
+ text_l = text.lower()
61
+ pos = ['great', 'good', 'excellent', 'love', 'wonderful']
62
+ neg = ['bad', 'terrible', 'awful', 'waste', 'boring']
63
+ score = sum(1 for w in pos if w in text_l) - sum(1 for w in neg if w in text_l)
64
+ if score > 0: return "Positive"
65
+ elif score < 0: return "Negative"
66
+ return "Neutral"
67
+
68
+ # 3. Create Interfaces
69
+ img_iface = gr.Interface(
70
+ fn=predict_image,
71
+ inputs=[gr.Image(type='pil'), gr.Dropdown(['MNIST (Digits)', 'FashionMNIST (Clothing)'], label='Dataset')],
72
+ outputs='text',
73
+ title='Computer Vision Classification'
74
+ )
75
+
76
+ txt_iface = gr.Interface(
77
+ fn=predict_sentiment,
78
+ inputs=gr.Textbox(lines=3, placeholder='Enter review...'),
79
+ outputs='text',
80
+ title='NLP Sentiment Analysis'
81
+ )
82
+
83
+ # 4. Combine into TabbedInterface
84
+ app = gr.TabbedInterface([img_iface, txt_iface], ['Images', 'Sentiment'], title='Multi-Modal PyTorch App')
85
+
86
+ if __name__ == '__main__':
87
+ app.launch()