ferdaouskachouri commited on
Commit
942415c
·
verified ·
1 Parent(s): a38685d

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +57 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch, torch.nn as nn, json, io
3
+ from torchvision import models, transforms
4
+ from huggingface_hub import hf_hub_download
5
+ from PIL import Image
6
+
7
+ HF_REPO_ID = "ferdaouskachouri/financial_classification"
8
+ SECRET_KEY = "mon-api-key-secret-2004" # ← change par ce que tu veux
9
+
10
+ with open(hf_hub_download(HF_REPO_ID, "config.json")) as f:
11
+ config = json.load(f)
12
+
13
+ m = models.efficientnet_b0(weights=None)
14
+ in_f = m.classifier[1].in_features
15
+ m.classifier = nn.Sequential(
16
+ nn.Dropout(0.4), nn.Linear(in_f, 256),
17
+ nn.ReLU(), nn.Dropout(0.2),
18
+ nn.Linear(256, config["num_classes"])
19
+ )
20
+ m.load_state_dict(torch.load(
21
+ hf_hub_download(HF_REPO_ID, "pytorch_model.bin"),
22
+ map_location="cpu"
23
+ ))
24
+ m.eval()
25
+
26
+ transform = transforms.Compose([
27
+ transforms.Resize((224, 224)),
28
+ transforms.ToTensor(),
29
+ transforms.Normalize(config["normalize_mean"], config["normalize_std"]),
30
+ ])
31
+
32
+ def predict(image, api_key):
33
+ if api_key != SECRET_KEY:
34
+ return "❌ Clé API invalide", {}
35
+ tensor = transform(image).unsqueeze(0)
36
+ with torch.no_grad():
37
+ probs = torch.softmax(m(tensor), dim=1)[0]
38
+ top_idx = probs.argmax().item()
39
+ return config["classes"][top_idx], {
40
+ cls: round(probs[i].item(), 4)
41
+ for i, cls in enumerate(config["classes"])
42
+ }
43
+
44
+ demo = gr.Interface(
45
+ fn=predict,
46
+ inputs=[
47
+ gr.Image(type="pil", label="Document"),
48
+ gr.Textbox(label="Clé API", type="password")
49
+ ],
50
+ outputs=[
51
+ gr.Textbox(label="Classe prédite"),
52
+ gr.JSON(label="Probabilités")
53
+ ],
54
+ title="Document Classifier"
55
+ )
56
+
57
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ torch
2
+ torchvision
3
+ huggingface_hub
4
+ pillow
5
+ gradio