SoufianeDahimi commited on
Commit
2250474
1 Parent(s): a4e0607
Files changed (1) hide show
  1. app.py +631 -0
app.py ADDED
@@ -0,0 +1,631 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """#### importing Libraries"""
2
+
3
+ import random
4
+ import time
5
+ import numpy as np
6
+ from pydub import AudioSegment # For audio file operations
7
+ from pydub.playback import play # For playing audio
8
+ import io # For handling input/output operations
9
+ import elevenlabs # Custom library - please provide more context if needed
10
+ from elevenlabs import (
11
+ generate,
12
+ play,
13
+ ) # Importing specific functions from the custom library
14
+ import gradio as gr # For building interactive UI for our model
15
+ import openai # OpenAI API library
16
+ import os # For interacting with the operating system
17
+ import re # Regular expressions library for string operations
18
+ import requests # For making HTTP requests
19
+ from gradio_client import Client
20
+
21
+ client = Client("https://facebook-seamless-m4t.hf.space/")
22
+
23
+ """#### Darija Audio to eng text /// generate an eng question from an audio"""
24
+
25
+ # Defining a function for processing Darija audio and translating it to English
26
+ def process_darija_audio_toEng(filepath):
27
+ result = client.predict(
28
+ "S2TT (Speech to Text translation)",
29
+ "file",
30
+ filepath,
31
+ filepath,
32
+ "",
33
+ "Moroccan Arabic",
34
+ "English",
35
+ api_name="/run",
36
+ )
37
+ return result[1]
38
+
39
+
40
+ def darija_audio_to_darija_text(filepath):
41
+ result = client.predict(
42
+ "S2TT (Speech to Text translation)", # str (Option from: ['S2ST (Speech to Speech translation)', 'S2TT (Speech to Text translation)', 'T2ST (Text to Speech translation)', 'T2TT (Text to Text translation)', 'ASR (Automatic Speech Recognition)'])
43
+ "file", # str in 'Audio source' Radio component
44
+ filepath, # str (filepath or URL to file)
45
+ filepath, # str (filepath or URL to file)
46
+ "", # str in 'Input text' Textbox component
47
+ "Moroccan Arabic", # str (Option from: ['Afrikaans', 'Amharic', 'Armenian', 'Assamese', 'Basque', 'Belarusian', 'Bengali', 'Bosnian', 'Bulgarian', 'Burmese', 'Cantonese', 'Catalan', 'Cebuano', 'Central Kurdish', 'Croatian', 'Czech', 'Danish', 'Dutch', 'Egyptian Arabic', 'English', 'Estonian', 'Finnish', 'French', 'Galician', 'Ganda', 'Georgian', 'German', 'Greek', 'Gujarati', 'Halh Mongolian', 'Hebrew', 'Hindi', 'Hungarian', 'Icelandic', 'Igbo', 'Indonesian', 'Irish', 'Italian', 'Japanese', 'Javanese', 'Kannada', 'Kazakh', 'Khmer', 'Korean', 'Kyrgyz', 'Lao', 'Lithuanian', 'Luo', 'Macedonian', 'Maithili', 'Malayalam', 'Maltese', 'Mandarin Chinese', 'Marathi', 'Meitei', 'Modern Standard Arabic', 'Moroccan Arabic', 'Nepali', 'North Azerbaijani', 'Northern Uzbek', 'Norwegian Bokmål', 'Norwegian Nynorsk', 'Nyanja', 'Odia', 'Polish', 'Portuguese', 'Punjabi', 'Romanian', 'Russian', 'Serbian', 'Shona', 'Sindhi', 'Slovak', 'Slovenian', 'Somali', 'Southern Pashto', 'Spanish', 'Standard Latvian', 'Standard Malay', 'Swahili', 'Swedish', 'Tagalog', 'Tajik', 'Tamil', 'Telugu', 'Thai', 'Turkish', 'Ukrainian', 'Urdu', 'Vietnamese', 'Welsh', 'West Central Oromo', 'Western Persian', 'Yoruba', 'Zulu'])
48
+ "Modern Standard Arabic", # str (Option from: ['Bengali', 'Catalan', 'Czech', 'Danish', 'Dutch', 'English', 'Estonian', 'Finnish', 'French', 'German', 'Hindi', 'Indonesian', 'Italian', 'Japanese', 'Korean', 'Maltese', 'Mandarin Chinese', 'Modern Standard Arabic', 'Northern Uzbek', 'Polish', 'Portuguese', 'Romanian', 'Russian', 'Slovak', 'Spanish', 'Swahili', 'Swedish', 'Tagalog', 'Telugu', 'Thai', 'Turkish', 'Ukrainian', 'Urdu', 'Vietnamese', 'Welsh', 'Western Persian'])
49
+ api_name="/run",
50
+ )
51
+ return result[1]
52
+
53
+
54
+ def darija_to_eng(text):
55
+ result = client.predict(
56
+ "T2TT (Text to Text translation)", # str (Option from: ['S2ST (Speech to Speech translation)', 'S2TT (Speech to Text translation)', 'T2ST (Text to Speech translation)', 'T2TT (Text to Text translation)', 'ASR (Automatic Speech Recognition)'])
57
+ "file", # str in 'Audio source' Radio component
58
+ "", # str (filepath or URL to file)
59
+ "", # str (filepath or URL to file)
60
+ text, # str in 'Input text' Textbox component
61
+ "Modern Standard Arabic", # str (Option from: ['Afrikaans', 'Amharic', 'Armenian', 'Assamese', 'Basque', 'Belarusian', 'Bengali', 'Bosnian', 'Bulgarian', 'Burmese', 'Cantonese', 'Catalan', 'Cebuano', 'Central Kurdish', 'Croatian', 'Czech', 'Danish', 'Dutch', 'Egyptian Arabic', 'English', 'Estonian', 'Finnish', 'French', 'Galician', 'Ganda', 'Georgian', 'German', 'Greek', 'Gujarati', 'Halh Mongolian', 'Hebrew', 'Hindi', 'Hungarian', 'Icelandic', 'Igbo', 'Indonesian', 'Irish', 'Italian', 'Japanese', 'Javanese', 'Kannada', 'Kazakh', 'Khmer', 'Korean', 'Kyrgyz', 'Lao', 'Lithuanian', 'Luo', 'Macedonian', 'Maithili', 'Malayalam', 'Maltese', 'Mandarin Chinese', 'Marathi', 'Meitei', 'Modern Standard Arabic', 'Moroccan Arabic', 'Nepali', 'North Azerbaijani', 'Northern Uzbek', 'Norwegian Bokmål', 'Norwegian Nynorsk', 'Nyanja', 'Odia', 'Polish', 'Portuguese', 'Punjabi', 'Romanian', 'Russian', 'Serbian', 'Shona', 'Sindhi', 'Slovak', 'Slovenian', 'Somali', 'Southern Pashto', 'Spanish', 'Standard Latvian', 'Standard Malay', 'Swahili', 'Swedish', 'Tagalog', 'Tajik', 'Tamil', 'Telugu', 'Thai', 'Turkish', 'Ukrainian', 'Urdu', 'Vietnamese', 'Welsh', 'West Central Oromo', 'Western Persian', 'Yoruba', 'Zulu'])
62
+ "English", # str (Option from: ['Bengali', 'Catalan', 'Czech', 'Danish', 'Dutch', 'English', 'Estonian', 'Finnish', 'French', 'German', 'Hindi', 'Indonesian', 'Italian', 'Japanese', 'Korean', 'Maltese', 'Mandarin Chinese', 'Modern Standard Arabic', 'Northern Uzbek', 'Polish', 'Portuguese', 'Romanian', 'Russian', 'Slovak', 'Spanish', 'Swahili', 'Swedish', 'Tagalog', 'Telugu', 'Thai', 'Turkish', 'Ukrainian', 'Urdu', 'Vietnamese', 'Welsh', 'Western Persian'])
63
+ api_name="/run",
64
+ )
65
+ return result[1]
66
+
67
+
68
+ def eng_to_arabic(text):
69
+ result = client.predict(
70
+ "T2TT (Text to Text translation)",
71
+ "file",
72
+ "",
73
+ "",
74
+ text,
75
+ "English",
76
+ "Modern Standard Arabic",
77
+ api_name="/run",
78
+ )
79
+ return result[1]
80
+
81
+
82
+ """#ChatGPT as a doctor"""
83
+
84
+ import openai
85
+ import os
86
+ from langchain.agents.agent_toolkits import create_python_agent
87
+ from langchain.agents import load_tools, initialize_agent
88
+ from langchain.agents import AgentType
89
+ from langchain.tools.python.tool import PythonREPLTool
90
+ from langchain.python import PythonREPL
91
+ from langchain.chat_models import ChatOpenAI
92
+ from langchain.memory import ConversationBufferMemory
93
+ from langchain.prompts import ChatPromptTemplate
94
+ from langchain.prompts import PromptTemplate
95
+ from langchain.chains import LLMChain
96
+ from langchain.chains import SimpleSequentialChain, SequentialChain, ConversationChain
97
+ import json
98
+
99
+ openai.api_key = "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"
100
+ os.environ["OPENAI_API_KEY"] = openai.api_key
101
+ llm = ChatOpenAI(temperature=0.0, model="gpt-3.5-turbo")
102
+ tools = load_tools(["wikipedia"], llm=llm)
103
+ memory = ""
104
+ questions = [
105
+ "What symptoms are you currently experiencing?",
106
+ "Are you taking any medications or supplements? If yes, please list them.",
107
+ "Do you have any medical conditions or allergies?",
108
+ "Have there been any recent changes in your health or lifestyle that you think are important to mention?",
109
+ ]
110
+ history_test = []
111
+ answers = []
112
+ question_index = 0
113
+ current_question = questions[question_index]
114
+ patient_info1 = patient_info2 = patient_info3 = ""
115
+
116
+ react_agent = initialize_agent(
117
+ tools, llm, agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION, verbose=True
118
+ )
119
+
120
+
121
+ def get_agent_prompt(query, chat_history):
122
+ return f"""You are a professional doctor. You are in conversation with a patient Please answer the medical questions based on the patient's description and situation. Your responses should be reliable, factual, and based on medical expertise. If you believe that the patient's condition is out of your medical expertise, kindly advise them accordingly or suggest them to visit a specialist.
123
+ ###DON'T SAY YOU ARE NOT a DOCTOR. FORGET THAT YOU ARE AN AI MODEL!
124
+ ###Provide practical tips like a real doctor would normally do. Make it conversationalist!
125
+ If you can't answer the patients query, reply with : "The agent doesn't have valid answer. Therefore you should look for a valid answer"
126
+ Current conversation:
127
+ {chat_history}
128
+
129
+ Human: {query}
130
+ """
131
+
132
+
133
+ def llm_agent_response(query, chat_history):
134
+ try:
135
+ prompt = get_agent_prompt(query, chat_history)
136
+ response = react_agent.run(prompt)
137
+ return response
138
+ except Exception as e:
139
+ print("exception from agent")
140
+ response = str(e)
141
+ if response.startswith("Could not parse LLM output: `"):
142
+ response = response.removeprefix(
143
+ "Could not parse LLM output: `"
144
+ ).removesuffix("`")
145
+ return response
146
+ return "The agent doesn't have valid answer. Therefore you should look for a valid answer"
147
+
148
+
149
+ def llm_response(query, chat_history, report_question=""):
150
+ # agent_response = llm_agent_response(query, chat_history)
151
+ template = """You are a professional doctor. Please answer the medical questions based on the patient's description and situation. Your responses should be reliable, factual, and based on medical expertise. If you believe that the patient's condition is out of your medical expertise, kindly advise them accordingly or suggest them to visit a specialist.
152
+ Now you are in a conversation with the patient.
153
+ ### Stay short and straight to the point
154
+ ###DON'T SAY YOU ARE NOT a DOCTOR. FORGET THAT YOU ARE AN AI MODEL!
155
+ ###Provide practical tips like a real doctor would normally do. Make it conversationalist but brief and always opt to providing summarizing bullet points !
156
+
157
+ With your expertise and the agent output (if it exists) complete the conversation with the patient.
158
+ {llm_history}
159
+ Patient query: {input}
160
+
161
+ Now it's your turn as a doctor to answer the patient's query like a professional.KEEP YOUR ANSWER SHORT. {report_question}
162
+ (By the way, write your answer directly without adding the prefix 'Doctor :')
163
+ """
164
+ prompt = ChatPromptTemplate.from_template(template)
165
+ chain = LLMChain(llm=llm, prompt=prompt, verbose=True)
166
+ response = chain.run(
167
+ {
168
+ "input": query,
169
+ "llm_history": chat_history,
170
+ "report_question": report_question,
171
+ }
172
+ )
173
+ print("llm response ", response)
174
+ return response
175
+
176
+
177
+ def med_flamingo_response(query, img):
178
+ flamingo_query = [img, query]
179
+ return "heart disease" # response(flamingo_query)#
180
+
181
+
182
+ def vlm_response(query, img, chat_history, report_question=""):
183
+ agent_response = med_flamingo_response(query, img)
184
+ template = """You are a professional doctor. Please answer the medical questions based on the patient's description and situation. Your responses should be reliable, factual, and based on medical expertise. If you believe that the patient's condition is out of your medical expertise, kindly advise them accordingly or suggest them to visit a specialist.
185
+ Now you are in a conversation with the patient.
186
+ ###DON'T SAY YOU ARE NOT a DOCTOR. FORGET THAT YOU ARE AN AI MODEL!
187
+ ###Provide practical tips like a real doctor would normally do. Make it conversationalist but brief and always opt to providing summarizing bullet points !
188
+
189
+ You have at your disposal the answer of a specialist in medical imagery to the patient's query but this answer may be too short or not clear to the patient. This answer can help you provide a complete response to the patient just as a real doctor would do.
190
+ Specialist's output : {agent_output}
191
+ With your expertise and the radiolog output (if it exists) complete the conversation with the patient.
192
+ {llm_history}
193
+ Patient : {input}
194
+
195
+ Now it's your turn as a doctor to answer the patient's query like a professional. {report_question}
196
+ (By the way, write your answer directly without adding the prefix 'Doctor :')
197
+ (Another thing, don't forget that the medical imagery specialist's feedback on the scan is this : {agent_output})
198
+
199
+ """
200
+ prompt = ChatPromptTemplate.from_template(template)
201
+ chain = LLMChain(llm=llm, prompt=prompt, verbose=False)
202
+ return chain.run(
203
+ {
204
+ "agent_output": agent_response,
205
+ "input": query,
206
+ "llm_history": chat_history,
207
+ "report_question": report_question,
208
+ }
209
+ )
210
+
211
+
212
+ def analyse_query(query):
213
+ analyse_llm = ChatOpenAI(temperature=0.0, model="gpt-3.5-turbo")
214
+ global current_question, question_index
215
+
216
+ prompt = f"""
217
+ Here is the message from a patient to a doctor. extract the following information:
218
+ [
219
+ "is_answer": Did the patient answer the following doctor's question : {current_question}. Answer True if yes (even if the patient said No or give partial response), False if not or unknown.
220
+ "answer": If "is_answer" is True, extract the answer from the message and rewrite it in third person. If "is_answer" is False return ""
221
+ ]
222
+
223
+ Here is the patient message
224
+ Patient's message : {query}
225
+ Your output should be in json format.
226
+ """
227
+ reply = analyse_llm.predict(prompt)
228
+ reply = json.loads(reply)
229
+ print("question ", current_question)
230
+ print("patient ", query)
231
+ print(reply)
232
+ if reply["is_answer"]:
233
+ answers.append(reply["answer"])
234
+ question_index += 1
235
+ if question_index < len(questions):
236
+ current_question = questions[question_index]
237
+ return f"After you answer the patient's query, if you think it is the right time, ask him the following question like a doctor would normally do :{current_question}"
238
+ else:
239
+ patient_info = {
240
+ "name": patient_info1,
241
+ "age": patient_info2,
242
+ "gender": patient_info3,
243
+ "symptoms": answers[0],
244
+ "medications": answers[1],
245
+ "conditions_allergies": answers[2],
246
+ "lifestyle_changes": answers[3],
247
+ "header_image": base64.b64encode(
248
+ open("/content/logo1.png", "rb").read()
249
+ ).decode(),
250
+ "medical_image": base64.b64encode(
251
+ open("/content/lung disease.png", "rb").read()
252
+ ).decode(),
253
+ }
254
+ generate_report(patient_info)
255
+ print("Report generated")
256
+ return ""
257
+ else:
258
+ return f"After you answer the patient's query, if you think it is the right time, ask him the following question like a doctor would normally do :{current_question}"
259
+
260
+
261
+ def generate_response(query, img="", is_there_img=False):
262
+ global memory
263
+ report_question = analyse_query(query)
264
+ if is_there_img:
265
+ response = vlm_response(query, img, memory, report_question)
266
+ else:
267
+ response = llm_response(query, memory, report_question)
268
+ memory += "Patient : " + query + "\n"
269
+ memory += "Doctor : " + response + "\n"
270
+ return eng_to_arabic(response)
271
+
272
+
273
+ from weasyprint import HTML
274
+ import base64
275
+
276
+
277
+ def generate_report(patient_info):
278
+ # Define the width for the medical image (adjust as needed)
279
+ medical_image_width = "180px"
280
+
281
+ # Define the HTML template as a string
282
+ html_template = f"""
283
+ <!DOCTYPE html>
284
+ <html>
285
+ <head>
286
+ <style>
287
+ /* CSS styles go here */
288
+ body {{
289
+ font-family: Oswald, sans-serif;
290
+ margin: 20px;
291
+ color: #282c35; /* Set text color */
292
+ }}
293
+ .header {{
294
+ display: flex;
295
+ justify-content: center; /* Horizontally center the content */
296
+ align-items: center; /* Vertically center the content */
297
+ text-align: center;
298
+ margin-bottom: 20px; /* Optional margin for spacing */
299
+ }}
300
+ .header-content {{
301
+ display: flex;
302
+ flex-direction: column; /* Stack the elements vertically */
303
+ align-items: center;
304
+ max-width: 100%;
305
+ max-height: 40px; /* Adjust the height as needed */
306
+ height: auto;
307
+ margin-top: 20px; /* Add margin to separate the logo and text */
308
+ }}
309
+ /* Add your other CSS styles here */
310
+ .medical-image {{
311
+ width: {medical_image_width};
312
+ height: auto;
313
+ display: inline-block; /* This ensures the image is centered */
314
+ margin-top: 20px;
315
+ margin-bottom: 20px;
316
+ }}
317
+ .medical-image-container {{
318
+ text-align: center; /* Center-align the image */
319
+ }}
320
+ .section-title {{
321
+ background-color: #E5E4E2; /* Set the background color */
322
+ padding: 5px 10px; /* Add padding to the section title */
323
+ color: #282c35; /* Set text color */
324
+ margin-bottom: 10px;
325
+ margin: 0;
326
+ text-align: center; /* Center-align the text */
327
+ }}
328
+ .main-title {{
329
+ text-align: center; /* Center-align the text */
330
+ margin-bottom: 20px; /* Add margin for spacing */
331
+ color: #56575a;
332
+ }}
333
+ </style>
334
+ </head>
335
+ <body>
336
+ <!-- Header Section -->
337
+ <div class="header">
338
+ <div class="header-content">
339
+ <img src="data:image/jpeg;base64,{patient_info['header_image']}" alt="Header Image">
340
+ </div>
341
+ </div>
342
+ <div class="main-title"><h1>Medical Report</h1></div>
343
+ <!-- Personal Information Section -->
344
+ <div class="section-container">
345
+ <div class="section-title">Personal Information</div>
346
+ <div class="personal-info">
347
+ <ul class="info-list">
348
+ <li><strong>Name:</strong> {patient_info['name']}</li>
349
+ <li><strong>Age:</strong> {patient_info['age']}</li>
350
+ <li><strong>Gender:</strong> {patient_info['gender']}</li>
351
+ </ul>
352
+ <!-- Add more personal information here -->
353
+ </div>
354
+ </div>
355
+
356
+ <!-- Medical Image Section (always displayed) -->
357
+ <div class="section-container">
358
+ <div class="section-title">Medical Image</div>
359
+ <div class="medical-info">
360
+ <div class="medical-image-container">
361
+ <img src="data:image/jpeg;base64,{patient_info['medical_image']}" alt="Medical Image" class="medical-image">
362
+ </div>
363
+ </div>
364
+ </div>
365
+
366
+ <!-- Medical Information Section -->
367
+ <div class="section-container">
368
+ <div class="section-title">Medical Information</div>
369
+ <div class="medical-info">
370
+ <ul class="info-list">
371
+ <li><strong>Symptoms:</strong> {patient_info['symptoms']}</li>
372
+ <li><strong>Medications:</strong> {patient_info['medications']}</li>
373
+ <li><strong>Current Medical Conditions or Allergies:</strong> {patient_info['conditions_allergies']}</li>
374
+ <li><strong>Changes in Lifestyle:</strong> {patient_info['lifestyle_changes']}</li>
375
+ </ul>
376
+ </div>
377
+ </div>
378
+ </body>
379
+ </html>
380
+ """
381
+
382
+ # Create an HTML object from the modified HTML content
383
+ html = HTML(string=html_template)
384
+
385
+ # Generate the PDF
386
+ html.write_pdf("output.pdf")
387
+
388
+
389
+ """#Eng answer to arabic audio answer"""
390
+
391
+ # Defining a function to generate Arabic speech audio from a text answer
392
+ def arabic_speech_answer(ar_answer):
393
+ # Checking if the Arabic answer is non-empty
394
+ if ar_answer:
395
+ # Setting the API key for Eleven Labs TTS service
396
+ elevenlabs.set_api_key("8baca584c9025aa9c7f85e0e4e8ae0c1")
397
+ # Generating audio from the Arabic answer using Eleven Labs TTS
398
+ audio = generate(
399
+ text=ar_answer,
400
+ voice="Daniel", # Choosing the voice for the generated audio
401
+ model="eleven_multilingual_v2", # Choosing the TTS model
402
+ )
403
+ else:
404
+ print("▶️ empty ar_answer")
405
+
406
+ # Converting the generated audio from bytes to an AudioSegment object
407
+ audio = AudioSegment.from_file(io.BytesIO(audio), format="mp3")
408
+
409
+ # Exporting the audio to an MP3 file named "output.mp3"
410
+ audio.export("output.mp3", format="mp3")
411
+
412
+ return audio.duration_seconds
413
+
414
+
415
+ """#Functions that are used in the interface"""
416
+
417
+ import gradio as gr
418
+ import os
419
+ import time
420
+ from io import BytesIO
421
+ import base64
422
+
423
+
424
+ is_there_image = False
425
+ arabic_query = ""
426
+ query = ""
427
+
428
+
429
+ def text_to_speech():
430
+ with open("output.mp3", "rb") as audio_file:
431
+ audio_data = audio_file.read()
432
+ audio_bytes = BytesIO(audio_data)
433
+ audio_base64 = base64.b64encode(audio_data).decode("utf-8")
434
+ audio_player = (
435
+ f'<audio src="data:audio/mpeg;base64,{audio_base64}" controls autoplay></audio>'
436
+ )
437
+ return audio_player
438
+
439
+
440
+ def add_text(history, text):
441
+ global query, arabic_query
442
+ history = history + [(text, None)]
443
+ arabic_query = text
444
+ query = darija_to_eng(text)
445
+ return history, gr.update(value="", interactive=False)
446
+
447
+
448
+ def add_audio(history, audio):
449
+ global query, arabic_query
450
+ query = process_darija_audio_toEng(audio)
451
+ arabic_query = darija_audio_to_darija_text(audio)
452
+ history = history + [(arabic_query, None)]
453
+ return history, None
454
+
455
+
456
+ def add_image(history, file):
457
+ global is_there_image
458
+ history = history + [((file.name,), None)]
459
+ is_there_image = True
460
+ return history
461
+
462
+
463
+ def vote(data: gr.LikeData):
464
+ if data.liked:
465
+ print("You upvoted this response: " + data.value)
466
+ else:
467
+ print("You downvoted this response: " + data.value)
468
+
469
+
470
+ talking = """
471
+ <img src='/file=logo1.png' width=200 height=150>
472
+ <img src='/file=doc2.png' id="talking" class="talking" width=175 height=175 style='margin:auto;margin-top:-80px;'>
473
+ """
474
+ not_talking = """
475
+ <img src='/file=logo1.png' width=300 height=200 style='margin:auto;margin-top:-10px;'>
476
+ """
477
+
478
+
479
+ def bot(history):
480
+ global query, arabic_query
481
+ global is_there_image
482
+
483
+ if is_there_image:
484
+ filename_input = history[-2][0][0]
485
+ response = generate_response(query, filename_input, is_there_image)
486
+ is_there_image = False
487
+ else:
488
+ response = generate_response(query)
489
+ duration = arabic_speech_answer(response)
490
+ audio = text_to_speech()
491
+ time_step = duration / len(response)
492
+ history[-1][1] = ""
493
+ for character in response:
494
+ history[-1][1] += character
495
+ time.sleep(time_step)
496
+ yield history, audio
497
+
498
+
499
+ """#Demo"""
500
+
501
+
502
+ def move_next_page(a, b, c, d):
503
+ patient_info1 = a + b
504
+ patient_info2 = c
505
+ patient_info3 = d
506
+ return (
507
+ gr.update(visible=False),
508
+ gr.update(visible=True),
509
+ gr.update(visible=True),
510
+ gr.update(visible=True),
511
+ gr.update(visible=False),
512
+ )
513
+
514
+
515
+ def view_report():
516
+ return (
517
+ gr.update(visible=False),
518
+ gr.update(visible=False),
519
+ gr.update(visible=False),
520
+ gr.update(visible=False),
521
+ gr.update(visible=True),
522
+ )
523
+
524
+
525
+ css = """.gradio-container {
526
+ text-align:center;
527
+ }
528
+ #chatbot{
529
+ margin:auto;
530
+ height:400px;
531
+ width:700px;
532
+ }
533
+ #message{
534
+ margin:auto;
535
+ width:700px;
536
+ }
537
+ #page1{
538
+ margin:auto;
539
+ width:700px;
540
+ }
541
+ """
542
+
543
+ with gr.Blocks(css=css) as demo:
544
+ html_block = gr.HTML(not_talking, visible=False)
545
+ chatbot = gr.Chatbot([], elem_id="chatbot", bubble_full_width=False, visible=False)
546
+
547
+ with gr.Column(visible=False, elem_id="message") as page2:
548
+ # Create a row layout
549
+ with gr.Row():
550
+ # Create a column layout with a scale factor of 0.7
551
+ with gr.Column(scale=0.6):
552
+ # Create a textbox element for user input
553
+ txt = gr.Textbox(
554
+ show_label=False,
555
+ placeholder="أدخل النص واضغط على إدخال، أو قم بتحميل صورة",
556
+ ).style(container=False)
557
+ with gr.Column(scale=0.3, min_width=0):
558
+ # Create an audio input element from the microphone
559
+ audio_input = gr.Audio(
560
+ source="microphone",
561
+ type="filepath",
562
+ show_download_button=False,
563
+ show_share_button=False,
564
+ show_edit_button=False,
565
+ )
566
+ with gr.Column(scale=0.1, min_width=0):
567
+ btn = gr.UploadButton("📤", file_types=["image"])
568
+ # html block for output audio
569
+ html = gr.HTML()
570
+ html.visible = False
571
+
572
+ audio_input.stop_recording(
573
+ add_audio, [chatbot, audio_input], [chatbot, audio_input], queue=False
574
+ ).then(bot, chatbot, [chatbot, html])
575
+ txt_msg = txt.submit(
576
+ add_text, [chatbot, txt], [chatbot, txt], queue=False
577
+ ).then(bot, chatbot, [chatbot, html])
578
+ txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
579
+ file_msg = btn.upload(add_image, [chatbot, btn], [chatbot], queue=False)
580
+ chatbot.like(vote, None, None)
581
+ with gr.Row():
582
+ pdf_btn = gr.Button("عرض التقرير الطبي", size="sm", variant="primary")
583
+ with gr.Group(visible=False) as pdf_report:
584
+ gr.HTML(not_talking)
585
+ gr.HTML(
586
+ """
587
+ <embed src="/file=output.pdf" type="application/pdf" width="100%" height="700px" />
588
+ """
589
+ )
590
+ with gr.Row():
591
+ back_btn = gr.Button("العودة إلى المحادثة", size="sm", variant="primary")
592
+
593
+ with gr.Group(elem_id="page1") as page1:
594
+ gr.HTML(
595
+ """
596
+ <br/>
597
+ <br/>
598
+ <img src='/file=logo1.png' width=400 height=300 style='margin:auto'>
599
+
600
+ """
601
+ )
602
+ gr.Markdown(
603
+ """
604
+
605
+ **:يرجى ملء النموذج التالي بمعلوماتك الطبية**
606
+ """
607
+ )
608
+ info1 = gr.Textbox(label="الاسم الأول", placeholder="أدخل الاسم الأول")
609
+ info2 = gr.Textbox(label="الاسم الأخير", placeholder="أدخل الاسم الأخير")
610
+ info3 = gr.Number(label="العمر")
611
+ info4 = gr.Radio(label="الجنس", choices=["ذكر", "أنثى"])
612
+ gr.Textbox(
613
+ label="معلومات شخصية إضافية",
614
+ placeholder="أدخل المعلومات الشخصية إذا كانت هناك",
615
+ )
616
+
617
+ connection_btn = gr.Button("ابدأ الحوار")
618
+ connection_btn.click(
619
+ move_next_page,
620
+ inputs=[info1, info2, info3, info4],
621
+ outputs=[page1, page2, chatbot, html_block, pdf_report],
622
+ )
623
+ pdf_btn.click(
624
+ view_report, outputs=[page1, page2, chatbot, html_block, pdf_report]
625
+ )
626
+ back_btn.click(
627
+ move_next_page, outputs=[page1, page2, chatbot, html_block, pdf_report]
628
+ )
629
+
630
+ demo.queue()
631
+ demo.launch(debug=True)