import gradio as gr import os import requests from dotenv import load_dotenv load_dotenv() API_TRANSCRIBE = os.getenv("API_TRANSCRIBE") API_TEXT = os.getenv("API_TEXT") # ==== Function for backend calls ==== def handle_audio(audio_file): if audio_file is None: return "", "", "" with open(audio_file, "rb") as f: files = {"audio": f} response = requests.post(API_TRANSCRIBE, files=files) result = response.json() return result.get("transcription", "-"), result.get("soap_content", "-"), result.get("tags_content", "-") def handle_text(dialogue): if not dialogue.strip(): return "", "", "" response = requests.post(API_TEXT, json={"dialogue": dialogue}) result = response.json() return dialogue, result.get("soap_content", "-"), result.get("tags_content", "-") # ==== Function to toggle inputs with refresh ==== def toggle_inputs_with_refresh(choice): # Clear semua output saat dropdown berubah return ( gr.update(visible=(choice == "Upload Audio"), value=None), # Clear audio upload gr.update(visible=(choice == "Realtime Recording"), value=None), # Clear audio record gr.update(visible=(choice == "Input Teks"), value=""), # Clear text input gr.update(value=""), # Clear transcript output gr.update(value=""), # Clear SOAP output gr.update(value="") # Clear tags output ) # ==== Function to clear all data ==== def clear_all_data(): return ( gr.update(value=None), # Clear audio upload gr.update(value=None), # Clear audio record gr.update(value=""), # Clear text input gr.update(value=""), # Clear transcript output gr.update(value=""), # Clear SOAP output gr.update(value="") # Clear tags output ) # ==== Combined processing function ==== def process_data(choice, audio_upload, audio_record, text_input): if choice == "Upload Audio": return handle_audio(audio_upload) elif choice == "Realtime Recording": return handle_audio(audio_record) elif choice == "Input Teks": return handle_text(text_input) else: return "", "", "" # ==== UI ==== with gr.Blocks(title="SOAP AI Dropdown Input") as app: gr.Markdown("## 🩺 SOAP AI β€” Pilih Jenis Input") with gr.Row(): input_choice = gr.Dropdown( choices=["Upload Audio", "Realtime Recording", "Input Teks"], value="Upload Audio", label="Pilih Metode Input", scale=4 ) clear_button = gr.Button("πŸ—‘οΈClear all", variant="secondary", size="sm", scale=0.5, min_width=25) # Input fields (hidden by default except selected) # Upload Audio - seperti gambar 1 (drag & drop + upload button) audio_upload = gr.Audio( sources=["upload"], label="πŸ”Š Upload File Audio", type="filepath", visible=True ) # Realtime Recording - seperti gambar 2 (tombol record + microphone selector) audio_record = gr.Audio( sources=["microphone"], label="πŸ”Š Realtime Recording", type="filepath", visible=False ) # Text Input text_input = gr.Textbox( label="πŸ“ Masukkan Percakapan Dokter-Pasien", lines=6, visible=False ) # Tombol proses process_button = gr.Button("πŸš€ Proses ke SOAP") # Output transcript_output = gr.Textbox(label="πŸ“ Hasil Transkripsi", lines=3) soap_output = gr.Textbox(label="πŸ“‹ Ringkasan SOAP", lines=6) tags_output = gr.Textbox(label="🏷️ Medical Tags", lines=6) # === Events === # Event untuk dropdown change - akan clear semua data input_choice.change( fn=toggle_inputs_with_refresh, inputs=input_choice, outputs=[audio_upload, audio_record, text_input, transcript_output, soap_output, tags_output] ) # Event untuk tombol clear - akan clear semua data clear_button.click( fn=clear_all_data, outputs=[audio_upload, audio_record, text_input, transcript_output, soap_output, tags_output] ) # Single event handler untuk semua jenis input process_button.click( fn=process_data, inputs=[input_choice, audio_upload, audio_record, text_input], outputs=[transcript_output, soap_output, tags_output], show_progress="minimal" ) app.launch()