Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
3 |
+
from langchain_community.vectorstores import Chroma
|
4 |
+
from langchain_community.llms import HuggingFaceHub
|
5 |
+
from langchain.prompts import PromptTemplate
|
6 |
+
from langchain.chains import RetrievalQA, ConversationalRetrievalChain
|
7 |
+
from langchain.memory import ConversationBufferMemory
|
8 |
+
import warnings
|
9 |
+
from transformers import pipeline
|
10 |
+
import torch
|
11 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
12 |
+
import os
|
13 |
+
from dotenv import load_dotenv
|
14 |
+
|
15 |
+
warnings.filterwarnings("ignore")
|
16 |
+
load_dotenv()
|
17 |
+
|
18 |
+
# Constants and configurations
|
19 |
+
APP_TITLE = "π Asisten Kesehatan Feminacare"
|
20 |
+
INITIAL_MESSAGE = """Halo! π Saya adalah asisten kesehatan feminacare yang siap membantu Anda dengan informasi seputar kesehatan wanita.
|
21 |
+
Silakan ajukan pertanyaan apa saja dan saya akan membantu Anda dengan informasi yang akurat."""
|
22 |
+
|
23 |
+
# Model configurations
|
24 |
+
MODEL_NAME = "SeaLLMs/SeaLLMs-v3-7B-Chat"
|
25 |
+
EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
26 |
+
TOP_K_DOCS = 5
|
27 |
+
|
28 |
+
def initialize_models():
|
29 |
+
"""Initialize the embedding model and vector store"""
|
30 |
+
data_directory = os.path.join(os.path.dirname(__file__), "vector_db_dir")
|
31 |
+
embedding_model = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL)
|
32 |
+
vector_store = Chroma(
|
33 |
+
embedding_function=embedding_model,
|
34 |
+
persist_directory=data_directory
|
35 |
+
)
|
36 |
+
return vector_store
|
37 |
+
|
38 |
+
def create_llm():
|
39 |
+
"""Initialize the language model with optimized parameters"""
|
40 |
+
bnb_config = BitsAndBytesConfig(
|
41 |
+
load_in_4bit=True,
|
42 |
+
bnb_4bit_use_double_quant=True,
|
43 |
+
bnb_4bit_quant_type="nf4",
|
44 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
45 |
+
)
|
46 |
+
|
47 |
+
model = AutoModelForCausalLM.from_pretrained(
|
48 |
+
MODEL_NAME,
|
49 |
+
device_map="auto",
|
50 |
+
torch_dtype=torch.float16,
|
51 |
+
quantization_config=bnb_config
|
52 |
+
)
|
53 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
54 |
+
|
55 |
+
terminators = [tokenizer.eos_token_id, tokenizer.convert_tokens_to_ids("<|eot_id|>")]
|
56 |
+
|
57 |
+
text_generation_pipeline = pipeline(
|
58 |
+
model=model,
|
59 |
+
tokenizer=tokenizer,
|
60 |
+
task="text-generation",
|
61 |
+
temperature=0.2,
|
62 |
+
do_sample=True,
|
63 |
+
repetition_penalty=1.1,
|
64 |
+
return_full_text=False,
|
65 |
+
max_new_tokens=200,
|
66 |
+
eos_token_id=terminators,
|
67 |
+
device_map="auto"
|
68 |
+
)
|
69 |
+
|
70 |
+
return HuggingFacePipeline(pipeline=text_generation_pipeline)
|
71 |
+
|
72 |
+
PROMPT_TEMPLATE = """
|
73 |
+
Anda adalah asisten kesehatan profesional dengan nama Feminacare.
|
74 |
+
Berikan informasi yang akurat, jelas, dan bermanfaat berdasarkan konteks yang tersedia.
|
75 |
+
Context yang tersedia:
|
76 |
+
{context}
|
77 |
+
Chat history:
|
78 |
+
{chat_history}
|
79 |
+
Question: {question}
|
80 |
+
Instruksi untuk menjawab:
|
81 |
+
1. Berikan jawaban yang LENGKAP dan TERSTRUKTUR
|
82 |
+
2. Selalu sertakan SUMBER informasi dari konteks yang diberikan
|
83 |
+
3. Jika informasi tidak tersedia dalam konteks, katakan: "Maaf, saya tidak memiliki informasi yang cukup untuk menjawab pertanyaan tersebut secara akurat. Silakan konsultasi dengan tenaga kesehatan untuk informasi lebih lanjut."
|
84 |
+
4. Gunakan bahasa yang mudah dipahami
|
85 |
+
5. Jika relevan, berikan poin-poin penting menggunakan format yang rapi
|
86 |
+
6. Akhiri dengan anjuran untuk konsultasi dengan tenaga kesehatan jika diperlukan
|
87 |
+
Answer:
|
88 |
+
"""
|
89 |
+
|
90 |
+
class HealthAssistant:
|
91 |
+
def __init__(self):
|
92 |
+
self.vector_store = initialize_models()
|
93 |
+
self.memory = ConversationBufferMemory(
|
94 |
+
memory_key="chat_history",
|
95 |
+
return_messages=True,
|
96 |
+
output_key='answer'
|
97 |
+
)
|
98 |
+
self.qa_chain = self.setup_qa_chain()
|
99 |
+
|
100 |
+
def setup_qa_chain(self):
|
101 |
+
"""Set up the QA chain with improved configuration"""
|
102 |
+
custom_prompt = PromptTemplate(
|
103 |
+
template=PROMPT_TEMPLATE,
|
104 |
+
input_variables=["context", "question", "chat_history"]
|
105 |
+
)
|
106 |
+
|
107 |
+
return ConversationalRetrievalChain.from_llm(
|
108 |
+
llm=create_llm(),
|
109 |
+
retriever=self.vector_store.as_retriever(),
|
110 |
+
memory=self.memory,
|
111 |
+
return_source_documents=True,
|
112 |
+
)
|
113 |
+
|
114 |
+
def respond(self, message, history):
|
115 |
+
"""Generate response for user input"""
|
116 |
+
if not message:
|
117 |
+
return ""
|
118 |
+
|
119 |
+
response = self.qa_chain({"question": message})
|
120 |
+
return response["answer"]
|
121 |
+
|
122 |
+
def clear_history(self):
|
123 |
+
"""Clear conversation history"""
|
124 |
+
self.memory.clear()
|
125 |
+
return []
|
126 |
+
|
127 |
+
def create_demo():
|
128 |
+
# Initialize the assistant
|
129 |
+
assistant = HealthAssistant()
|
130 |
+
|
131 |
+
# Create the Gradio interface
|
132 |
+
with gr.Blocks(css="footer {visibility: hidden}") as demo:
|
133 |
+
gr.Markdown(f"# {APP_TITLE}")
|
134 |
+
gr.Markdown("""
|
135 |
+
Asisten digital ini dirancang untuk membantu Anda berkonsultasi tentang kesehatan wanita.
|
136 |
+
|
137 |
+
_Catatan: Informasi yang diberikan bersifat umum. Selalu konsultasikan dengan tenaga kesehatan untuk saran yang lebih spesifik._
|
138 |
+
""")
|
139 |
+
|
140 |
+
chatbot = gr.Chatbot(
|
141 |
+
value=[[None, INITIAL_MESSAGE]],
|
142 |
+
height=400
|
143 |
+
)
|
144 |
+
|
145 |
+
with gr.Row():
|
146 |
+
msg = gr.Textbox(
|
147 |
+
placeholder="Ketik pertanyaan Anda di sini...",
|
148 |
+
show_label=False,
|
149 |
+
scale=9
|
150 |
+
)
|
151 |
+
submit = gr.Button("Kirim", scale=1)
|
152 |
+
clear = gr.Button("ποΈ Hapus Riwayat", scale=1)
|
153 |
+
|
154 |
+
# Set up event handlers
|
155 |
+
submit_click = submit.click(
|
156 |
+
assistant.respond,
|
157 |
+
inputs=[msg, chatbot],
|
158 |
+
outputs=[chatbot],
|
159 |
+
queue=True
|
160 |
+
)
|
161 |
+
submit_click.then(lambda: "", None, msg)
|
162 |
+
|
163 |
+
msg.submit(
|
164 |
+
assistant.respond,
|
165 |
+
inputs=[msg, chatbot],
|
166 |
+
outputs=[chatbot],
|
167 |
+
queue=True
|
168 |
+
).then(lambda: "", None, msg)
|
169 |
+
|
170 |
+
clear.click(
|
171 |
+
assistant.clear_history,
|
172 |
+
outputs=[chatbot],
|
173 |
+
queue=False
|
174 |
+
)
|
175 |
+
|
176 |
+
return demo
|
177 |
+
|
178 |
+
# Create and launch the demo
|
179 |
+
demo = create_demo()
|