SOAPAPI / app /main.py
syafiqq02's picture
fast api
9c86826
raw
history blame
4.71 kB
import os
import nltk
import uvicorn
from fastapi import FastAPI, File, UploadFile
from pydantic import BaseModel
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lsa import LsaSummarizer
from groq import Groq
GROQ_API_KEY = "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"
groq_client = Groq(api_key=GROQ_API_KEY)
def save_to_file(content: str, filename: str) -> str:
with open(filename, 'w', encoding='utf-8') as file:
file.write(content)
return filename
def transcribe_and_summarize(audio_path: str):
with open(audio_path, "rb") as audio_file:
response = groq_client.audio.transcriptions.create(
model="whisper-large-v3",
file=audio_file,
response_format="text"
)
transcription = response
parser = PlaintextParser.from_string(transcription, Tokenizer("english"))
summarizer = LsaSummarizer()
summary_sentences = summarizer(parser.document, 5)
summarized_text = " ".join(str(s) for s in summary_sentences)
original_tokens = len(nltk.word_tokenize(transcription))
summarized_tokens = len(nltk.word_tokenize(summarized_text))
token_info = f"Asli: {original_tokens} token | Ringkasan: {summarized_tokens} token"
summarized_file = save_to_file(summarized_text, "summarized_transcription.txt")
return transcription, summarized_text, summarized_file, audio_path, token_info
def summarize_soap(dialogue: str):
prompt_soap = f"""
Anda adalah asisten medis yang membantu dokter dalam menyusun catatan SOAP berdasarkan percakapan dokter dan pasien.
Ringkaskan dalam bentuk paragraf tanpa adanya bullet point dan gunakan bahasa Indonesia.
Harap buat ringkasan dalam format berikut:
Subjective:
Objective:
Assessment:
Plan:
### Percakapan:
{dialogue}
Tolong jangan tambahkan informasi tambahan selain yang berkaitan dengan diagnosis, obat, hasil lab, dan radiologi.
"""
response_soap = groq_client.chat.completions.create(
model="llama3-8b-8192",
messages=[{"role": "user", "content": prompt_soap}]
)
return response_soap.choices[0].message.content
def generate_soap(transcription: str):
soap_content = summarize_soap(transcription)
soap_file = save_to_file(soap_content, "soap_summary.txt")
return soap_content, soap_file
def detect_medical_tags(dialogue: str):
prompt_tags = f"""
Identifikasi dan berikan luaran dalam bahasa Indonesia tags berikut dari percakapan dengan format:
Diagnosis:
Obat:
Hasil Lab:
Radiologi:
### Percakapan:
{dialogue}
Tolong jangan tambahkan informasi tambahan selain yang berkaitan dengan diagnosis, obat, hasil lab, dan radiologi.
"""
response_tags = groq_client.chat.completions.create(
model="llama3-8b-8192",
messages=[{"role": "user", "content": prompt_tags}]
)
return response_tags.choices[0].message.content
def generate_tags(transcription: str):
tags_content = detect_medical_tags(transcription)
tags_file = save_to_file(tags_content, "medical_tags.txt")
return tags_content, tags_file
app = FastAPI(title="Medical Transcription Pipeline (Groq API)")
@app.post("/full_process")
async def full_process(audio: UploadFile = File(...)):
filename = audio.filename
temp_audio_path = f"temp_{filename}"
with open(temp_audio_path, "wb") as f:
f.write(await audio.read())
transcription, summarized_text, summarized_file, audio_path, token_info = transcribe_and_summarize(temp_audio_path)
soap_content, soap_file = generate_soap(transcription)
tags_content, tags_file = generate_tags(transcription)
os.remove(temp_audio_path) # bersihkan file temporer setelah selesai digunakan
return {
"transcription": transcription,
"summarized_text": summarized_text,
"summarized_file": summarized_file,
"audio_path": audio_path,
"token_info": token_info,
"soap_content": soap_content,
"soap_file": soap_file,
"tags_content": tags_content,
"tags_file": tags_file
}
class TranscriptionInput(BaseModel):
dialogue: str
@app.post("/soap_tags")
async def soap_tags(data: TranscriptionInput):
transcript_text = data.dialogue
soap_content, soap_file = generate_soap(transcript_text)
tags_content, tags_file = generate_tags(transcript_text)
return {
"soap_content": soap_content,
"soap_file": soap_file,
"tags_content": tags_content,
"tags_file": tags_file
}
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)