Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,27 @@
|
|
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
|
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 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
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
|
@@ -36,23 +37,20 @@ def initialize_models():
|
|
36 |
return vector_store
|
37 |
|
38 |
def create_llm():
|
39 |
-
"""Initialize the language model with
|
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 |
-
|
51 |
-
quantization_config=bnb_config
|
52 |
)
|
53 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
54 |
|
55 |
-
terminators = [tokenizer.eos_token_id
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
text_generation_pipeline = pipeline(
|
58 |
model=model,
|
@@ -64,7 +62,6 @@ def create_llm():
|
|
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)
|
@@ -74,7 +71,7 @@ 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
|
78 |
{chat_history}
|
79 |
Question: {question}
|
80 |
Instruksi untuk menjawab:
|
@@ -89,91 +86,98 @@ Answer:
|
|
89 |
|
90 |
class HealthAssistant:
|
91 |
def __init__(self):
|
92 |
-
|
93 |
self.memory = ConversationBufferMemory(
|
94 |
memory_key="chat_history",
|
95 |
return_messages=True,
|
96 |
output_key='answer'
|
97 |
)
|
98 |
-
|
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 |
-
|
108 |
llm=create_llm(),
|
109 |
-
retriever=
|
110 |
memory=self.memory,
|
|
|
111 |
return_source_documents=True,
|
112 |
)
|
113 |
|
114 |
def respond(self, message, history):
|
115 |
-
"""
|
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
|
124 |
self.memory.clear()
|
125 |
-
return
|
126 |
|
127 |
def create_demo():
|
128 |
-
# Initialize the assistant
|
129 |
assistant = HealthAssistant()
|
130 |
|
131 |
-
#
|
132 |
-
with gr.Blocks(
|
133 |
-
gr.Markdown(
|
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 |
-
|
142 |
-
height=
|
|
|
143 |
)
|
144 |
|
145 |
with gr.Row():
|
146 |
msg = gr.Textbox(
|
147 |
-
|
148 |
-
|
149 |
scale=9
|
150 |
)
|
151 |
submit = gr.Button("Kirim", scale=1)
|
152 |
-
|
|
|
153 |
|
154 |
# Set up event handlers
|
155 |
submit_click = submit.click(
|
156 |
assistant.respond,
|
157 |
inputs=[msg, chatbot],
|
158 |
outputs=[chatbot],
|
159 |
-
|
160 |
)
|
161 |
-
submit_click.then(lambda: "", None, msg)
|
162 |
|
163 |
msg.submit(
|
164 |
assistant.respond,
|
165 |
inputs=[msg, chatbot],
|
166 |
outputs=[chatbot],
|
167 |
-
|
168 |
-
).then(lambda: "", None, msg)
|
169 |
|
170 |
clear.click(
|
171 |
assistant.clear_history,
|
172 |
outputs=[chatbot],
|
173 |
-
|
174 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
return demo
|
177 |
|
178 |
-
|
179 |
-
demo = create_demo()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from langchain_huggingface import HuggingFaceEmbeddings
|
3 |
from langchain_community.vectorstores import Chroma
|
|
|
4 |
from langchain.prompts import PromptTemplate
|
5 |
+
from langchain.chains import ConversationalRetrievalChain
|
6 |
from langchain.memory import ConversationBufferMemory
|
7 |
import warnings
|
8 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
9 |
import os
|
10 |
from dotenv import load_dotenv
|
11 |
+
from langchain_huggingface import HuggingFacePipeline
|
12 |
|
13 |
warnings.filterwarnings("ignore")
|
14 |
load_dotenv()
|
15 |
|
16 |
# Constants and configurations
|
17 |
+
TITLE = "π Asisten Kesehatan Feminacare"
|
18 |
+
DESCRIPTION = """
|
19 |
+
# π Asisten Kesehatan Feminacare
|
20 |
+
Asisten digital ini dirancang untuk membantu Anda berkonsultasi tentang kesehatan wanita.
|
21 |
+
|
22 |
+
*Catatan: Informasi yang diberikan bersifat umum. Selalu konsultasikan dengan tenaga kesehatan untuk saran yang lebih spesifik.*
|
23 |
+
"""
|
24 |
|
|
|
25 |
MODEL_NAME = "SeaLLMs/SeaLLMs-v3-7B-Chat"
|
26 |
EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
27 |
TOP_K_DOCS = 5
|
|
|
37 |
return vector_store
|
38 |
|
39 |
def create_llm():
|
40 |
+
"""Initialize the language model with auto device mapping"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
model = AutoModelForCausalLM.from_pretrained(
|
42 |
MODEL_NAME,
|
43 |
device_map="auto",
|
44 |
+
trust_remote_code=True
|
|
|
45 |
)
|
46 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
47 |
|
48 |
+
terminators = [tokenizer.eos_token_id]
|
49 |
+
if hasattr(tokenizer, 'convert_tokens_to_ids'):
|
50 |
+
try:
|
51 |
+
terminators.append(tokenizer.convert_tokens_to_ids("<|eot_id|>"))
|
52 |
+
except:
|
53 |
+
pass
|
54 |
|
55 |
text_generation_pipeline = pipeline(
|
56 |
model=model,
|
|
|
62 |
return_full_text=False,
|
63 |
max_new_tokens=200,
|
64 |
eos_token_id=terminators,
|
|
|
65 |
)
|
66 |
|
67 |
return HuggingFacePipeline(pipeline=text_generation_pipeline)
|
|
|
71 |
Berikan informasi yang akurat, jelas, dan bermanfaat berdasarkan konteks yang tersedia.
|
72 |
Context yang tersedia:
|
73 |
{context}
|
74 |
+
Chat historyt:
|
75 |
{chat_history}
|
76 |
Question: {question}
|
77 |
Instruksi untuk menjawab:
|
|
|
86 |
|
87 |
class HealthAssistant:
|
88 |
def __init__(self):
|
89 |
+
vector_store = initialize_models()
|
90 |
self.memory = ConversationBufferMemory(
|
91 |
memory_key="chat_history",
|
92 |
return_messages=True,
|
93 |
output_key='answer'
|
94 |
)
|
95 |
+
|
|
|
|
|
|
|
96 |
custom_prompt = PromptTemplate(
|
97 |
template=PROMPT_TEMPLATE,
|
98 |
input_variables=["context", "question", "chat_history"]
|
99 |
)
|
100 |
|
101 |
+
self.qa_chain = ConversationalRetrievalChain.from_llm(
|
102 |
llm=create_llm(),
|
103 |
+
retriever=vector_store.as_retriever(),
|
104 |
memory=self.memory,
|
105 |
+
combine_docs_chain_kwargs={"prompt": custom_prompt},
|
106 |
return_source_documents=True,
|
107 |
)
|
108 |
|
109 |
def respond(self, message, history):
|
110 |
+
"""Process the message and return a response"""
|
|
|
|
|
|
|
111 |
response = self.qa_chain({"question": message})
|
112 |
return response["answer"]
|
113 |
+
|
114 |
def clear_history(self):
|
115 |
+
"""Clear the conversation memory"""
|
116 |
self.memory.clear()
|
117 |
+
return None
|
118 |
|
119 |
def create_demo():
|
|
|
120 |
assistant = HealthAssistant()
|
121 |
|
122 |
+
# Define the interface
|
123 |
+
with gr.Blocks(title=TITLE) as demo:
|
124 |
+
gr.Markdown(DESCRIPTION)
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
chatbot = gr.Chatbot(
|
127 |
+
label="Chat History",
|
128 |
+
height=600,
|
129 |
+
show_copy_button=True,
|
130 |
)
|
131 |
|
132 |
with gr.Row():
|
133 |
msg = gr.Textbox(
|
134 |
+
label="Ketik pertanyaan Anda di sini...",
|
135 |
+
placeholder="Contoh: Apa itu PCOS?",
|
136 |
scale=9
|
137 |
)
|
138 |
submit = gr.Button("Kirim", scale=1)
|
139 |
+
|
140 |
+
clear = gr.Button("ποΈ Hapus Riwayat Chat")
|
141 |
|
142 |
# Set up event handlers
|
143 |
submit_click = submit.click(
|
144 |
assistant.respond,
|
145 |
inputs=[msg, chatbot],
|
146 |
outputs=[chatbot],
|
147 |
+
show_progress="full"
|
148 |
)
|
149 |
+
submit_click.then(lambda: "", None, msg) # Clear input after sending
|
150 |
|
151 |
msg.submit(
|
152 |
assistant.respond,
|
153 |
inputs=[msg, chatbot],
|
154 |
outputs=[chatbot],
|
155 |
+
show_progress="full"
|
156 |
+
).then(lambda: "", None, msg) # Clear input after sending
|
157 |
|
158 |
clear.click(
|
159 |
assistant.clear_history,
|
160 |
outputs=[chatbot],
|
161 |
+
show_progress=True
|
162 |
)
|
163 |
+
|
164 |
+
# Add some CSS styling
|
165 |
+
gr.Markdown("""
|
166 |
+
<style>
|
167 |
+
.gradio-container {
|
168 |
+
max-width: 1200px !important;
|
169 |
+
margin: auto;
|
170 |
+
}
|
171 |
+
</style>
|
172 |
+
""")
|
173 |
|
174 |
return demo
|
175 |
|
176 |
+
if __name__ == "__main__":
|
177 |
+
demo = create_demo()
|
178 |
+
demo.launch(
|
179 |
+
share=True,
|
180 |
+
server_name="0.0.0.0",
|
181 |
+
server_port=7860,
|
182 |
+
enable_queue=True
|
183 |
+
)
|