Spaces:
Sleeping
Sleeping
Add application file
Browse files- Dockerfile +21 -0
- README.md +69 -5
- app/__pycache__/main.cpython-313.pyc +0 -0
- app/__pycache__/model_loader.cpython-313.pyc +0 -0
- app/main.py +41 -0
- app/model_loader.py +62 -0
- requirements.txt +8 -0
Dockerfile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 6 |
+
|
| 7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 8 |
+
# RUN apt-get update && apt-get install -y libbitsandbytes-dev
|
| 9 |
+
|
| 10 |
+
RUN useradd -m -u 1000 user
|
| 11 |
+
|
| 12 |
+
USER user
|
| 13 |
+
|
| 14 |
+
ENV HOME=/home/user \
|
| 15 |
+
PATH=/home/user/.local/bin:$PATH
|
| 16 |
+
|
| 17 |
+
WORKDIR $HOME/app
|
| 18 |
+
|
| 19 |
+
COPY --chown=user . $HOME/app
|
| 20 |
+
|
| 21 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,11 +1,75 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
-
short_description:
|
| 9 |
---
|
| 10 |
|
| 11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Brainbench
|
| 3 |
+
emoji: 🦀
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
+
short_description: Implementing braingpt using BrainGPT-7B-v0.1
|
| 9 |
---
|
| 10 |
|
| 11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 12 |
+
|
| 13 |
+
This app currently uses **BrainGPT** as the model engine for generating and evaluating abstracts.
|
| 14 |
+
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
## 🚀 Features
|
| 18 |
+
|
| 19 |
+
- 🧪 Presents users with neuroscience abstracts (either original or altered).
|
| 20 |
+
- ✅ Users decide if an abstract is AI-modified and rate their confidence.
|
| 21 |
+
- 🤖 BrainGPT model evaluates the same abstract.
|
| 22 |
+
- 📊 Results compare user guesses vs model output in a clear, styled interface.
|
| 23 |
+
- 📂 Backed by a curated dataset hosted on Hugging Face Datasets.
|
| 24 |
+
- 🛠️ Fully Dockerized FastAPI application deployed via Hugging Face Spaces.
|
| 25 |
+
|
| 26 |
+
---
|
| 27 |
+
|
| 28 |
+
## 📦 Tech Stack
|
| 29 |
+
|
| 30 |
+
- **Frontend**: Jinja2 + Tailwind-style CSS
|
| 31 |
+
- **Backend**: FastAPI
|
| 32 |
+
- **Model**: `BrainGPT-7B-v0.1` via Hugging Face Transformers
|
| 33 |
+
- **Hosting**: Hugging Face Spaces (Docker SDK)
|
| 34 |
+
- **Dataset**: Custom neuroscience benchmark (Parquet format)
|
| 35 |
+
|
| 36 |
+
---
|
| 37 |
+
|
| 38 |
+
## 🔧 Endpoints
|
| 39 |
+
|
| 40 |
+
- `/`: Home page
|
| 41 |
+
- `/start`: Begin a new session (random abstract trials)
|
| 42 |
+
- `/trial`: Active abstract assessment
|
| 43 |
+
- `/submit-trial`: Submit a response, compare with model
|
| 44 |
+
- `/results`: View summary of performance
|
| 45 |
+
- `/predict`: POST endpoint used internally to get BrainGPT-7B-v0.1 output
|
| 46 |
+
|
| 47 |
+
---
|
| 48 |
+
|
| 49 |
+
## 🧠 Example Use Case
|
| 50 |
+
|
| 51 |
+
1. A neuroscience researcher lands on the app.
|
| 52 |
+
2. They read an abstract and guess whether it’s been altered.
|
| 53 |
+
3. They rate their confidence.
|
| 54 |
+
4. BrainGPT-7B-v0.1 also analyzes the abstract.
|
| 55 |
+
5. After 3 rounds, the app displays a comparison: who got what right?
|
| 56 |
+
|
| 57 |
+
---
|
| 58 |
+
|
| 59 |
+
## 📌 Current Model
|
| 60 |
+
|
| 61 |
+
- Model: [`BrainGPT-7B-v0.1`](https://huggingface.co/BrainGPT/BrainGPT-7B-v0.1)
|
| 62 |
+
- Reason: Fast, lightweight, deployable in CPU-only Spaces
|
| 63 |
+
|
| 64 |
+
---
|
| 65 |
+
|
| 66 |
+
## 🗺️ Roadmap
|
| 67 |
+
|
| 68 |
+
- [ ] Add leaderboard tracking
|
| 69 |
+
- [ ] Expand dataset with more domains
|
| 70 |
+
|
| 71 |
+
---
|
| 72 |
+
|
| 73 |
+
## 🙌 Credits
|
| 74 |
+
|
| 75 |
+
Built by **Dr. David Andai** — a general practitioner & data scientist passionate about mental health and AI.
|
app/__pycache__/main.cpython-313.pyc
ADDED
|
Binary file (1.38 kB). View file
|
|
|
app/__pycache__/model_loader.cpython-313.pyc
ADDED
|
Binary file (1.43 kB). View file
|
|
|
app/main.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from fastapi import FastAPI, Request, Form
|
| 3 |
+
from fastapi.responses import JSONResponse
|
| 4 |
+
from app.model_loader import load_model
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
model, tokenizer = load_model()
|
| 9 |
+
|
| 10 |
+
# @app.post("/predict")
|
| 11 |
+
# async def predict(request: Request):
|
| 12 |
+
# data = await request.json()
|
| 13 |
+
# input_text = data.get("input", "")
|
| 14 |
+
# inputs = tokenizer(input_text, return_tensors="pt")
|
| 15 |
+
# with torch.no_grad():
|
| 16 |
+
# output = model.generate(
|
| 17 |
+
# **inputs,
|
| 18 |
+
# max_new_tokens=60,
|
| 19 |
+
# do_sample=False,
|
| 20 |
+
# temperature=0.3
|
| 21 |
+
# )
|
| 22 |
+
# response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 23 |
+
# return JSONResponse(content={"output": response})
|
| 24 |
+
|
| 25 |
+
@app.post("/predict")
|
| 26 |
+
async def predict(request: Request):
|
| 27 |
+
data = await request.json()
|
| 28 |
+
input_text = data.get("input", "")
|
| 29 |
+
|
| 30 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
|
| 31 |
+
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
outputs = model.generate(
|
| 34 |
+
**inputs,
|
| 35 |
+
max_new_tokens=60,
|
| 36 |
+
do_sample=False,
|
| 37 |
+
temperature=0.3
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 41 |
+
return JSONResponse(content={"output": response})
|
app/model_loader.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
from peft import PeftModel
|
| 5 |
+
|
| 6 |
+
def load_model():
|
| 7 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 8 |
+
if not hf_token:
|
| 9 |
+
raise RuntimeError("HF_TOKEN not set.")
|
| 10 |
+
|
| 11 |
+
# Use a user-writable cache directory (important for Docker non-root)
|
| 12 |
+
HF_CACHE = os.path.expanduser("~/.cache/huggingface")
|
| 13 |
+
os.makedirs(HF_CACHE, exist_ok=True)
|
| 14 |
+
|
| 15 |
+
os.environ["TRANSFORMERS_CACHE"] = HF_CACHE
|
| 16 |
+
os.environ["HF_HOME"] = HF_CACHE
|
| 17 |
+
|
| 18 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
+
"meta-llama/Llama-2-7b-chat-hf",
|
| 20 |
+
use_auth_token=hf_token,
|
| 21 |
+
cache_dir="/tmp/hf_cache",
|
| 22 |
+
torch_dtype="auto",
|
| 23 |
+
device_map="auto"
|
| 24 |
+
)
|
| 25 |
+
model = PeftModel.from_pretrained(
|
| 26 |
+
base_model,
|
| 27 |
+
"BrainGPT/BrainGPT-7B-v0.1",
|
| 28 |
+
use_auth_token=hf_token,
|
| 29 |
+
cache_dir="/tmp/hf_cache"
|
| 30 |
+
)
|
| 31 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 32 |
+
"meta-llama/Llama-2-7b-chat-hf",
|
| 33 |
+
use_auth_token=hf_token,
|
| 34 |
+
cache_dir="/tmp/hf_cache"
|
| 35 |
+
)
|
| 36 |
+
return model, tokenizer
|
| 37 |
+
|
| 38 |
+
## GPT 2 Model
|
| 39 |
+
# import os
|
| 40 |
+
# from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 41 |
+
|
| 42 |
+
# def load_model():
|
| 43 |
+
# # Use a user-writable cache directory (important for Docker non-root)
|
| 44 |
+
# HF_CACHE = os.path.expanduser("~/.cache/huggingface")
|
| 45 |
+
# os.makedirs(HF_CACHE, exist_ok=True)
|
| 46 |
+
|
| 47 |
+
# os.environ["TRANSFORMERS_CACHE"] = HF_CACHE
|
| 48 |
+
# os.environ["HF_HOME"] = HF_CACHE
|
| 49 |
+
|
| 50 |
+
# model_name = "gpt2"
|
| 51 |
+
|
| 52 |
+
# tokenizer = AutoTokenizer.from_pretrained(
|
| 53 |
+
# model_name,
|
| 54 |
+
# cache_dir=HF_CACHE
|
| 55 |
+
# )
|
| 56 |
+
|
| 57 |
+
# model = AutoModelForCausalLM.from_pretrained(
|
| 58 |
+
# model_name,
|
| 59 |
+
# cache_dir=HF_CACHE
|
| 60 |
+
# )
|
| 61 |
+
|
| 62 |
+
# return model, tokenizer
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
transformers
|
| 3 |
+
peft
|
| 4 |
+
torch
|
| 5 |
+
accelerate
|
| 6 |
+
fastapi
|
| 7 |
+
uvicorn
|
| 8 |
+
bitsandbytes
|