Chhagan005 commited on
Commit
93307ce
·
verified ·
1 Parent(s): 22b213b

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -1,5 +1,9 @@
1
 
2
  import os
 
 
 
 
3
  import gradio as gr
4
  import torch
5
  import torch.nn as nn
@@ -8,7 +12,7 @@ from huggingface_hub import hf_hub_download
8
  import json
9
  import string
10
 
11
- MAX_SEQ_LEN = 2000 # Updated for Phase 3 large JSON
12
 
13
  class CSMTokenizer:
14
  def __init__(self):
@@ -21,7 +25,6 @@ class CSMTokenizer:
21
  def decode(self, tokens):
22
  return "".join([self.inverse_vocab.get(t, "") for t in tokens if t not in [self.PAD, self.SOS, self.EOS]])
23
 
24
- # UPGRADED ARCHITECTURE MATCHING PHASE 3
25
  class CSMVisionEncoder(nn.Module):
26
  def __init__(self, embed_dim=512):
27
  super().__init__()
@@ -57,7 +60,7 @@ class CSMNativeModel(nn.Module):
57
  self.decoder = CSMTextDecoder(vocab_size)
58
 
59
  tokenizer = CSMTokenizer()
60
- device = torch.device("cpu") # Force CPU for free spaces
61
 
62
  print("Downloading Final Production Model Phase 3...")
63
  HF_SECURE_TOKEN = os.environ.get("HF_TOKEN")
@@ -65,7 +68,6 @@ HF_SECURE_TOKEN = os.environ.get("HF_TOKEN")
65
  model_path = hf_hub_download(repo_id="Chhagan005/CSM-KIE-Universal", filename="csm_kie_model.pth", token=HF_SECURE_TOKEN)
66
  model = CSMNativeModel(tokenizer.vocab_size)
67
 
68
- # Apply quantization structure to match the loaded weights
69
  import torch.ao.quantization
70
  model = torch.ao.quantization.quantize_dynamic(model, {nn.Linear, nn.Conv2d}, dtype=torch.qint8)
71
  model.load_state_dict(torch.load(model_path, map_location=device))
@@ -104,7 +106,7 @@ def process_id_card(front_img, back_img):
104
 
105
  with gr.Blocks() as demo:
106
  gr.Markdown("# 🪪 CSM-KIE Master VLM Scanner")
107
- gr.Markdown("Production Mode: Phase 3 Foundation Architecture. Extracts fully structured dynamic JSON data from International ID cards (Middle East, Africa, etc.)")
108
 
109
  with gr.Row():
110
  with gr.Column():
@@ -117,4 +119,6 @@ with gr.Blocks() as demo:
117
 
118
  scan_btn.click(process_id_card, inputs=[front, back], outputs=output_json)
119
 
120
- demo.launch(theme="soft")
 
 
 
1
 
2
  import os
3
+ import warnings
4
+ # Hide annoying PyTorch deprecation warnings
5
+ warnings.filterwarnings("ignore")
6
+
7
  import gradio as gr
8
  import torch
9
  import torch.nn as nn
 
12
  import json
13
  import string
14
 
15
+ MAX_SEQ_LEN = 2000
16
 
17
  class CSMTokenizer:
18
  def __init__(self):
 
25
  def decode(self, tokens):
26
  return "".join([self.inverse_vocab.get(t, "") for t in tokens if t not in [self.PAD, self.SOS, self.EOS]])
27
 
 
28
  class CSMVisionEncoder(nn.Module):
29
  def __init__(self, embed_dim=512):
30
  super().__init__()
 
60
  self.decoder = CSMTextDecoder(vocab_size)
61
 
62
  tokenizer = CSMTokenizer()
63
+ device = torch.device("cpu")
64
 
65
  print("Downloading Final Production Model Phase 3...")
66
  HF_SECURE_TOKEN = os.environ.get("HF_TOKEN")
 
68
  model_path = hf_hub_download(repo_id="Chhagan005/CSM-KIE-Universal", filename="csm_kie_model.pth", token=HF_SECURE_TOKEN)
69
  model = CSMNativeModel(tokenizer.vocab_size)
70
 
 
71
  import torch.ao.quantization
72
  model = torch.ao.quantization.quantize_dynamic(model, {nn.Linear, nn.Conv2d}, dtype=torch.qint8)
73
  model.load_state_dict(torch.load(model_path, map_location=device))
 
106
 
107
  with gr.Blocks() as demo:
108
  gr.Markdown("# 🪪 CSM-KIE Master VLM Scanner")
109
+ gr.Markdown("Production Mode: Phase 3 Foundation Architecture. Extracts fully structured dynamic JSON data from International ID cards.")
110
 
111
  with gr.Row():
112
  with gr.Column():
 
119
 
120
  scan_btn.click(process_id_card, inputs=[front, back], outputs=output_json)
121
 
122
+ # FIX: Forcing Port Binding for Hugging Face Spaces
123
+ if __name__ == "__main__":
124
+ demo.launch(server_name="0.0.0.0", server_port=7860)