Spaces:
Running
Running
File size: 4,182 Bytes
cd2255e 071b666 a02c8c8 0513bad 071b666 bb27d06 071b666 0513bad 943023c eae3904 bb27d06 cd2255e 0513bad 071b666 0513bad 071b666 0513bad 071b666 0513bad 071b666 0513bad 071b666 cd2255e 0513bad 071b666 0513bad 071b666 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
from fastapi import FastAPI, Depends, HTTPException, status, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
import gradio as gr
import os
import tempfile
import json
from typing import Optional
import spacy
from preprocessing import clean_audio
from asr import transcribe_file, MedicalASR
from diarization import diarize_segments
from privacy import MedicalPrivacyProcessor
from config import settings
# HuggingFace token'ını ayarla (kullanıcının kendi token'ını eklemesini sağla)
os.environ["HF_TOKEN"] = os.environ.get("HF_TOKEN", "")
os.environ["ENVIRONMENT"] = "production"
# Arayüz konfigürasyonu
THEME = gr.themes.Soft(
primary_hue="indigo",
secondary_hue="blue",
)
LOGO = "assets/pediatric_logo.png" # Logo ekleyin (opsiyonel)
# SpaCy modelini yükle
try:
nlp = spacy.load("tr_core_news_sm")
except OSError:
nlp = spacy.blank("tr")
# Gradio arayüzü
def create_gradio_app():
def transcribe_gr(audio_file, diarize=True, enhance_audio=True, anonymize=True):
try:
if audio_file is None:
return "Lütfen bir ses dosyası yükleyin."
asr_model = get_asr_model()
result = asr_model.transcribe(
audio_file,
speaker_diarization=diarize,
enhance_audio=enhance_audio
)
output = "📝 Transkripsiyon:\n\n"
if "diarization" in result:
for segment in result["diarization"]:
output += f"🗣️ {segment['speaker']} ({segment['start']:.1f}s - {segment['end']:.1f}s):\n"
output += f"{segment['text']}\n\n"
else:
output += result["text"]
if result.get("anonymized"):
output += "\n🔒 Kişisel veriler anonimleştirildi."
return output
except Exception as e:
return f"Bir hata oluştu: {str(e)}"
demo = gr.Interface(
fn=transcribe_gr,
inputs=[
gr.Audio(type="filepath", label="Ses Dosyası"),
gr.Checkbox(label="Konuşmacı Ayrımı", value=True),
gr.Checkbox(label="Ses İyileştirme", value=True),
gr.Checkbox(label="Kişisel Verileri Anonimleştir", value=True)
],
outputs=gr.Textbox(label="Transkripsiyon Sonucu", lines=10),
title="🏥 Tıbbi Konuşma Transkripsiyon Servisi",
description="Bu servis, doktor vizitelerindeki konuşmaları yazıya döker ve konuşmacıları ayırt eder.",
theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="blue"),
allow_flagging="never"
)
return demo
# FastAPI uygulaması
app = FastAPI(title="Pediatrik ASR API")
# CORS ayarları
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ASR modeli için bağımlılık
def get_asr_model():
config = {
"language": "tr",
"model": settings.ASR_MODEL,
"domain": "medical"
}
return MedicalASR(config)
# API endpoint'i
@app.post("/api/v1/transcribe")
async def transcribe_audio_api(
file: UploadFile = File(...),
diarize: bool = True,
enhance_audio: bool = True,
anonymize: Optional[bool] = None,
asr_model: MedicalASR = Depends(get_asr_model)
):
try:
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
temp_file.write(await file.read())
temp_file_path = temp_file.name
result = asr_model.transcribe(
temp_file_path,
speaker_diarization=diarize,
enhance_audio=enhance_audio
)
os.unlink(temp_file_path)
return result
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Gradio uygulamasını oluştur ve mount et
gradio_app = create_gradio_app()
app = gr.mount_gradio_app(app, gradio_app, path="/")
# Uygulamayı başlat
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860) |