Seicas commited on
Commit
943023c
·
verified ·
1 Parent(s): 933c075

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -50
app.py CHANGED
@@ -1,5 +1,4 @@
1
  from fastapi import FastAPI, Depends, HTTPException, Security, status, UploadFile, File
2
- from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
3
  from fastapi.middleware.cors import CORSMiddleware
4
  import gradio as gr
5
  import os
@@ -24,11 +23,10 @@ THEME = gr.themes.Soft(
24
  )
25
  LOGO = "assets/pediatric_logo.png" # Logo ekleyin (opsiyonel)
26
 
27
- # SpaCy modelini yükle (sm modeli kullan)
28
  try:
29
  nlp = spacy.load("tr_core_news_sm")
30
  except OSError:
31
- # Model yüklenemezse boş model kullan
32
  nlp = spacy.blank("tr")
33
 
34
  # FastAPI uygulaması
@@ -62,53 +60,43 @@ async def transcribe_audio_api(
62
  asr_model: MedicalASR = Depends(get_asr_model)
63
  ):
64
  try:
65
- # Geçici dosya oluştur
66
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
67
  temp_file.write(await file.read())
68
  temp_file_path = temp_file.name
69
 
70
- # Transkripsiyon yap
71
  result = asr_model.transcribe(
72
  temp_file_path,
73
  speaker_diarization=diarize,
74
  enhance_audio=enhance_audio
75
  )
76
 
77
- # Geçici dosyayı temizle
78
  os.unlink(temp_file_path)
79
-
80
  return result
81
 
82
  except Exception as e:
83
  raise HTTPException(status_code=500, detail=str(e))
84
 
85
- # Gradio arayüzü
86
- def transcribe_audio(audio_file, diarize=True, enhance_audio=True, anonymize=True):
87
  """Ses dosyasını transkribe eder"""
88
  try:
89
  if audio_file is None:
90
  return "Lütfen bir ses dosyası yükleyin."
91
 
92
- # ASR modelini oluştur
93
  asr_model = get_asr_model()
94
-
95
- # Transkripsiyon yap
96
  result = asr_model.transcribe(
97
  audio_file,
98
  speaker_diarization=diarize,
99
  enhance_audio=enhance_audio
100
  )
101
 
102
- # Sonuçları formatla
103
  output = "📝 Transkripsiyon:\n\n"
104
 
105
  if "diarization" in result:
106
- # Konuşmacı bazlı transkripsiyon
107
  for segment in result["diarization"]:
108
  output += f"🗣️ {segment['speaker']} ({segment['start']:.1f}s - {segment['end']:.1f}s):\n"
109
  output += f"{segment['text']}\n\n"
110
  else:
111
- # Düz transkripsiyon
112
  output += result["text"]
113
 
114
  if result.get("anonymized"):
@@ -118,41 +106,22 @@ def transcribe_audio(audio_file, diarize=True, enhance_audio=True, anonymize=Tru
118
  except Exception as e:
119
  return f"Bir hata oluştu: {str(e)}"
120
 
121
- # Gradio arayüzünü oluştur
122
- with gr.Blocks(title="Tıbbi Konuşma Transkripsiyon Servisi") as demo:
123
- gr.Markdown("""
124
- # 🏥 Tıbbi Konuşma Transkripsiyon Servisi
125
-
126
- Bu servis, doktor vizitelerindeki konuşmaları yazıya döker ve konuşmacıları ayırt eder.
127
- """)
128
-
129
- with gr.Row():
130
- with gr.Column():
131
- # Yeni Gradio Audio bileşeni kullanımı
132
- audio_input = gr.Audio(
133
- label="Ses Dosyası",
134
- type="filepath"
135
- )
136
-
137
- with gr.Row():
138
- diarize = gr.Checkbox(label="Konuşmacı Ayrımı", value=True)
139
- enhance = gr.Checkbox(label="Ses İyileştirme", value=True)
140
- anonymize = gr.Checkbox(label="Kişisel Verileri Anonimleştir", value=True)
141
-
142
- submit_btn = gr.Button("Transkribe Et", variant="primary")
143
-
144
- with gr.Column():
145
- output_text = gr.Textbox(
146
- label="Transkripsiyon Sonucu",
147
- lines=10
148
- )
149
-
150
- # Buton tıklama olayı
151
- submit_btn.click(
152
- fn=transcribe_audio,
153
- inputs=[audio_input, diarize, enhance, anonymize],
154
- outputs=output_text
155
- )
156
 
157
  # Uygulamayı başlat
158
  if __name__ == "__main__":
 
1
  from fastapi import FastAPI, Depends, HTTPException, Security, status, UploadFile, File
 
2
  from fastapi.middleware.cors import CORSMiddleware
3
  import gradio as gr
4
  import os
 
23
  )
24
  LOGO = "assets/pediatric_logo.png" # Logo ekleyin (opsiyonel)
25
 
26
+ # SpaCy modelini yükle
27
  try:
28
  nlp = spacy.load("tr_core_news_sm")
29
  except OSError:
 
30
  nlp = spacy.blank("tr")
31
 
32
  # FastAPI uygulaması
 
60
  asr_model: MedicalASR = Depends(get_asr_model)
61
  ):
62
  try:
 
63
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
64
  temp_file.write(await file.read())
65
  temp_file_path = temp_file.name
66
 
 
67
  result = asr_model.transcribe(
68
  temp_file_path,
69
  speaker_diarization=diarize,
70
  enhance_audio=enhance_audio
71
  )
72
 
 
73
  os.unlink(temp_file_path)
 
74
  return result
75
 
76
  except Exception as e:
77
  raise HTTPException(status_code=500, detail=str(e))
78
 
79
+ # Gradio wrapper fonksiyonu
80
+ def transcribe_gr(audio_file, diarize=True, enhance_audio=True, anonymize=True):
81
  """Ses dosyasını transkribe eder"""
82
  try:
83
  if audio_file is None:
84
  return "Lütfen bir ses dosyası yükleyin."
85
 
 
86
  asr_model = get_asr_model()
 
 
87
  result = asr_model.transcribe(
88
  audio_file,
89
  speaker_diarization=diarize,
90
  enhance_audio=enhance_audio
91
  )
92
 
 
93
  output = "📝 Transkripsiyon:\n\n"
94
 
95
  if "diarization" in result:
 
96
  for segment in result["diarization"]:
97
  output += f"🗣️ {segment['speaker']} ({segment['start']:.1f}s - {segment['end']:.1f}s):\n"
98
  output += f"{segment['text']}\n\n"
99
  else:
 
100
  output += result["text"]
101
 
102
  if result.get("anonymized"):
 
106
  except Exception as e:
107
  return f"Bir hata oluştu: {str(e)}"
108
 
109
+ # Gradio arayüzü
110
+ demo = gr.Interface(
111
+ fn=transcribe_gr,
112
+ inputs=[
113
+ gr.Audio(type="filepath", label="Ses Dosyası"),
114
+ gr.Checkbox(label="Konuşmacı Ayrımı", value=True),
115
+ gr.Checkbox(label="Ses İyileştirme", value=True),
116
+ gr.Checkbox(label="Kişisel Verileri Anonimleştir", value=True)
117
+ ],
118
+ outputs=gr.Textbox(label="Transkripsiyon Sonucu", lines=10),
119
+ title="🏥 Tıbbi Konuşma Transkripsiyon Servisi",
120
+ description="Bu servis, doktor vizitelerindeki konuşmaları yazıya döker ve konuşmacıları ayırt eder."
121
+ )
122
+
123
+ # Gradio'yu FastAPI'ye mount et
124
+ app.mount("/", demo.app)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  # Uygulamayı başlat
127
  if __name__ == "__main__":