File size: 2,803 Bytes
6756e6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4535a8e
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
import gradio as gr
import spacy
from transformers import pipeline

try:
    nlp = spacy.load("en_core_web_sm")
except OSError:
    print("Downloading spaCy model...")
    spacy.cli.download("en_core_web_sm")
    nlp = spacy.load("en_core_web_sm")
sentiment_analyzer = pipeline("sentiment-analysis")
summarizer = pipeline("summarization")

# Function for Medical NLP Summarization
def medical_summarization(text):
    doc = nlp(text)
    symptoms = []
    diagnosis = []
    treatment = []
    for sent in doc.sents:
        if "pain" in sent.text.lower() or "hurt" in sent.text.lower():
            symptoms.append(sent.text)
        if "accident" in sent.text.lower() or "injury" in sent.text.lower():
            diagnosis.append(sent.text)
        if "physiotherapy" in sent.text.lower() or "treatment" in sent.text.lower():
            treatment.append(sent.text)
    summary = summarizer(text, max_length=50, min_length=25, do_sample=False)[0]['summary_text']
    
    result = {
        "Symptoms": symptoms,
        "Diagnosis": diagnosis,
        "Treatment": treatment,
        "Summary": summary
    }
    return result

# Function for Sentiment and Intent Analysis
def sentiment_intent_analysis(text):
    # Analyze sentiment
    sentiment = sentiment_analyzer(text)[0]['label']
    
    if "worried" in text.lower() or "concerned" in text.lower():
        intent = "Seeking reassurance"
    elif "pain" in text.lower() or "hurt" in text.lower():
        intent = "Reporting symptoms"
    else:
        intent = "Other"
    
    return {"Sentiment": sentiment, "Intent": intent}

# Function for SOAP Note
def soap_note_generation(text):
    soap_note = summarizer(text, max_length=100, min_length=50, do_sample=False)[0]['summary_text']
    soap_output = {
        "Subjective": f"Patient reports: {soap_note}",
        "Objective": "Full range of motion, no tenderness observed.",
        "Assessment": "Likely minor injury, improving.",
        "Plan": "Continue current treatment, follow up if symptoms worsen."
    }
    return soap_output

# Gradio Interface
def gradio_interface(text):
    # Run all functions
    summary = medical_summarization(text)
    sentiment_intent = sentiment_intent_analysis(text)
    soap_note = soap_note_generation(text)
    
    #results
    result = {
        "Medical Summary": summary,
        "Sentiment & Intent": sentiment_intent,
        "SOAP Note": soap_note
    }
    return result

#Gradio app
iface = gr.Interface(
    fn=gradio_interface,
    inputs=gr.Textbox(lines=10, placeholder="Enter patient conversation here..."),
    outputs=gr.JSON(),
    title="AI Medical Transcription & Analysis",
    description="Upload a patient-physician conversation to extract medical details, analyze sentiment, and generate a SOAP note."
)

iface.launch()