File size: 7,055 Bytes
42b81a8
 
 
 
 
116c6eb
42b81a8
79340f2
d48537f
 
 
b7a96ac
116c6eb
 
 
 
 
 
 
 
 
 
 
 
 
9dbf8fe
42b81a8
 
 
 
 
 
d48537f
116c6eb
79340f2
 
d48537f
 
 
 
 
 
 
 
 
 
 
 
 
 
9dbf8fe
d48537f
 
 
116c6eb
d48537f
79340f2
d48537f
79340f2
d48537f
79340f2
d48537f
 
 
 
 
 
 
116c6eb
 
f7afa35
b7a96ac
 
 
 
 
 
 
 
 
 
d48537f
116c6eb
d48537f
 
 
 
 
 
 
42b81a8
 
d48537f
42b81a8
 
 
 
 
 
b7a96ac
42b81a8
 
 
 
d48537f
116c6eb
 
 
 
42b81a8
 
116c6eb
 
 
 
 
 
 
 
 
 
 
 
 
42b81a8
94debe9
42b81a8
 
116c6eb
42b81a8
 
 
 
 
 
116c6eb
 
 
 
 
42b81a8
 
116c6eb
 
 
42b81a8
 
 
116c6eb
 
 
42b81a8
116c6eb
 
94debe9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eafd360
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import gradio as gr
import os
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from langchain_chroma import Chroma
import re
import dotenv
from utils import HuggingChat
from langchain_core.prompts import PromptTemplate
from langchain_community.embeddings import HuggingFaceEmbeddings
import langchain
from langchain_groq.chat_models import ChatGroq

# import json
# import shutil
# import magic
# import ollama
# from langchain_community.vectorstores.qdrant import Qdrant
# from langchain_core.documents import Document
# from langchain_core.prompts import ChatPromptTemplate
# from langchain_community.chains import 
from langchain_community.chat_models import ChatOllama
# from hugchat import hugchat
# from langchain.callbacks import SystemMessage
# from hugchat.login import Login
langchain.debug = False

dotenv.load_dotenv()


class GradioApp:
    def __init__(self):
        self.history = []
        self.links = []
        

#         template = """
# You are a helpful health assistant. These Human will ask you a questions about their pregnancy health.
# Use following piece of context to answer the question.
# If you don't know the answer, just say you don't know.
# Keep the answer within 2 sentences and concise.

# Context: {context}
# Question: {question}
# Answer: """


        self.template = """
You are a helpful AI bot that guides the customer or user through the website content and provides the user with exact details they want.
You help everyone by answering questions, and improve your answers from previous answers in History.
Don't try to make up an answer, if you don't know, ask for more detail otherwise say you can contact the support team at https://pragetx.com/contact-us/
Answer in the same language the question was asked.
Answer in a way that is easy to understand.
Try to limit the answer to 3-4 sentences.
Provide source url as well at the end of the answer.
Do not say "Based on the information you provided, ..." or "I think the answer is...". Just answer the question directly in detail.

History: {chat_history}

Context: {context}

Question: {question}
Answer: 
"""
        self.prompt = PromptTemplate(
            template=self.template,
            input_variables=["chat_history","context", "question"]
        )
        self.db = Chroma(persist_directory="./pragetx_chroma", embedding_function=HuggingFaceEmbeddings(), collection_name="pragetx")  
        # self.llm = ChatOllama(model="phi3:3.8b", base_url="http://localhost:11434", num_gpu=16)
        # self.llm = HuggingChat(email = os.getenv("HF_EMAIL") , psw = os.getenv("HF_PASS") )
        # self.llm = HuggingChat(email = "brij1808" , psw = "Brijesh321@R" )
        os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
        self.llm = ChatGroq(
                model="gemma2-9b-it",
                temperature=1,
                max_tokens=512,
                timeout=5,
                max_retries=2,
            )

        self.chain = (
            {"chat_history": self.chat_history, "context": self.db.as_retriever(search_kwargs={"k":3}), "question": RunnablePassthrough()} |
            self.prompt |
            self.llm | 
            StrOutputParser())
    def chat_history(self, history):
        print(self.history)
        print("\n".join(f"##Human: {x[0]}\n{'##Bot: '+x[1] if x[1] else ''}" for x in self.history))
        return "\n".join(f"##Human: {x[0]}\n{'##Bot: '+x[1] if x[1] else ''}" for x in self.history)

    def user(self,user_message, history):
        self.history = history + [[user_message, None]]
        return "", history + [[user_message, None]]
    
    def bot(self,history):
        print(history)
        prompt = history[-1][0] or ""
        for chunks in self.chain.stream(prompt):
            print('chunks: ', chunks)
            history[-1][1] = history[-1][1] or ""
            history[-1][1] += chunks
            yield history
        history[-1][1] = history[-1][1] or ""
        self.history = history
        # extract links in the response and store them
        links = re.findall(r'(https?://\S+)', history[-1][1])
        print(links)
        self.links = links
        print(history[-1][1])
        print(history)
        
    # def list_links(self):
    #     print( "link_buttons", self.links)
    #     link_buttons = []
    #     for link in range(5):
    #         if link < len(self.links):
    #             btn = gr.Button(f"Open {self.links[link]}", visible=True)
                
    #         else:
    #             btn = gr.Button(visible=False)
    #         link_buttons.append(btn)
    #     return link_buttons
        

'''
with gr.Blocks() as demo:
    gradio_app = GradioApp()
    # link_buttons = []
    # files = gr.Files(label="Upload Documents and Medical Reports", type="filepath", file_types=["pdf", "docx", "jpg", "jpeg", "png"])
    # upload_button = gr.UploadButton(label="Upload Documents and Medical Reports", type="filepath", file_count='multiple', file_types=["pdf", "docx", "jpg", "jpeg", "png"], )
    output_text = gr.Markdown(label="Output", value="   ")
    infer_status = gr.Label("Infer Status: ", visible=False)

    # upload_button.upload(gradio_app.upload_files, upload_button, [files, output_text])
    chatbot = gr.Chatbot()
    # with gr.Row():
    #     for link in range(5):
    #         btn = gr.Button(visible=False)
    #         link_buttons.append(btn)
    msg = gr.Textbox()
    clear = gr.Button("Clear")
    # for link in range(5):
    #     print(gradio_app.links)
    #     btn.click(lambda: None, None, None, js=f"window.location.assign('{gradio_app.links[link]}');" if link < len(gradio_app.links) else None)
    msg.submit(gradio_app.user, [msg, chatbot], [msg, chatbot], queue=False).then(
      gradio_app.bot, chatbot, chatbot
    )
    # .then(
    #     gradio_app.list_links, None, link_buttons
    # )
    clear.click(lambda: None, None, chatbot, queue=False)
demo.queue()
demo.launch(share=True, server_name="0.0.0.0", root_path="/bot")
'''


with gr.Blocks(css="""
#chatbot {margin-bottom: -20px;} 
footer {display: none !important;}
#colab-link-container {display: none !important;}
#chatbot .wrap > .label {
    display: none !important;
}
""") as demo:
    gradio_app = GradioApp()
    output_text = gr.Markdown(label="Output", value="   ")
    infer_status = gr.Label("Infer Status: ", visible=False)
    
    with gr.Column(scale=1, min_width=800):
        chatbot = gr.Chatbot(elem_id="chatbot", height=400)
        with gr.Row():
            msg = gr.Textbox(label="Enter your message", show_label=False, lines=1)
            send_button = gr.Button("Send", elem_id="send_button", scale=0)
    
    msg.submit(gradio_app.user, [msg, chatbot], [msg, chatbot], queue=False).then(
        gradio_app.bot, chatbot, chatbot
    )
    send_button.click(gradio_app.user, [msg, chatbot], [msg, chatbot], queue=False).then(
        gradio_app.bot, chatbot, chatbot
    )

demo.queue()
demo.launch(share=True, server_name="0.0.0.0", root_path="/bot")