Spaces:
Runtime error
Runtime error
Commit
·
fba53f9
1
Parent(s):
6903066
llm added
Browse files- app.py +30 -10
- requirements.txt +4 -1
app.py
CHANGED
@@ -2,6 +2,8 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
import matplotlib.pyplot as plt
|
4 |
from PIL import Image
|
|
|
|
|
5 |
import os
|
6 |
from monai.networks.nets import SegResNet
|
7 |
from monai.inferers import sliding_window_inference
|
@@ -18,6 +20,8 @@ from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
|
18 |
import librosa
|
19 |
import torch
|
20 |
|
|
|
|
|
21 |
title = 'Detect and Segment Brain Tumors 🧠'
|
22 |
description = '''
|
23 |
'''
|
@@ -84,14 +88,30 @@ def process_audio(sampling_rate, waveform):
|
|
84 |
return waveform
|
85 |
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
def detector(tumor_file, slice_number, channel, language, audio_question):
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
95 |
tumor_file_path = tumor_file.name
|
96 |
processed_data = preproc_transforms({'image': [tumor_file_path]})
|
97 |
tensor_3d_input = processed_data['image'].unsqueeze(0).to('cpu')
|
@@ -110,17 +130,17 @@ def detector(tumor_file, slice_number, channel, language, audio_question):
|
|
110 |
plt.savefig(output_image_path, bbox_inches='tight', pad_inches=0)
|
111 |
segment_image = np.asarray(Image.open(output_image_path))
|
112 |
os.remove(output_image_path)
|
113 |
-
return (channel_image, segment_image,
|
114 |
|
115 |
|
116 |
interface = gr.Interface(fn=detector, inputs=[gr.File(label="Tumor File"),
|
117 |
gr.Slider(0, 200, 50, step=1, label="Slice Number"),
|
118 |
gr.Radio((0, 1, 2), label="Channel"),
|
119 |
gr.Radio(("english", "japanese", "german", "spanish"), label="Language"),
|
120 |
-
gr.Audio(source="microphone"), ],
|
121 |
outputs=[gr.Image(label='channel', shape=(1, 1)),
|
122 |
gr.Image(label='Segmented Tumor', shape=(1, 1)),
|
123 |
-
gr.Textbox(label="
|
124 |
examples=examples,
|
125 |
description=description, theme='dark')
|
126 |
|
|
|
2 |
import numpy as np
|
3 |
import matplotlib.pyplot as plt
|
4 |
from PIL import Image
|
5 |
+
import openai
|
6 |
+
from dotenv import load_dotenv
|
7 |
import os
|
8 |
from monai.networks.nets import SegResNet
|
9 |
from monai.inferers import sliding_window_inference
|
|
|
20 |
import librosa
|
21 |
import torch
|
22 |
|
23 |
+
load_dotenv()
|
24 |
+
|
25 |
title = 'Detect and Segment Brain Tumors 🧠'
|
26 |
description = '''
|
27 |
'''
|
|
|
88 |
return waveform
|
89 |
|
90 |
|
91 |
+
openai.api_key = os.environ.get("OPENAI_KEY")
|
92 |
+
|
93 |
+
|
94 |
+
def make_llm_call(prompt,
|
95 |
+
context="You are a text generation model DR-Brain Developed by team brute force team consist of HARSHA VARDHAN V , SAWIN KUMAR Y , CHARAN TEJA P, KISHORE S. Your specialized in medical stuff"):
|
96 |
+
messages = [{"role": "user", "content": prompt}]
|
97 |
+
if context:
|
98 |
+
messages.insert(0, {"role": "system", "content": context})
|
99 |
+
response_obj = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
|
100 |
+
response_message = dict(dict(response_obj)['choices'][0])["message"]["content"]
|
101 |
+
return response_message
|
102 |
+
|
103 |
+
|
104 |
def detector(tumor_file, slice_number, channel, language, audio_question):
|
105 |
+
llm_answer = "Hi I'm Dr brain please enter a question to answer"
|
106 |
+
if audio_question:
|
107 |
+
sampling_rate, waveform = audio_question
|
108 |
+
forced_decoder_ids = processor_whisper.get_decoder_prompt_ids(language=language, task="transcribe")
|
109 |
+
waveform = process_audio(sampling_rate, waveform)
|
110 |
+
audio_inputs = processor_whisper(audio=waveform, sampling_rate=16000, return_tensors="pt")
|
111 |
+
predicted_ids = model_whisper.generate(**audio_inputs, max_length=400, forced_decoder_ids=forced_decoder_ids)
|
112 |
+
transcription = processor_whisper.batch_decode(predicted_ids, skip_special_tokens=True)
|
113 |
+
llm_quesion = transcription[0]
|
114 |
+
llm_answer = make_llm_call(llm_quesion)
|
115 |
tumor_file_path = tumor_file.name
|
116 |
processed_data = preproc_transforms({'image': [tumor_file_path]})
|
117 |
tensor_3d_input = processed_data['image'].unsqueeze(0).to('cpu')
|
|
|
130 |
plt.savefig(output_image_path, bbox_inches='tight', pad_inches=0)
|
131 |
segment_image = np.asarray(Image.open(output_image_path))
|
132 |
os.remove(output_image_path)
|
133 |
+
return (channel_image, segment_image, llm_answer)
|
134 |
|
135 |
|
136 |
interface = gr.Interface(fn=detector, inputs=[gr.File(label="Tumor File"),
|
137 |
gr.Slider(0, 200, 50, step=1, label="Slice Number"),
|
138 |
gr.Radio((0, 1, 2), label="Channel"),
|
139 |
gr.Radio(("english", "japanese", "german", "spanish"), label="Language"),
|
140 |
+
gr.Audio(info="Ask our medical specialist", source="microphone"), ],
|
141 |
outputs=[gr.Image(label='channel', shape=(1, 1)),
|
142 |
gr.Image(label='Segmented Tumor', shape=(1, 1)),
|
143 |
+
gr.Textbox(label="Dr brain response")], title=title,
|
144 |
examples=examples,
|
145 |
description=description, theme='dark')
|
146 |
|
requirements.txt
CHANGED
@@ -5,4 +5,7 @@ torchaudio
|
|
5 |
nibabel
|
6 |
monai
|
7 |
matplotlib
|
8 |
-
librosa
|
|
|
|
|
|
|
|
5 |
nibabel
|
6 |
monai
|
7 |
matplotlib
|
8 |
+
librosa
|
9 |
+
python-dotenv
|
10 |
+
requests
|
11 |
+
openai
|