Unit4IAI commited on
Commit
98d37aa
·
verified ·
1 Parent(s): 4c6d3b2

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +7 -7
  2. app.py +89 -0
  3. requirements.txt +10 -0
README.md CHANGED
@@ -1,13 +1,13 @@
1
  ---
2
- title: Fuse MD
3
- emoji: 🐢
4
- colorFrom: green
5
- colorTo: blue
6
  sdk: gradio
7
- sdk_version: 6.24.0
8
- python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
1
  ---
2
+ title: Fuse-MD
3
+ emoji: 🔎
4
+ colorFrom: blue
5
+ colorTo: purple
6
  sdk: gradio
 
 
7
  app_file: app.py
8
  pinned: false
9
  ---
10
 
11
+ # Fuse-MD
12
+
13
+ Research demo for multimodal misogyny-meme detection in Malayalam.
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Fuse-MD Hugging Face ZeroGPU demo.
2
+
3
+ The Space must also contain the ``src`` and ``api`` folders and the selected
4
+ checkpoint at the repository root. The large text backbone is downloaded from
5
+ the Hugging Face Hub during startup.
6
+ """
7
+
8
+ import os
9
+ import sys
10
+ import time
11
+ from pathlib import Path
12
+
13
+ try:
14
+ import spaces
15
+ GPU = spaces.GPU
16
+ except ImportError: # pragma: no cover - local-only fallback
17
+ def GPU(*args, **kwargs):
18
+ def decorate(function):
19
+ return function
20
+ return decorate
21
+
22
+ import gradio as gr
23
+ from huggingface_hub import snapshot_download
24
+
25
+
26
+ ROOT = Path(__file__).resolve().parent
27
+ sys.path.insert(0, str(ROOT))
28
+
29
+ TEXT_MODEL = "VishnuPJ/MalayaLLM_7B_Base"
30
+ CHECKPOINT = ROOT / "custom_malayalam_llamavit_fusion_element.pth"
31
+ MODEL_DIR = ROOT / ".hf_model_cache" / "VishnuPJ__MalayaLLM_7B_Base"
32
+
33
+ # Download one text backbone only. Keeping a single language model reduces
34
+ # startup time and storage usage for the free demo.
35
+ if not MODEL_DIR.exists():
36
+ snapshot_download(repo_id=TEXT_MODEL, local_dir=MODEL_DIR)
37
+
38
+ os.environ.setdefault("FUSEMD_CHECKPOINT", str(CHECKPOINT))
39
+ os.environ.setdefault("FUSEMD_LOCAL_MODEL_ROOT", str(ROOT / ".hf_model_cache"))
40
+ os.environ.setdefault("FUSEMD_DEVICE", "cuda")
41
+
42
+ from api.api.config import settings # noqa: E402
43
+ from api.api.service import FuseMDService # noqa: E402
44
+
45
+
46
+ service = FuseMDService(settings)
47
+ service.load()
48
+
49
+
50
+ @GPU(duration=120)
51
+ def predict(image_path, text, threshold):
52
+ if image_path is None:
53
+ return "Please upload a meme image."
54
+
55
+ started = time.perf_counter()
56
+ with open(image_path, "rb") as image_file:
57
+ response = service.predict(
58
+ text=text or "",
59
+ image_bytes=image_file.read(),
60
+ image_filename=Path(image_path).name,
61
+ threshold=threshold,
62
+ )
63
+
64
+ elapsed = time.perf_counter() - started
65
+ return (
66
+ f"### Prediction: **{response['label']}**\n\n"
67
+ f"Positive probability: **{response['probability']:.1%}**\n\n"
68
+ f"Threshold: {response['threshold']:.2f} | "
69
+ f"Language: {response['language']} | Fusion: {response['fusion_method']}\n\n"
70
+ f"Runtime: **{elapsed:.1f} seconds**"
71
+ )
72
+
73
+
74
+ with gr.Blocks(title="Fuse-MD Demo") as demo:
75
+ gr.Markdown(
76
+ "# Fuse-MD\n"
77
+ "Upload a Malayalam meme and enter its transcription. The model predicts "
78
+ "whether it contains misogynistic content."
79
+ )
80
+ with gr.Row():
81
+ image = gr.Image(type="filepath", label="Meme image", width=420, height=420)
82
+ text = gr.Textbox(label="Meme transcription", lines=5)
83
+ threshold = gr.Slider(0.0, 1.0, value=0.5, step=0.05, label="Decision threshold")
84
+ button = gr.Button("Predict", variant="primary")
85
+ result = gr.Markdown()
86
+ button.click(predict, inputs=[image, text, threshold], outputs=result)
87
+
88
+
89
+ demo.queue().launch()
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ spaces
2
+ gradio
3
+ huggingface_hub
4
+ transformers
5
+ accelerate
6
+ bitsandbytes
7
+ timm
8
+ opencv-python-headless
9
+ fastapi
10
+ python-multipart