Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
import torch
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
model = None
|
| 7 |
+
|
| 8 |
+
@spaces.GPU
|
| 9 |
+
def predict_brain_response(video_path=None, audio_path=None, text_input=None):
|
| 10 |
+
"""Predict fMRI brain responses to video, audio, or text stimuli."""
|
| 11 |
+
global model
|
| 12 |
+
|
| 13 |
+
if model is None:
|
| 14 |
+
from transformers import AutoModel
|
| 15 |
+
model = AutoModel.from_pretrained("facebook/tribev2", trust_remote_code=True)
|
| 16 |
+
model = model.cuda()
|
| 17 |
+
model.eval()
|
| 18 |
+
|
| 19 |
+
results = {}
|
| 20 |
+
|
| 21 |
+
if text_input:
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
output = model.predict_text(text_input)
|
| 24 |
+
results["text_response"] = {
|
| 25 |
+
"num_brain_regions": int(output.shape[-1]) if hasattr(output, "shape") else "N/A",
|
| 26 |
+
"mean_activation": float(output.mean()) if hasattr(output, "mean") else "N/A",
|
| 27 |
+
"max_activation": float(output.max()) if hasattr(output, "max") else "N/A",
|
| 28 |
+
"summary": "Brain response predicted across cortical regions"
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
if video_path:
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
output = model.predict_video(video_path)
|
| 34 |
+
results["video_response"] = {
|
| 35 |
+
"num_brain_regions": int(output.shape[-1]) if hasattr(output, "shape") else "N/A",
|
| 36 |
+
"mean_activation": float(output.mean()) if hasattr(output, "mean") else "N/A",
|
| 37 |
+
"summary": "Visual + temporal cortex response predicted"
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
if audio_path:
|
| 41 |
+
with torch.no_grad():
|
| 42 |
+
output = model.predict_audio(audio_path)
|
| 43 |
+
results["audio_response"] = {
|
| 44 |
+
"num_brain_regions": int(output.shape[-1]) if hasattr(output, "shape") else "N/A",
|
| 45 |
+
"mean_activation": float(output.mean()) if hasattr(output, "mean") else "N/A",
|
| 46 |
+
"summary": "Auditory cortex response predicted"
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
return results
|
| 50 |
+
|
| 51 |
+
demo = gr.Interface(
|
| 52 |
+
fn=predict_brain_response,
|
| 53 |
+
inputs=[
|
| 54 |
+
gr.File(label="Video (optional)", type="filepath"),
|
| 55 |
+
gr.File(label="Audio (optional)", type="filepath"),
|
| 56 |
+
gr.Textbox(label="Text (optional)", placeholder="Enter text stimulus..."),
|
| 57 |
+
],
|
| 58 |
+
outputs=gr.JSON(label="Brain Response Prediction"),
|
| 59 |
+
title="TRIBE V2 - Brain Response Prediction (Meta)",
|
| 60 |
+
description="Predicts fMRI brain responses to video, audio, and text stimuli using Meta TRIBE V2 model.",
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
demo.queue().launch()
|