Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, uuid, subprocess
|
| 2 |
+
from flask import Flask, request, jsonify, render_template
|
| 3 |
+
from faster_whisper import WhisperModel
|
| 4 |
+
from transformers import pipeline, AutoTokenizer, AutoModel
|
| 5 |
+
from deep_translator import GoogleTranslator
|
| 6 |
+
import torch
|
| 7 |
+
import torch.nn.functional as F
|
| 8 |
+
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
|
| 11 |
+
# ---------------- LOAD MODELS ----------------
|
| 12 |
+
print("Loading models...")
|
| 13 |
+
|
| 14 |
+
whisper_model = WhisperModel("small", device="cpu", compute_type="int8")
|
| 15 |
+
|
| 16 |
+
bart = pipeline("text2text-generation", model="facebook/bart-large")
|
| 17 |
+
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained("nlpie/clinical-roberta-base")
|
| 19 |
+
roberta = AutoModel.from_pretrained("nlpie/clinical-roberta-base")
|
| 20 |
+
|
| 21 |
+
# ---------------- EMBEDDING ----------------
|
| 22 |
+
def get_embedding(text):
|
| 23 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True)
|
| 24 |
+
outputs = roberta(**inputs)
|
| 25 |
+
return outputs.last_hidden_state.mean(dim=1)
|
| 26 |
+
|
| 27 |
+
# ---------------- REFERENCE ----------------
|
| 28 |
+
ref_texts = {
|
| 29 |
+
"emergency": "severe chest pain unconscious breathing failure stroke",
|
| 30 |
+
"urgent": "fever infection moderate pain vomiting",
|
| 31 |
+
"non-urgent": "mild headache cold stable"
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
ref_embeddings = {k: get_embedding(v) for k, v in ref_texts.items()}
|
| 35 |
+
|
| 36 |
+
# ---------------- ROUTES ----------------
|
| 37 |
+
@app.route("/")
|
| 38 |
+
def home():
|
| 39 |
+
return render_template("index.html")
|
| 40 |
+
|
| 41 |
+
@app.route("/process", methods=["POST"])
|
| 42 |
+
def process():
|
| 43 |
+
|
| 44 |
+
file = request.files["audio"]
|
| 45 |
+
|
| 46 |
+
uid = str(uuid.uuid4())
|
| 47 |
+
input_path = f"{uid}.webm"
|
| 48 |
+
wav_path = f"{uid}.wav"
|
| 49 |
+
|
| 50 |
+
file.save(input_path)
|
| 51 |
+
|
| 52 |
+
subprocess.run([
|
| 53 |
+
"ffmpeg", "-y", "-i", input_path,
|
| 54 |
+
"-ac", "1", "-ar", "16000", wav_path
|
| 55 |
+
])
|
| 56 |
+
|
| 57 |
+
# Whisper
|
| 58 |
+
segments, info = whisper_model.transcribe(wav_path)
|
| 59 |
+
text = " ".join([seg.text for seg in segments])
|
| 60 |
+
|
| 61 |
+
# Translate
|
| 62 |
+
try:
|
| 63 |
+
text_en = GoogleTranslator(source="auto", target="en").translate(text)
|
| 64 |
+
except:
|
| 65 |
+
text_en = text
|
| 66 |
+
|
| 67 |
+
# BART clean
|
| 68 |
+
cleaned = bart(f"Extract symptoms: {text_en}")[0]["generated_text"]
|
| 69 |
+
|
| 70 |
+
# RoBERTa classify
|
| 71 |
+
emb = get_embedding(cleaned)
|
| 72 |
+
|
| 73 |
+
scores = {}
|
| 74 |
+
for k, ref in ref_embeddings.items():
|
| 75 |
+
scores[k] = F.cosine_similarity(emb, ref).item()
|
| 76 |
+
|
| 77 |
+
label = max(scores, key=scores.get)
|
| 78 |
+
confidence = scores[label]
|
| 79 |
+
|
| 80 |
+
score_map = {"emergency": 90, "urgent": 60, "non-urgent": 20}
|
| 81 |
+
score = int(score_map[label] * max(confidence, 0.5))
|
| 82 |
+
|
| 83 |
+
return jsonify({
|
| 84 |
+
"text": text,
|
| 85 |
+
"cleaned": cleaned,
|
| 86 |
+
"severity": label,
|
| 87 |
+
"score": score
|
| 88 |
+
})
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
app.run(host="0.0.0.0", port=7860)
|