xww033 commited on
Commit
bf29c6d
1 Parent(s): e7d4cd2
Files changed (3) hide show
  1. app.py +6 -64
  2. app_bak.py +0 -103
  3. app_new.py +161 -0
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  from threading import Thread
3
  from typing import Iterator
4
- import re
5
  import gradio as gr
6
  import spaces
7
  import torch
@@ -21,8 +21,8 @@ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
21
 
22
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
23
 
24
- model_id = "SeaLLMs/med-seallm3-7b-sftv2-department-recommend"
25
-
26
  tokenizer = Qwen2TokenizerFast.from_pretrained(model_id)
27
  model = AutoModelForCausalLM.from_pretrained(
28
  model_id,
@@ -33,30 +33,7 @@ model.config.sliding_window = 4096
33
  model.eval()
34
 
35
 
36
-
37
  system_prompt = "You are a helpful assistant."
38
- mergeddepartment2hospital_eka = {'Akupunktur': [('EKA Hospital Bekasi', 'Akupuntur Medik'), ('EKA Hospital Pekanbaru', 'Akupuntur Medik'), ('EKA Hospital BSD', 'Spesialis Akupunktur Medik')], 'Spesialis Anak': [('EKA Hospital Bekasi', 'Spesialis Anak'), ('EKA Hospital BSD', 'Spesialis Anak'), ('EKA Hospital Cibubur', 'Spesialis Anak'), ('EKA Hospital Permata Hijau', 'Spesialis Anak'), ('EKA Hospital Pekanbaru', 'Spesialis Anak'), ('EKA Hospital Family', 'Dokter Spesialis Anak'), ('EKA Hospital Grand Family', 'Dokter Spesialis Anak')], 'Tumbuh kembang anak': [('EKA Hospital Bekasi', 'Tumbuh Kembang Anak'), ('EKA Hospital Pekanbaru', 'Tumbuh Kembang Anak')], 'Bedah Anak': [('EKA Hospital Bekasi', 'Bedah Anak'), ('EKA Hospital Pekanbaru', 'Bedah Anak'), ('EKA Hospital BSD', 'Spesialis Bedah Anak'), ('EKA Hospital Cibubur', 'Spesialis Bedah Anak'), ('EKA Hospital Grand Family', 'Dokter Spesialis Bedah Anak')], 'Bedah Digestif': [('EKA Hospital Bekasi', 'Bedah Digestif'), ('EKA Hospital Pekanbaru', 'Bedah Digestif'), ('EKA Hospital BSD', 'Spesialis Bedah Digestif'), ('EKA Hospital Cibubur', 'Spesialis Bedah Digestif')], 'Bedah Mulut': [('EKA Hospital Bekasi', 'Bedah Mulut'), ('EKA Hospital Pekanbaru', 'Bedah Mulut'), ('EKA Hospital BSD', 'Spesialis Bedah Mulut'), ('EKA Hospital Cibubur', 'Spesialis Bedah Mulut'), ('EKA Hospital Grand Family', 'Spesialis Bedah Mulut Dan Maksilofasial')], 'Bedah Onkologi': [('EKA Hospital Bekasi', 'Bedah Onkologi'), ('EKA Hospital Pekanbaru', 'Bedah Onkologi'), ('EKA Hospital BSD', 'Spesialis Bedah Onkologi'), ('EKA Hospital Cibubur', 'Spesialis Bedah Onkologi')], 'Bedah Plastik': [('EKA Hospital Bekasi', 'Bedah Plastik'), ('EKA Hospital Pekanbaru', 'Bedah Plastik'), ('EKA Hospital BSD', 'Spesialis Bedah Plastik'), ('EKA Hospital Cibubur', 'Spesialis Bedah Plastik')], 'Bedah Saraf': [('EKA Hospital Bekasi', 'Bedah Saraf'), ('EKA Hospital Pekanbaru', 'Bedah Saraf'), ('EKA Hospital BSD', 'Spesialis Bedah Saraf'), ('EKA Hospital Cibubur', 'Spesialis Bedah Saraf')], 'Bedah Thoraks Kardiovaskular': [('EKA Hospital Bekasi', 'Bedah Thoraks Kardiovaskular'), ('EKA Hospital Pekanbaru', 'Bedah Thoraks Kardiovaskular'), ('EKA Hospital BSD', 'Spesialis Bedah Toraks, Kardiak dan Vaskular'), ('EKA Hospital Cibubur', 'Spesialis Bedah Toraks, Kardiak dan Vaskular')], 'Bedah Umum': [('EKA Hospital Bekasi', 'Bedah Umum'), ('EKA Hospital Permata Hijau', 'Bedah Umum'), ('EKA Hospital Pekanbaru', 'Bedah Umum'), ('EKA Hospital BSD', 'Spesialis Bedah Umum'), ('EKA Hospital Cibubur', 'Spesialis Bedah Umum'), ('EKA Hospital Family', 'Dokter Spesialis Bedah'), ('EKA Hospital Grand Family', 'Dokter Spesialis Bedah')], 'Bedah Vaskular': [('EKA Hospital Bekasi', 'Bedah Vaskular'), ('EKA Hospital Pekanbaru', 'Bedah Vaskular'), ('EKA Hospital BSD', 'Spesialis Bedah Vaskular'), ('EKA Hospital Cibubur', 'Spesialis Bedah Vaskular')], 'Farmakologi Klinis': [('EKA Hospital Bekasi', 'Farmakologi Klinis'), ('EKA Hospital Pekanbaru', 'Farmakologi Klinis')], 'Dokter Gigi': [('EKA Hospital Bekasi', 'Dokter Gigi'), ('EKA Hospital Permata Hijau', 'Dokter Gigi'), ('EKA Hospital Pekanbaru', 'Dokter Gigi'), ('EKA Hospital Family', 'Dokter Gigi'), ('EKA Hospital Grand Family', 'Dokter Gigi'), ('EKA Hospital BSD', 'Gigi Umum'), ('EKA Hospital Cibubur', 'Gigi Umum')], 'Gizi Klinik': [('EKA Hospital Bekasi', 'Gizi Klinik'), ('EKA Hospital Pekanbaru', 'Gizi Klinik'), ('EKA Hospital BSD', 'Spesialis Gizi Klinik'), ('EKA Hospital Cibubur', 'Spesialis Gizi Klinik'), ('EKA Hospital Family', 'Dokter Spesialis Gizi Klinik')], 'Jantung & Pembuluh Darah': [('EKA Hospital Bekasi', 'Jantung & Pembuluh Darah'), ('EKA Hospital Permata Hijau', 'Jantung & Pembuluh Darah'), ('EKA Hospital Pekanbaru', 'Jantung & Pembuluh Darah'), ('EKA Hospital BSD', 'Spesialis Jantung dan Pembuluh Darah'), ('EKA Hospital Cibubur', 'Spesialis Jantung dan Pembuluh Darah')], 'Jantung Anak': [('EKA Hospital Bekasi', 'Jantung Anak'), ('EKA Hospital Pekanbaru', 'Jantung Anak'), ('EKA Hospital BSD', 'Spesialis Jantung Anak')], 'Kebidanan & Kandungan': [('EKA Hospital Bekasi', 'Kebidanan & Kandungan'), ('EKA Hospital Pekanbaru', 'Kebidanan & Kandungan'), ('EKA Hospital Permata Hijau', 'Kandungan & Kebidanan'), ('EKA Hospital BSD', 'Spesialis Kebidanan dan Kandungan'), ('EKA Hospital Cibubur', 'Spesialis Kebidanan dan Kandungan'), ('Pusat Unggulan', 'Pusat Layanan Kebidanan & Kandungan'), ('EKA Hospital Family', 'Dokter Spesialis Kandungan'), ('EKA Hospital Grand Family', 'Dokter Spesialis Kandungan')], 'Kebidanan Onkologi': [('EKA Hospital Bekasi', 'Kebidanan Onkologi'), ('EKA Hospital Pekanbaru', 'Kebidanan Onkologi'), ('EKA Hospital BSD', 'Spesialis Ginekologi Onkologi'), ('EKA Hospital Cibubur', 'Spesialis Ginekologi Onkologi')], 'Kedokteran Fisik & Rehabilitasi': [('EKA Hospital Bekasi', 'Kedokteran Fisik & Rehabilitasi'), ('EKA Hospital Pekanbaru', 'Kedokteran Fisik & Rehabilitasi'), ('EKA Hospital BSD', 'Spesialis Kedokteran Fisik dan Rehabilitasi'), ('EKA Hospital Cibubur', 'Spesialis Kedokteran Fisik dan Rehabilitasi'), ('EKA Hospital Family', 'Dokter Spesialis Kedokteran Fisik Dan Rehabilitasi'), ('EKA Hospital Grand Family', 'Dokter Spesialis Kedokteran Fisik Dan Rehabilitasi')], 'Kedokteran Okupasi': [('EKA Hospital Bekasi', 'Kedokteran Okupasi'), ('EKA Hospital Pekanbaru', 'Kedokteran Okupasi')], 'Kedokteran Olahraga': [('EKA Hospital Bekasi', 'Kedokteran Olahraga'), ('EKA Hospital Pekanbaru', 'Kedokteran Olahraga'), ('EKA Hospital BSD', 'Spesialis Kedokteran Olahraga')], 'Kesehatan Gigi Anak': [('EKA Hospital Bekasi', 'Kesehatan Gigi Anak'), ('EKA Hospital Pekanbaru', 'Kesehatan Gigi Anak'), ('EKA Hospital BSD', 'Spesialis Kesehatan Gigi Anak'), ('EKA Hospital Cibubur', 'Spesialis Kesehatan Gigi Anak'), ('EKA Hospital Family', 'Dokter Spesialis Kesehatan Gigi Anak'), ('EKA Hospital Grand Family', 'Dokter Spesialis Kesehatan Gigi Anak')], 'Konservasi Gigi': [('EKA Hospital Bekasi', 'Konservasi Gigi'), ('EKA Hospital Permata Hijau', 'Konservasi Gigi'), ('EKA Hospital Pekanbaru', 'Konservasi Gigi'), ('EKA Hospital BSD', 'Spesialis Konservasi Gigi'), ('EKA Hospital Cibubur', 'Spesialis Konservasi Gigi'), ('EKA Hospital Grand Family', 'Dokter Spesialis Konservasi Gigi')], 'Kulit & Kelamin': [('EKA Hospital Bekasi', 'Kulit & Kelamin'), ('EKA Hospital Pekanbaru', 'Kulit & Kelamin'), ('EKA Hospital BSD', 'Spesialis Kulit dan Kelamin'), ('EKA Hospital Cibubur', 'Spesialis Kulit dan Kelamin'), ('EKA Hospital Permata Hijau', 'Dermatologi & Venereologi'), ('EKA Hospital Family', 'Dermatologi & Venerologi'), ('EKA Hospital Grand Family', 'Dermatologi & Venerologi')], 'Spesialis Mata': [('EKA Hospital Bekasi', 'Spesialis Mata'), ('EKA Hospital BSD', 'Spesialis Mata'), ('EKA Hospital Cibubur', 'Spesialis Mata'), ('EKA Hospital Pekanbaru', 'Spesialis Mata'), ('EKA Hospital Permata Hijau', 'Dokter Mata')], 'Mikrobiologi Klinik': [('EKA Hospital Bekasi', 'Mikrobiologi Klinik'), ('EKA Hospital Pekanbaru', 'Mikrobiologi Klinik'), ('EKA Hospital BSD', 'Spesialis Mikrobiologi Klinik'), ('EKA Hospital Cibubur', 'Spesialis Mikrobiologi Klinik')], 'Orthodonti': [('EKA Hospital Bekasi', 'Orthodonti'), ('EKA Hospital Pekanbaru', 'Orthodonti'), ('EKA Hospital BSD', 'Spesialis Ortodonsia'), ('EKA Hospital Cibubur', 'Spesialis Ortodonsia')], 'Orthopedi & Traumatologi': [('EKA Hospital Bekasi', 'Orthopedi & Traumatologi'), ('EKA Hospital Permata Hijau', 'Orthopedi & Traumatologi'), ('EKA Hospital Pekanbaru', 'Orthopedi & Traumatologi'), ('EKA Hospital Grand Family', 'Orthopedi & Traumatologi'), ('EKA Hospital BSD', 'Spesialis Orthopedi Traumatologi'), ('EKA Hospital Cibubur', 'Spesialis Orthopedi Traumatologi')], 'Orthopedi: Cedera Bahu & Tangan': [('EKA Hospital Bekasi', 'Orthopedi: Cedera Bahu & Tangan'), ('EKA Hospital Pekanbaru', 'Orthopedi: Cedera Bahu & Tangan'), ('EKA Hospital BSD', 'Spesialis Orthopedi Cedera Bahu dan Tangan')], 'Orthopedi: Pediatrik/Anak': [('EKA Hospital Bekasi', 'Orthopedi: Pediatrik/anak'), ('EKA Hospital Pekanbaru', 'Orthopedi: Pediatrik/anak'), ('EKA Hospital BSD', 'Spesialis Orthopedi Pediatrik/Anak'), ('EKA Hospital Cibubur', 'Spesialis Orthopedi Pediatrik/Anak')], 'Orthopedi: Sendi Lutut & Pinggul': [('EKA Hospital Bekasi', 'Orthopedi: Sendi Lutut & Pinggul'), ('EKA Hospital Pekanbaru', 'Orthopedi: Sendi Lutut & Pinggul'), ('EKA Hospital BSD', 'Spesialis Orthopedi: Sendi Lutut dan Pinggul'), ('EKA Hospital Cibubur', 'Spesialis Orthopedi: Sendi Lutut dan Pinggul')], 'Orthopedi: Tulang Belakang': [('EKA Hospital Bekasi', 'Orthopedi: Tulang Belakang'), ('EKA Hospital Pekanbaru', 'Orthopedi: Tulang Belakang'), ('EKA Hospital BSD', 'Spesialis Orthopedi Tulang Belakang'), ('EKA Hospital Cibubur', 'Spesialis Orthopedi Tulang Belakang')], 'Orthopedi: Kaki dan Pergelangan Kaki': [('EKA Hospital BSD', 'Spesialis Orthopedi: Kaki dan Pergelangan Kaki')], 'Spesialis Paru': [('EKA Hospital Bekasi', 'Spesialis Paru'), ('EKA Hospital Pekanbaru', 'Spesialis Paru'), ('EKA Hospital BSD', 'Spesialis Paru dan Pernapasan'), ('EKA Hospital Cibubur', 'Spesialis Paru dan Pernapasan'), ('EKA Hospital Permata Hijau', 'Paru & Pernapasan')], 'Patologi Anatomi': [('EKA Hospital Bekasi', 'Patologi Anatomi'), ('EKA Hospital Pekanbaru', 'Patologi Anatomi'), ('EKA Hospital BSD', 'Spesialis Patologi Anatomi'), ('EKA Hospital Cibubur', 'Spesialis Patologi Anatomi')], 'Patologi Klinik': [('EKA Hospital Bekasi', 'Patologi Klinik'), ('EKA Hospital Pekanbaru', 'Patologi Klinik'), ('EKA Hospital BSD', 'Spesialis Patologi Klinik'), ('EKA Hospital Cibubur', 'Spesialis Patologi Klinik')], 'Penyakit Dalam': [('EKA Hospital Bekasi', 'Penyakit Dalam'), ('EKA Hospital Permata Hijau', 'Penyakit Dalam'), ('EKA Hospital Pekanbaru', 'Penyakit Dalam'), ('EKA Hospital BSD', 'Spesialis Penyakit Dalam'), ('EKA Hospital Cibubur', 'Spesialis Penyakit Dalam'), ('EKA Hospital Family', 'Dokter Spesialis Penyakit Dalam'), ('EKA Hospital Grand Family', 'Dokter Spesialis Penyakit Dalam')], 'Penyakit Dalam: Hemato-Onkologi': [('EKA Hospital Bekasi', 'Penyakit Dalam: Hemato-onkologi'), ('EKA Hospital Pekanbaru', 'Penyakit Dalam: Hemato-onkologi')], 'Penyakit Dalam: Ginjal & Hipertensi': [('EKA Hospital Permata Hijau', 'Penyakit Dalam : Ginjal & Hipertensi')], 'Periodonti': [('EKA Hospital Bekasi', 'Periodonti'), ('EKA Hospital Pekanbaru', 'Periodonti')], 'Prostodonti': [('EKA Hospital Bekasi', 'Prostodonti'), ('EKA Hospital Pekanbaru', 'Prostodonti'), ('EKA Hospital BSD', 'Spesialis Prostodonsia'), ('EKA Hospital Cibubur', 'Spesialis Prostodonsia'), ('EKA Hospital Grand Family', 'Dokter Spesialis Prostodonsia')], 'Psikiatri': [('EKA Hospital Bekasi', 'Psikiatri'), ('EKA Hospital Pekanbaru', 'Psikiatri'), ('EKA Hospital BSD', 'Spesialis Psikiatri'), ('EKA Hospital Cibubur', 'Spesialis Psikiatri')], 'Psikolog Klinis': [('EKA Hospital Bekasi', 'Psikolog Klinis'), ('EKA Hospital BSD', 'Psikolog Klinis'), ('EKA Hospital Cibubur', 'Psikolog Klinis'), ('EKA Hospital Pekanbaru', 'Psikolog Klinis'), ('EKA Hospital Grand Family', 'Psikolog')], 'Spesialis Saraf': [('EKA Hospital Bekasi', 'Spesialis Saraf'), ('EKA Hospital BSD', 'Spesialis Saraf'), ('EKA Hospital Cibubur', 'Spesialis Saraf'), ('EKA Hospital Permata Hijau', 'Spesialis Saraf'), ('EKA Hospital Pekanbaru', 'Spesialis Saraf'), ('EKA Hospital Permata Hijau', 'Neurologi')], 'THT-KL': [('EKA Hospital Bekasi', 'THT-KL'), ('EKA Hospital Pekanbaru', 'THT-KL'), ('EKA Hospital BSD', 'Spesialis THT-KL'), ('EKA Hospital Cibubur', 'Spesialis THT-KL'), ('EKA Hospital Permata Hijau', 'THT')], 'Urologi': [('EKA Hospital Bekasi', 'Urologi'), ('EKA Hospital Permata Hijau', 'Urologi'), ('EKA Hospital Pekanbaru', 'Urologi'), ('Pusat Unggulan', 'Urology & Couple Clinic'), ('EKA Hospital BSD', 'Spesialis Urologi'), ('EKA Hospital Cibubur', 'Spesialis Urologi')], 'Dokter Umum': [('EKA Hospital BSD', 'Dokter Umum'), ('EKA Hospital Cibubur', 'Dokter Umum')], 'Laktasi': [('EKA Hospital Family', 'Dokter Laktasi'), ('EKA Hospital Grand Family', 'Dokter Laktasi')], 'Rehabilitasi Medik': [('EKA Hospital Permata Hijau', 'Rehabilitasi Medik')], 'Anestesi': [('EKA Hospital Bekasi', 'Anastesi'), ('EKA Hospital Pekanbaru', 'Anastesi'), ('EKA Hospital BSD', 'Spesialis Anestesi'), ('EKA Hospital Cibubur', 'Spesialis Anestesi')], 'Radiologi': [('EKA Hospital BSD', 'Spesialis Radiologi'), ('EKA Hospital Cibubur', 'Spesialis Radiologi'), ('EKA Hospital Family', 'Dokter Spesialis Radiologi'), ('EKA Hospital Grand Family', 'Dokter Spesialis Radiologi')], 'Dokter Lainnya': [('EKA Hospital Family', 'Dokter Lainnya')], 'Pusat Layanan Kesehatan Anak Terpadu': [('Pusat Unggulan', 'Pusat Layanan Kesehatan Anak Terpadu')], 'Eka Tjipta Widjaja Cancer Center': [('Pusat Unggulan', 'ETWCC: Eka Tjipta Widjaja Cancer Center')], 'Gatam Institute: Pusat Ortopedi & Tulang Belakang': [('Pusat Unggulan', 'Gatam Institute: Pusat Ortopedi & Tulang Belakang')], 'Mycardia: Pusat Layanan Jantung dan Pembuluh Darah': [('Pusat Unggulan', 'MYcardia: Pusat Layanan Jantung dan Pembuluh Darah')], 'Pusat Neonatal Intensive Care (NICU)': [('Pusat Unggulan', 'Pusat Neonatal Intensive Care (NICU)')], 'Diabetes Connection Care: Pusat Diabetes Terintegrasi': [('Pusat Unggulan', 'Diabetes Connection Care: Pusat Diabetes Terintegrasi')], 'Divine: Digestive Intervention & Endoscopy Center': [('Pusat Unggulan', 'DIVINE: Digestive Intervention & Endoscopy Center')], 'Spesialis kedokteran penerbangan': [('EKA Hospital BSD', 'Spesialis Kedokteran Penerbangan')], 'Anak: hemato-onkologi': [('EKA Hospital Bekasi', 'Anak: Hemato-onkologi'), ('EKA Hospital Pekanbaru', 'Anak: Hemato-onkologi')]}
39
- def extract_list(text, type):
40
- pattern = rf'<{type}>(.*?)</{type}>'
41
- matches = re.findall(pattern, text)
42
- matches = [x.strip() for x in matches]
43
- return matches
44
-
45
- def check_department_eka_simple(text):
46
- departments = extract_list(text, 'd')
47
- good_department = {x.lower():1 for x in mergeddepartment2hospital_eka.keys()}
48
- if departments:
49
- good = True
50
- for item in departments:
51
- if item.lower() in good_department:
52
- continue
53
- else:
54
- good = False
55
- break
56
- else:
57
- good = False
58
- return good, departments
59
-
60
 
61
 
62
  @spaces.GPU(duration=90)
@@ -69,16 +46,7 @@ def generate(
69
  top_k: int = 50,
70
  repetition_penalty: float = 1.2,
71
  ) -> Iterator[str]:
72
- # conversation = [{"role": "system", "content": system_prompt}]
73
- conversation = []
74
- department_text = f"{list(mergeddepartment2hospital_eka.keys())}"
75
- message = user_text = f'''Department:
76
- {department_text}
77
-
78
- Question:
79
- {message}
80
-
81
- Please reply to the above question and add a disclaimer statement "Informasi yang kami berikan tidak sesuai untuk, dan tidak boleh dianggap sebagai, bentuk layanan diagnosis apapun. Anda tidak boleh mengandalkan informasi yang diberikan oleh layanan teknis kami untuk mendiagnosis atau mengobati penyakit." in your response.'''
82
  for user, assistant in chat_history:
83
  conversation.extend(
84
  [
@@ -111,35 +79,9 @@ Please reply to the above question and add a disclaimer statement "Informasi yan
111
  t.start()
112
 
113
  outputs = []
114
- department_all = ''
115
- d_flag = False
116
  for text in streamer:
117
- if (not d_flag) and "<d>" in text and "</d>" in text:
118
- department_all += department
119
- elif (not d_flag) and "<d>" in text:
120
- d_flag = True
121
- department = text
122
- elif d_flag and (not "</d>" in text):
123
- department += text
124
- elif d_flag and "</d>" in text:
125
- department += text
126
- department_all += department
127
- d_flag = False
128
- outputs.append(text.replace("<d>", "").replace("</d>", ""))
129
-
130
-
131
-
132
  yield "".join(outputs)
133
- good, departments = check_department_eka_simple(department_all)
134
- if good:
135
- recommendation_text = '\nDepartemen yang direkomendasikan di atas dapat ditemukan di beberapa cabang EKA Hospital:\n'
136
- for i_d, item in enumerate(departments):
137
- hospital = mergeddepartment2hospital_eka[item]
138
- hospital = [f" * {x[1]}, {x[0]}" for x in hospital]
139
- item_text = f"{i_d+1}. {item}:\n"
140
- hospital_text = "\n".join(hospital)
141
- recommendation_text += item_text + hospital_text + "\n"
142
- yield "".join(outputs) + recommendation_text
143
 
144
 
145
  chat_interface = gr.ChatInterface(
@@ -147,7 +89,7 @@ chat_interface = gr.ChatInterface(
147
  additional_inputs=[],
148
  stop_btn=None,
149
  examples=[
150
- ["i get a cold. Help me doctor."],
151
  ],
152
  cache_examples=False,
153
  )
 
1
  import os
2
  from threading import Thread
3
  from typing import Iterator
4
+
5
  import gradio as gr
6
  import spaces
7
  import torch
 
21
 
22
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
23
 
24
+ #model_id = "SeaLLMs/SeaLLMs-v3-7B-Chat"
25
+ model_id = "SeaLLMs/debug_4"
26
  tokenizer = Qwen2TokenizerFast.from_pretrained(model_id)
27
  model = AutoModelForCausalLM.from_pretrained(
28
  model_id,
 
33
  model.eval()
34
 
35
 
 
36
  system_prompt = "You are a helpful assistant."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
 
39
  @spaces.GPU(duration=90)
 
46
  top_k: int = 50,
47
  repetition_penalty: float = 1.2,
48
  ) -> Iterator[str]:
49
+ conversation = [{"role": "system", "content": system_prompt}]
 
 
 
 
 
 
 
 
 
50
  for user, assistant in chat_history:
51
  conversation.extend(
52
  [
 
79
  t.start()
80
 
81
  outputs = []
 
 
82
  for text in streamer:
83
+ outputs.append(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  yield "".join(outputs)
 
 
 
 
 
 
 
 
 
 
85
 
86
 
87
  chat_interface = gr.ChatInterface(
 
89
  additional_inputs=[],
90
  stop_btn=None,
91
  examples=[
92
+ ["Explain the Symptoms of Covid-19"],
93
  ],
94
  cache_examples=False,
95
  )
app_bak.py DELETED
@@ -1,103 +0,0 @@
1
- import os
2
- from threading import Thread
3
- from typing import Iterator
4
-
5
- import gradio as gr
6
- import spaces
7
- import torch
8
- from transformers import AutoModelForCausalLM, Qwen2TokenizerFast, TextIteratorStreamer
9
-
10
- # based on gemma demo code
11
-
12
- DESCRIPTION = """\
13
- # SeaLLMs Med
14
-
15
- SeaLLMs Med is a medical version of SeaLLMs
16
- """
17
-
18
- MAX_MAX_NEW_TOKENS = 2048
19
- DEFAULT_MAX_NEW_TOKENS = 1024
20
- MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
21
-
22
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
23
-
24
- #model_id = "SeaLLMs/SeaLLMs-v3-7B-Chat"
25
- model_id = "SeaLLMs/debug_4"
26
- tokenizer = Qwen2TokenizerFast.from_pretrained(model_id)
27
- model = AutoModelForCausalLM.from_pretrained(
28
- model_id,
29
- device_map="auto",
30
- torch_dtype=torch.bfloat16,
31
- )
32
- model.config.sliding_window = 4096
33
- model.eval()
34
-
35
-
36
- system_prompt = "You are a helpful assistant."
37
-
38
-
39
- @spaces.GPU(duration=90)
40
- def generate(
41
- message: str,
42
- chat_history: list[tuple[str, str]],
43
- max_new_tokens: int = 1024,
44
- temperature: float = 0.6,
45
- top_p: float = 0.9,
46
- top_k: int = 50,
47
- repetition_penalty: float = 1.2,
48
- ) -> Iterator[str]:
49
- conversation = [{"role": "system", "content": system_prompt}]
50
- for user, assistant in chat_history:
51
- conversation.extend(
52
- [
53
- {"role": "user", "content": user},
54
- {"role": "assistant", "content": assistant},
55
- ]
56
- )
57
- conversation.append({"role": "user", "content": message})
58
-
59
- input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")
60
- if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
61
- input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
62
- gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
63
- input_ids = input_ids.to(model.device)
64
-
65
- streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
66
- generate_kwargs = dict(
67
- {"input_ids": input_ids},
68
- streamer=streamer,
69
- max_new_tokens=max_new_tokens,
70
- do_sample=True,
71
- top_p=top_p,
72
- top_k=top_k,
73
- temperature=temperature,
74
- num_beams=1,
75
- repetition_penalty=repetition_penalty,
76
- eos_token_id=tokenizer.eos_token_id,
77
- )
78
- t = Thread(target=model.generate, kwargs=generate_kwargs)
79
- t.start()
80
-
81
- outputs = []
82
- for text in streamer:
83
- outputs.append(text)
84
- yield "".join(outputs)
85
-
86
-
87
- chat_interface = gr.ChatInterface(
88
- fn=generate,
89
- additional_inputs=[],
90
- stop_btn=None,
91
- examples=[
92
- ["Explain the Symptoms of Covid-19"],
93
- ],
94
- cache_examples=False,
95
- )
96
-
97
- with gr.Blocks(css="style.css", fill_height=True) as demo:
98
- gr.Markdown(DESCRIPTION)
99
- gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
100
- chat_interface.render()
101
-
102
- if __name__ == "__main__":
103
- demo.queue(max_size=20).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app_new.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from threading import Thread
3
+ from typing import Iterator
4
+ import re
5
+ import gradio as gr
6
+ import spaces
7
+ import torch
8
+ from transformers import AutoModelForCausalLM, Qwen2TokenizerFast, TextIteratorStreamer
9
+
10
+ # based on gemma demo code
11
+
12
+ DESCRIPTION = """\
13
+ # SeaLLMs Med
14
+
15
+ SeaLLMs Med is a medical version of SeaLLMs
16
+ """
17
+
18
+ MAX_MAX_NEW_TOKENS = 2048
19
+ DEFAULT_MAX_NEW_TOKENS = 1024
20
+ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
21
+
22
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
23
+
24
+ model_id = "SeaLLMs/med-seallm3-7b-sftv2-department-recommend"
25
+
26
+ tokenizer = Qwen2TokenizerFast.from_pretrained(model_id)
27
+ model = AutoModelForCausalLM.from_pretrained(
28
+ model_id,
29
+ device_map="auto",
30
+ torch_dtype=torch.bfloat16,
31
+ )
32
+ model.config.sliding_window = 4096
33
+ model.eval()
34
+
35
+
36
+
37
+ system_prompt = "You are a helpful assistant."
38
+ mergeddepartment2hospital_eka = {'Akupunktur': [('EKA Hospital Bekasi', 'Akupuntur Medik'), ('EKA Hospital Pekanbaru', 'Akupuntur Medik'), ('EKA Hospital BSD', 'Spesialis Akupunktur Medik')], 'Spesialis Anak': [('EKA Hospital Bekasi', 'Spesialis Anak'), ('EKA Hospital BSD', 'Spesialis Anak'), ('EKA Hospital Cibubur', 'Spesialis Anak'), ('EKA Hospital Permata Hijau', 'Spesialis Anak'), ('EKA Hospital Pekanbaru', 'Spesialis Anak'), ('EKA Hospital Family', 'Dokter Spesialis Anak'), ('EKA Hospital Grand Family', 'Dokter Spesialis Anak')], 'Tumbuh kembang anak': [('EKA Hospital Bekasi', 'Tumbuh Kembang Anak'), ('EKA Hospital Pekanbaru', 'Tumbuh Kembang Anak')], 'Bedah Anak': [('EKA Hospital Bekasi', 'Bedah Anak'), ('EKA Hospital Pekanbaru', 'Bedah Anak'), ('EKA Hospital BSD', 'Spesialis Bedah Anak'), ('EKA Hospital Cibubur', 'Spesialis Bedah Anak'), ('EKA Hospital Grand Family', 'Dokter Spesialis Bedah Anak')], 'Bedah Digestif': [('EKA Hospital Bekasi', 'Bedah Digestif'), ('EKA Hospital Pekanbaru', 'Bedah Digestif'), ('EKA Hospital BSD', 'Spesialis Bedah Digestif'), ('EKA Hospital Cibubur', 'Spesialis Bedah Digestif')], 'Bedah Mulut': [('EKA Hospital Bekasi', 'Bedah Mulut'), ('EKA Hospital Pekanbaru', 'Bedah Mulut'), ('EKA Hospital BSD', 'Spesialis Bedah Mulut'), ('EKA Hospital Cibubur', 'Spesialis Bedah Mulut'), ('EKA Hospital Grand Family', 'Spesialis Bedah Mulut Dan Maksilofasial')], 'Bedah Onkologi': [('EKA Hospital Bekasi', 'Bedah Onkologi'), ('EKA Hospital Pekanbaru', 'Bedah Onkologi'), ('EKA Hospital BSD', 'Spesialis Bedah Onkologi'), ('EKA Hospital Cibubur', 'Spesialis Bedah Onkologi')], 'Bedah Plastik': [('EKA Hospital Bekasi', 'Bedah Plastik'), ('EKA Hospital Pekanbaru', 'Bedah Plastik'), ('EKA Hospital BSD', 'Spesialis Bedah Plastik'), ('EKA Hospital Cibubur', 'Spesialis Bedah Plastik')], 'Bedah Saraf': [('EKA Hospital Bekasi', 'Bedah Saraf'), ('EKA Hospital Pekanbaru', 'Bedah Saraf'), ('EKA Hospital BSD', 'Spesialis Bedah Saraf'), ('EKA Hospital Cibubur', 'Spesialis Bedah Saraf')], 'Bedah Thoraks Kardiovaskular': [('EKA Hospital Bekasi', 'Bedah Thoraks Kardiovaskular'), ('EKA Hospital Pekanbaru', 'Bedah Thoraks Kardiovaskular'), ('EKA Hospital BSD', 'Spesialis Bedah Toraks, Kardiak dan Vaskular'), ('EKA Hospital Cibubur', 'Spesialis Bedah Toraks, Kardiak dan Vaskular')], 'Bedah Umum': [('EKA Hospital Bekasi', 'Bedah Umum'), ('EKA Hospital Permata Hijau', 'Bedah Umum'), ('EKA Hospital Pekanbaru', 'Bedah Umum'), ('EKA Hospital BSD', 'Spesialis Bedah Umum'), ('EKA Hospital Cibubur', 'Spesialis Bedah Umum'), ('EKA Hospital Family', 'Dokter Spesialis Bedah'), ('EKA Hospital Grand Family', 'Dokter Spesialis Bedah')], 'Bedah Vaskular': [('EKA Hospital Bekasi', 'Bedah Vaskular'), ('EKA Hospital Pekanbaru', 'Bedah Vaskular'), ('EKA Hospital BSD', 'Spesialis Bedah Vaskular'), ('EKA Hospital Cibubur', 'Spesialis Bedah Vaskular')], 'Farmakologi Klinis': [('EKA Hospital Bekasi', 'Farmakologi Klinis'), ('EKA Hospital Pekanbaru', 'Farmakologi Klinis')], 'Dokter Gigi': [('EKA Hospital Bekasi', 'Dokter Gigi'), ('EKA Hospital Permata Hijau', 'Dokter Gigi'), ('EKA Hospital Pekanbaru', 'Dokter Gigi'), ('EKA Hospital Family', 'Dokter Gigi'), ('EKA Hospital Grand Family', 'Dokter Gigi'), ('EKA Hospital BSD', 'Gigi Umum'), ('EKA Hospital Cibubur', 'Gigi Umum')], 'Gizi Klinik': [('EKA Hospital Bekasi', 'Gizi Klinik'), ('EKA Hospital Pekanbaru', 'Gizi Klinik'), ('EKA Hospital BSD', 'Spesialis Gizi Klinik'), ('EKA Hospital Cibubur', 'Spesialis Gizi Klinik'), ('EKA Hospital Family', 'Dokter Spesialis Gizi Klinik')], 'Jantung & Pembuluh Darah': [('EKA Hospital Bekasi', 'Jantung & Pembuluh Darah'), ('EKA Hospital Permata Hijau', 'Jantung & Pembuluh Darah'), ('EKA Hospital Pekanbaru', 'Jantung & Pembuluh Darah'), ('EKA Hospital BSD', 'Spesialis Jantung dan Pembuluh Darah'), ('EKA Hospital Cibubur', 'Spesialis Jantung dan Pembuluh Darah')], 'Jantung Anak': [('EKA Hospital Bekasi', 'Jantung Anak'), ('EKA Hospital Pekanbaru', 'Jantung Anak'), ('EKA Hospital BSD', 'Spesialis Jantung Anak')], 'Kebidanan & Kandungan': [('EKA Hospital Bekasi', 'Kebidanan & Kandungan'), ('EKA Hospital Pekanbaru', 'Kebidanan & Kandungan'), ('EKA Hospital Permata Hijau', 'Kandungan & Kebidanan'), ('EKA Hospital BSD', 'Spesialis Kebidanan dan Kandungan'), ('EKA Hospital Cibubur', 'Spesialis Kebidanan dan Kandungan'), ('Pusat Unggulan', 'Pusat Layanan Kebidanan & Kandungan'), ('EKA Hospital Family', 'Dokter Spesialis Kandungan'), ('EKA Hospital Grand Family', 'Dokter Spesialis Kandungan')], 'Kebidanan Onkologi': [('EKA Hospital Bekasi', 'Kebidanan Onkologi'), ('EKA Hospital Pekanbaru', 'Kebidanan Onkologi'), ('EKA Hospital BSD', 'Spesialis Ginekologi Onkologi'), ('EKA Hospital Cibubur', 'Spesialis Ginekologi Onkologi')], 'Kedokteran Fisik & Rehabilitasi': [('EKA Hospital Bekasi', 'Kedokteran Fisik & Rehabilitasi'), ('EKA Hospital Pekanbaru', 'Kedokteran Fisik & Rehabilitasi'), ('EKA Hospital BSD', 'Spesialis Kedokteran Fisik dan Rehabilitasi'), ('EKA Hospital Cibubur', 'Spesialis Kedokteran Fisik dan Rehabilitasi'), ('EKA Hospital Family', 'Dokter Spesialis Kedokteran Fisik Dan Rehabilitasi'), ('EKA Hospital Grand Family', 'Dokter Spesialis Kedokteran Fisik Dan Rehabilitasi')], 'Kedokteran Okupasi': [('EKA Hospital Bekasi', 'Kedokteran Okupasi'), ('EKA Hospital Pekanbaru', 'Kedokteran Okupasi')], 'Kedokteran Olahraga': [('EKA Hospital Bekasi', 'Kedokteran Olahraga'), ('EKA Hospital Pekanbaru', 'Kedokteran Olahraga'), ('EKA Hospital BSD', 'Spesialis Kedokteran Olahraga')], 'Kesehatan Gigi Anak': [('EKA Hospital Bekasi', 'Kesehatan Gigi Anak'), ('EKA Hospital Pekanbaru', 'Kesehatan Gigi Anak'), ('EKA Hospital BSD', 'Spesialis Kesehatan Gigi Anak'), ('EKA Hospital Cibubur', 'Spesialis Kesehatan Gigi Anak'), ('EKA Hospital Family', 'Dokter Spesialis Kesehatan Gigi Anak'), ('EKA Hospital Grand Family', 'Dokter Spesialis Kesehatan Gigi Anak')], 'Konservasi Gigi': [('EKA Hospital Bekasi', 'Konservasi Gigi'), ('EKA Hospital Permata Hijau', 'Konservasi Gigi'), ('EKA Hospital Pekanbaru', 'Konservasi Gigi'), ('EKA Hospital BSD', 'Spesialis Konservasi Gigi'), ('EKA Hospital Cibubur', 'Spesialis Konservasi Gigi'), ('EKA Hospital Grand Family', 'Dokter Spesialis Konservasi Gigi')], 'Kulit & Kelamin': [('EKA Hospital Bekasi', 'Kulit & Kelamin'), ('EKA Hospital Pekanbaru', 'Kulit & Kelamin'), ('EKA Hospital BSD', 'Spesialis Kulit dan Kelamin'), ('EKA Hospital Cibubur', 'Spesialis Kulit dan Kelamin'), ('EKA Hospital Permata Hijau', 'Dermatologi & Venereologi'), ('EKA Hospital Family', 'Dermatologi & Venerologi'), ('EKA Hospital Grand Family', 'Dermatologi & Venerologi')], 'Spesialis Mata': [('EKA Hospital Bekasi', 'Spesialis Mata'), ('EKA Hospital BSD', 'Spesialis Mata'), ('EKA Hospital Cibubur', 'Spesialis Mata'), ('EKA Hospital Pekanbaru', 'Spesialis Mata'), ('EKA Hospital Permata Hijau', 'Dokter Mata')], 'Mikrobiologi Klinik': [('EKA Hospital Bekasi', 'Mikrobiologi Klinik'), ('EKA Hospital Pekanbaru', 'Mikrobiologi Klinik'), ('EKA Hospital BSD', 'Spesialis Mikrobiologi Klinik'), ('EKA Hospital Cibubur', 'Spesialis Mikrobiologi Klinik')], 'Orthodonti': [('EKA Hospital Bekasi', 'Orthodonti'), ('EKA Hospital Pekanbaru', 'Orthodonti'), ('EKA Hospital BSD', 'Spesialis Ortodonsia'), ('EKA Hospital Cibubur', 'Spesialis Ortodonsia')], 'Orthopedi & Traumatologi': [('EKA Hospital Bekasi', 'Orthopedi & Traumatologi'), ('EKA Hospital Permata Hijau', 'Orthopedi & Traumatologi'), ('EKA Hospital Pekanbaru', 'Orthopedi & Traumatologi'), ('EKA Hospital Grand Family', 'Orthopedi & Traumatologi'), ('EKA Hospital BSD', 'Spesialis Orthopedi Traumatologi'), ('EKA Hospital Cibubur', 'Spesialis Orthopedi Traumatologi')], 'Orthopedi: Cedera Bahu & Tangan': [('EKA Hospital Bekasi', 'Orthopedi: Cedera Bahu & Tangan'), ('EKA Hospital Pekanbaru', 'Orthopedi: Cedera Bahu & Tangan'), ('EKA Hospital BSD', 'Spesialis Orthopedi Cedera Bahu dan Tangan')], 'Orthopedi: Pediatrik/Anak': [('EKA Hospital Bekasi', 'Orthopedi: Pediatrik/anak'), ('EKA Hospital Pekanbaru', 'Orthopedi: Pediatrik/anak'), ('EKA Hospital BSD', 'Spesialis Orthopedi Pediatrik/Anak'), ('EKA Hospital Cibubur', 'Spesialis Orthopedi Pediatrik/Anak')], 'Orthopedi: Sendi Lutut & Pinggul': [('EKA Hospital Bekasi', 'Orthopedi: Sendi Lutut & Pinggul'), ('EKA Hospital Pekanbaru', 'Orthopedi: Sendi Lutut & Pinggul'), ('EKA Hospital BSD', 'Spesialis Orthopedi: Sendi Lutut dan Pinggul'), ('EKA Hospital Cibubur', 'Spesialis Orthopedi: Sendi Lutut dan Pinggul')], 'Orthopedi: Tulang Belakang': [('EKA Hospital Bekasi', 'Orthopedi: Tulang Belakang'), ('EKA Hospital Pekanbaru', 'Orthopedi: Tulang Belakang'), ('EKA Hospital BSD', 'Spesialis Orthopedi Tulang Belakang'), ('EKA Hospital Cibubur', 'Spesialis Orthopedi Tulang Belakang')], 'Orthopedi: Kaki dan Pergelangan Kaki': [('EKA Hospital BSD', 'Spesialis Orthopedi: Kaki dan Pergelangan Kaki')], 'Spesialis Paru': [('EKA Hospital Bekasi', 'Spesialis Paru'), ('EKA Hospital Pekanbaru', 'Spesialis Paru'), ('EKA Hospital BSD', 'Spesialis Paru dan Pernapasan'), ('EKA Hospital Cibubur', 'Spesialis Paru dan Pernapasan'), ('EKA Hospital Permata Hijau', 'Paru & Pernapasan')], 'Patologi Anatomi': [('EKA Hospital Bekasi', 'Patologi Anatomi'), ('EKA Hospital Pekanbaru', 'Patologi Anatomi'), ('EKA Hospital BSD', 'Spesialis Patologi Anatomi'), ('EKA Hospital Cibubur', 'Spesialis Patologi Anatomi')], 'Patologi Klinik': [('EKA Hospital Bekasi', 'Patologi Klinik'), ('EKA Hospital Pekanbaru', 'Patologi Klinik'), ('EKA Hospital BSD', 'Spesialis Patologi Klinik'), ('EKA Hospital Cibubur', 'Spesialis Patologi Klinik')], 'Penyakit Dalam': [('EKA Hospital Bekasi', 'Penyakit Dalam'), ('EKA Hospital Permata Hijau', 'Penyakit Dalam'), ('EKA Hospital Pekanbaru', 'Penyakit Dalam'), ('EKA Hospital BSD', 'Spesialis Penyakit Dalam'), ('EKA Hospital Cibubur', 'Spesialis Penyakit Dalam'), ('EKA Hospital Family', 'Dokter Spesialis Penyakit Dalam'), ('EKA Hospital Grand Family', 'Dokter Spesialis Penyakit Dalam')], 'Penyakit Dalam: Hemato-Onkologi': [('EKA Hospital Bekasi', 'Penyakit Dalam: Hemato-onkologi'), ('EKA Hospital Pekanbaru', 'Penyakit Dalam: Hemato-onkologi')], 'Penyakit Dalam: Ginjal & Hipertensi': [('EKA Hospital Permata Hijau', 'Penyakit Dalam : Ginjal & Hipertensi')], 'Periodonti': [('EKA Hospital Bekasi', 'Periodonti'), ('EKA Hospital Pekanbaru', 'Periodonti')], 'Prostodonti': [('EKA Hospital Bekasi', 'Prostodonti'), ('EKA Hospital Pekanbaru', 'Prostodonti'), ('EKA Hospital BSD', 'Spesialis Prostodonsia'), ('EKA Hospital Cibubur', 'Spesialis Prostodonsia'), ('EKA Hospital Grand Family', 'Dokter Spesialis Prostodonsia')], 'Psikiatri': [('EKA Hospital Bekasi', 'Psikiatri'), ('EKA Hospital Pekanbaru', 'Psikiatri'), ('EKA Hospital BSD', 'Spesialis Psikiatri'), ('EKA Hospital Cibubur', 'Spesialis Psikiatri')], 'Psikolog Klinis': [('EKA Hospital Bekasi', 'Psikolog Klinis'), ('EKA Hospital BSD', 'Psikolog Klinis'), ('EKA Hospital Cibubur', 'Psikolog Klinis'), ('EKA Hospital Pekanbaru', 'Psikolog Klinis'), ('EKA Hospital Grand Family', 'Psikolog')], 'Spesialis Saraf': [('EKA Hospital Bekasi', 'Spesialis Saraf'), ('EKA Hospital BSD', 'Spesialis Saraf'), ('EKA Hospital Cibubur', 'Spesialis Saraf'), ('EKA Hospital Permata Hijau', 'Spesialis Saraf'), ('EKA Hospital Pekanbaru', 'Spesialis Saraf'), ('EKA Hospital Permata Hijau', 'Neurologi')], 'THT-KL': [('EKA Hospital Bekasi', 'THT-KL'), ('EKA Hospital Pekanbaru', 'THT-KL'), ('EKA Hospital BSD', 'Spesialis THT-KL'), ('EKA Hospital Cibubur', 'Spesialis THT-KL'), ('EKA Hospital Permata Hijau', 'THT')], 'Urologi': [('EKA Hospital Bekasi', 'Urologi'), ('EKA Hospital Permata Hijau', 'Urologi'), ('EKA Hospital Pekanbaru', 'Urologi'), ('Pusat Unggulan', 'Urology & Couple Clinic'), ('EKA Hospital BSD', 'Spesialis Urologi'), ('EKA Hospital Cibubur', 'Spesialis Urologi')], 'Dokter Umum': [('EKA Hospital BSD', 'Dokter Umum'), ('EKA Hospital Cibubur', 'Dokter Umum')], 'Laktasi': [('EKA Hospital Family', 'Dokter Laktasi'), ('EKA Hospital Grand Family', 'Dokter Laktasi')], 'Rehabilitasi Medik': [('EKA Hospital Permata Hijau', 'Rehabilitasi Medik')], 'Anestesi': [('EKA Hospital Bekasi', 'Anastesi'), ('EKA Hospital Pekanbaru', 'Anastesi'), ('EKA Hospital BSD', 'Spesialis Anestesi'), ('EKA Hospital Cibubur', 'Spesialis Anestesi')], 'Radiologi': [('EKA Hospital BSD', 'Spesialis Radiologi'), ('EKA Hospital Cibubur', 'Spesialis Radiologi'), ('EKA Hospital Family', 'Dokter Spesialis Radiologi'), ('EKA Hospital Grand Family', 'Dokter Spesialis Radiologi')], 'Dokter Lainnya': [('EKA Hospital Family', 'Dokter Lainnya')], 'Pusat Layanan Kesehatan Anak Terpadu': [('Pusat Unggulan', 'Pusat Layanan Kesehatan Anak Terpadu')], 'Eka Tjipta Widjaja Cancer Center': [('Pusat Unggulan', 'ETWCC: Eka Tjipta Widjaja Cancer Center')], 'Gatam Institute: Pusat Ortopedi & Tulang Belakang': [('Pusat Unggulan', 'Gatam Institute: Pusat Ortopedi & Tulang Belakang')], 'Mycardia: Pusat Layanan Jantung dan Pembuluh Darah': [('Pusat Unggulan', 'MYcardia: Pusat Layanan Jantung dan Pembuluh Darah')], 'Pusat Neonatal Intensive Care (NICU)': [('Pusat Unggulan', 'Pusat Neonatal Intensive Care (NICU)')], 'Diabetes Connection Care: Pusat Diabetes Terintegrasi': [('Pusat Unggulan', 'Diabetes Connection Care: Pusat Diabetes Terintegrasi')], 'Divine: Digestive Intervention & Endoscopy Center': [('Pusat Unggulan', 'DIVINE: Digestive Intervention & Endoscopy Center')], 'Spesialis kedokteran penerbangan': [('EKA Hospital BSD', 'Spesialis Kedokteran Penerbangan')], 'Anak: hemato-onkologi': [('EKA Hospital Bekasi', 'Anak: Hemato-onkologi'), ('EKA Hospital Pekanbaru', 'Anak: Hemato-onkologi')]}
39
+ def extract_list(text, type):
40
+ pattern = rf'<{type}>(.*?)</{type}>'
41
+ matches = re.findall(pattern, text)
42
+ matches = [x.strip() for x in matches]
43
+ return matches
44
+
45
+ def check_department_eka_simple(text):
46
+ departments = extract_list(text, 'd')
47
+ good_department = {x.lower():1 for x in mergeddepartment2hospital_eka.keys()}
48
+ if departments:
49
+ good = True
50
+ for item in departments:
51
+ if item.lower() in good_department:
52
+ continue
53
+ else:
54
+ good = False
55
+ break
56
+ else:
57
+ good = False
58
+ return good, departments
59
+
60
+
61
+
62
+ @spaces.GPU(duration=90)
63
+ def generate(
64
+ message: str,
65
+ chat_history: list[tuple[str, str]],
66
+ max_new_tokens: int = 1024,
67
+ temperature: float = 0.6,
68
+ top_p: float = 0.9,
69
+ top_k: int = 50,
70
+ repetition_penalty: float = 1.2,
71
+ ) -> Iterator[str]:
72
+ # conversation = [{"role": "system", "content": system_prompt}]
73
+ conversation = []
74
+ department_text = f"{list(mergeddepartment2hospital_eka.keys())}"
75
+ message = user_text = f'''Department:
76
+ {department_text}
77
+
78
+ Question:
79
+ {message}
80
+
81
+ Please reply to the above question and add a disclaimer statement "Informasi yang kami berikan tidak sesuai untuk, dan tidak boleh dianggap sebagai, bentuk layanan diagnosis apapun. Anda tidak boleh mengandalkan informasi yang diberikan oleh layanan teknis kami untuk mendiagnosis atau mengobati penyakit." in your response.'''
82
+ for user, assistant in chat_history:
83
+ conversation.extend(
84
+ [
85
+ {"role": "user", "content": user},
86
+ {"role": "assistant", "content": assistant},
87
+ ]
88
+ )
89
+ conversation.append({"role": "user", "content": message})
90
+
91
+ input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")
92
+ if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
93
+ input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
94
+ gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
95
+ input_ids = input_ids.to(model.device)
96
+
97
+ streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
98
+ generate_kwargs = dict(
99
+ {"input_ids": input_ids},
100
+ streamer=streamer,
101
+ max_new_tokens=max_new_tokens,
102
+ do_sample=True,
103
+ top_p=top_p,
104
+ top_k=top_k,
105
+ temperature=temperature,
106
+ num_beams=1,
107
+ repetition_penalty=repetition_penalty,
108
+ eos_token_id=tokenizer.eos_token_id,
109
+ )
110
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
111
+ t.start()
112
+
113
+ outputs = []
114
+ department_all = ''
115
+ d_flag = False
116
+ for text in streamer:
117
+ if (not d_flag) and "<d>" in text and "</d>" in text:
118
+ department_all += department
119
+ elif (not d_flag) and "<d>" in text:
120
+ d_flag = True
121
+ department = text
122
+ elif d_flag and (not "</d>" in text):
123
+ department += text
124
+ elif d_flag and "</d>" in text:
125
+ department += text
126
+ department_all += department
127
+ d_flag = False
128
+ outputs.append(text.replace("<d>", "").replace("</d>", ""))
129
+
130
+
131
+
132
+ yield "".join(outputs)
133
+ good, departments = check_department_eka_simple(department_all)
134
+ if good:
135
+ recommendation_text = '\nDepartemen yang direkomendasikan di atas dapat ditemukan di beberapa cabang EKA Hospital:\n'
136
+ for i_d, item in enumerate(departments):
137
+ hospital = mergeddepartment2hospital_eka[item]
138
+ hospital = [f" * {x[1]}, {x[0]}" for x in hospital]
139
+ item_text = f"{i_d+1}. {item}:\n"
140
+ hospital_text = "\n".join(hospital)
141
+ recommendation_text += item_text + hospital_text + "\n"
142
+ yield "".join(outputs) + recommendation_text
143
+
144
+
145
+ chat_interface = gr.ChatInterface(
146
+ fn=generate,
147
+ additional_inputs=[],
148
+ stop_btn=None,
149
+ examples=[
150
+ ["i get a cold. Help me doctor."],
151
+ ],
152
+ cache_examples=False,
153
+ )
154
+
155
+ with gr.Blocks(css="style.css", fill_height=True) as demo:
156
+ gr.Markdown(DESCRIPTION)
157
+ gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
158
+ chat_interface.render()
159
+
160
+ if __name__ == "__main__":
161
+ demo.queue(max_size=20).launch()