reichenbach commited on
Commit
7bff9b8
1 Parent(s): 5ee2d13

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +148 -0
app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import torch
3
+ import requests
4
+ import gradio as gr
5
+ from transformers import pipeline
6
+ from usellm import Message, Options, UseLLM
7
+ from transformers import AutoTokenizer, AutoModelForCausalLM
8
+ from transformers import WhisperProcessor, WhisperForConditionalGeneration
9
+
10
+
11
+ def text_to_speech(text_input):
12
+ CHUNK_SIZE = 1024
13
+ url = "https://api.elevenlabs.io/v1/text-to-speech/TxGEqnHWrfWFTfGW9XjX"
14
+
15
+ headers = {
16
+ "Accept": "audio/mpeg",
17
+ "Content-Type": "application/json",
18
+ "xi-api-key": "7f91dfdd5390bbfd9d44148c59644039"
19
+ }
20
+
21
+ data = {
22
+ "text": text_input,
23
+ "model_id": "eleven_monolingual_v1"
24
+ }
25
+
26
+ audio_write_path = f"""output_{int(time.time())}.mp3"""
27
+
28
+ response = requests.post(url, json=data, headers=headers)
29
+ with open(audio_write_path, 'wb') as f:
30
+ for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
31
+ if chunk:
32
+ f.write(chunk)
33
+
34
+ return audio_write_path
35
+
36
+
37
+ def whisper_inference(input_audio):
38
+
39
+ processor1 = WhisperProcessor.from_pretrained("openai/whisper-large-v2")
40
+ model1 = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large-v2")
41
+
42
+ forced_decoder_ids = processor1.get_decoder_prompt_ids(task="translate")
43
+
44
+ input_features = processor1(input_audio, sampling_rate=16000, return_tensors="pt").input_features
45
+ predicted_ids = model1.generate(input_features, forced_decoder_ids=forced_decoder_ids)
46
+ transcription = processor1.batch_decode(predicted_ids, skip_special_tokens=True)
47
+
48
+ return transcription
49
+
50
+
51
+
52
+ def biogpt_large_infer(input_text):
53
+
54
+ tokenizer1 = AutoTokenizer.from_pretrained("microsoft/BioGPT-Large-PubMedQA", add_special_tokens=False)
55
+ model = AutoModelForCausalLM.from_pretrained("microsoft/BioGPT-Large-PubMedQA")#.to('cuda:0')
56
+
57
+ generator = pipeline("text-generation", model=model, tokenizer=tokenizer1)#, device="cuda:0")
58
+ output = generator(input_text, min_length=100,max_length=1024,num_beams=5,early_stopping=True,
59
+ num_return_sequences=1, do_sample=True)
60
+ output = output[0]['generated_text']
61
+ output = output.replace('▃','').replace('FREETEXT','').replace('TITLE','').replace('PARAGRAPH','').replace('ABSTRACT','').replace('<','').replace('>','').replace('/','').strip()
62
+
63
+ return output
64
+
65
+
66
+
67
+ def chatgpt_infer(input_text):
68
+
69
+ # Initialize the service
70
+ service = UseLLM(service_url="https://usellm.org/api/llm")
71
+
72
+ # Prepare the conversation
73
+ messages = [
74
+ Message(role="system", content="You are a medical assistant, which answers the query based on factual medical information only."),
75
+ Message(role="user", content=f"Give me few points on the disease {input_text} and its treatment."),
76
+ ]
77
+
78
+ options = Options(messages=messages)
79
+
80
+ # Interact with the service
81
+ response = service.chat(options)
82
+
83
+ return response.content
84
+
85
+
86
+ def audio_interface_demo(input_audio):
87
+
88
+ en_prompt = whisper_inference(input_audio)
89
+
90
+ biogpt_output = biogpt_large_infer(en_prompt)
91
+ chatgpt_output = chatgpt_infer(en_prompt)
92
+
93
+ bio_audio_output = text_to_speech(biogpt_output)
94
+ chat_audio_output = text_to_speech(chatgpt_output)
95
+
96
+ return biogpt_output, chatgpt_output, bio_audio_output, chat_audio_output
97
+
98
+
99
+ def text_interface_demo(input_text):
100
+
101
+ #en_prompt = whisper_inference(input_audio)
102
+
103
+ biogpt_output = biogpt_large_infer(input_text)
104
+ chatgpt_output = chatgpt_infer(input_text)
105
+
106
+ return biogpt_output, chatgpt_output
107
+
108
+
109
+ examples = [
110
+ ["Meningitis is"],
111
+ ["Brain Tumour is"]
112
+ ]
113
+
114
+ app = gr.Blocks()
115
+ with app:
116
+ gr.Markdown("# **<h4 align='center'>Voice based Medical Informational Bot<h4>**")
117
+
118
+ with gr.Row():
119
+ with gr.Column():
120
+
121
+ with gr.Tab("Text"):
122
+ input_text = gr.Textbox(lines=3, value="Brain Tumour is", label="Text")
123
+ text_button = gr.Button(value="Predict")
124
+
125
+ with gr.Tab("Audio"):
126
+ input_audio = gr.Audio(value="/home/ec2-user/SageMaker/text_vd_demo/artifacts/input.mp3", source="upload", type="filepath", label='Audio')
127
+ audio_button = gr.Button(value="Predict")
128
+
129
+ with gr.Row():
130
+ with gr.Column():
131
+ with gr.Tab("Output Text"):
132
+
133
+ biogpt_output = gr.Textbox(lines=3, label="BioGpt Output")
134
+ chatgpt_output = gr.Textbox(lines=3,label="ChatGPT Output")
135
+
136
+ with gr.Tab("Output Audio"):
137
+
138
+ biogpt_output = gr.Textbox(lines=3, label="BioGpt Output")
139
+ chatgpt_output = gr.Textbox(lines=3,label="ChatGPT Output")
140
+
141
+ audio_output1 = gr.Audio(value=None, label="ChatGPT Audio Output")
142
+ audio_output2 = gr.Audio(value=None, label="BioGpt Audio Output")
143
+
144
+ #gr.Examples(examples, inputs=[input_text], outputs=[prompt_text, output_text, translated_text], fn=biogpt_text, cache_examples=False)
145
+ text_button.click(text_interface_demo, inputs=[input_text], outputs=[biogpt_output, chatgpt_output])
146
+ audio_button.click(audio_interface_demo, inputs=[input_audio], outputs=[biogpt_output, chatgpt_output, audio_output2, audio_output1])
147
+
148
+ app.launch(share=True, debug=True)