File size: 10,769 Bytes
94e17b8
c28e3f7
 
0308e6e
c41e5ec
94e17b8
 
 
 
 
 
 
 
 
 
c41e5ec
96f677c
e5bacb6
96f677c
e5bacb6
 
a6156ff
96f677c
 
 
c41e5ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6156ff
c41e5ec
 
 
 
 
 
 
 
96f677c
c41e5ec
3eb54fe
a6156ff
 
 
94e17b8
 
 
3eb54fe
96f677c
94e17b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6156ff
 
 
 
 
94e17b8
a6156ff
 
 
 
94e17b8
a6156ff
 
 
 
94e17b8
a6156ff
c41e5ec
94e17b8
 
 
 
 
 
 
 
 
 
 
 
 
31e6eb8
3eb54fe
c28e3f7
 
 
 
 
c41e5ec
94e17b8
 
c28e3f7
c41e5ec
 
 
c28e3f7
3eb54fe
c41e5ec
d5dcf9b
a6156ff
94e17b8
e5bacb6
 
 
 
 
 
 
 
 
 
 
 
 
 
94e17b8
e5bacb6
94e17b8
e5bacb6
94e17b8
e5bacb6
 
 
 
 
 
94e17b8
c41e5ec
 
a6156ff
c41e5ec
94e17b8
96f677c
3eb54fe
96f677c
0308e6e
31e6eb8
94e17b8
 
 
 
 
 
31e6eb8
 
 
 
 
 
 
d5dcf9b
96f677c
 
 
 
 
 
 
 
 
c41e5ec
94e17b8
c41e5ec
 
 
96f677c
d5dcf9b
c41e5ec
d5dcf9b
 
 
94e17b8
d5dcf9b
94e17b8
d5dcf9b
96f677c
 
 
d5dcf9b
96f677c
d5dcf9b
 
 
 
96f677c
 
 
c41e5ec
3eb54fe
d5dcf9b
 
c41e5ec
 
96f677c
94e17b8
 
 
96f677c
94e17b8
96f677c
d5dcf9b
94e17b8
d5dcf9b
96f677c
 
 
 
d5dcf9b
96f677c
d5dcf9b
 
 
96f677c
d5dcf9b
 
c41e5ec
96f677c
d5dcf9b
 
e5bacb6
d5dcf9b
96f677c
 
 
3eb54fe
a6156ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94e17b8
 
 
 
 
 
 
 
 
 
a6156ff
 
 
94e17b8
a6156ff
 
c41e5ec
 
a6156ff
 
c41e5ec
94e17b8
c41e5ec
31e6eb8
a6156ff
94e17b8
 
96f677c
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import csv
import gradio as gr
from huggingface_hub import InferenceClient
import os

from matplotlib import colors
from rag import run_rag
from gradio.themes.utils import (
    colors,
    fonts,
    get_matching_version,
    get_theme_assets,
    sizes,
)
 
#  ================================================================================================================================
TOKEN = os.getenv("HF_TOKEN")
# client = InferenceClient("HuggingFaceH4/zephyr-7b-beta" , token=TOKEN)
system_message ="You are a capable and freindly assistant."
Endpoint_URL = "https://gx986bv0z1k42aqe.us-east-1.aws.endpoints.huggingface.cloud/"
client = InferenceClient(Endpoint_URL, token=TOKEN)

no_change_btn = gr.Button()
enable_btn = gr.Button(interactive=True)
disable_btn = gr.Button(interactive=False)
# ================================================================================================================================
class Int_State:
    def __init__(self):
        # initialise history of type list[tuple[str, str]]
        self.history = []
        self.current_query = ""
        self.current_response = ""
        self.roles = ["user", "system"]
        print("State has been initialise")
    
    def save_question(self, question):
        self.current_query = question
        self.current_response = ""
        self.history.append({"role": "user", "content": question})
        print("Question added ")
 
    def save_response(self,  assistant_message):
        # current_question = self.current_query
        self.current_response = assistant_message
        self.history.append({"role": "system", "content": assistant_message})
        print("Response saved  ")
 
 
    def get_history(self):
        return self.history
  
#  ================================================================================================================================
state = Int_State()
#  ================================================================================================================================
def clear_chat(chatbot ):
    state.history = []
 
    chatbot.clear() 
 
    yield ("" , chatbot) + (enable_btn,) * 5

#  ================================================================================================================================
def save_chat( question, answer, upvote, downvote, flag):
    file_path = "chat_data.csv"
    with open(file_path, 'r', newline='') as file:
        reader = csv.reader(file)
        data = list(reader)

    # Add new row with provided data
    new_row = [question, answer, upvote, downvote, flag]
    data.append(new_row)

    # Write updated data back to CSV file
    with open(file_path, 'w', newline='') as file:
        writer = csv.writer(file)
        writer.writerows(data)

    print("New row added successfully to", file_path)



def upvote_last_response():
    print("Upvoted")
    save_chat(state.current_query, state.current_response, 1, 0, 0)
    return (disable_btn,) * 3 + (enable_btn,)*2

def downvote_last_response():
    print("Downvoted")
    save_chat(state.current_query, state.current_response, 0, 1, 0)
    return (disable_btn,) * 3 + (enable_btn,)*2

def flag_last_response():
    print("Flagged")
    save_chat(state.current_query, state.current_response, 0, 0, 1)
    return (disable_btn,) * 3 + (enable_btn,)*2
 
def remove_last_response(chatbot): 
    print("Regenerated")
    textbox =state.current_query
    state.history.pop()
    state.history.pop()
    chatbot.clear()
    
    return (textbox ,chatbot ) + (enable_btn,) * 5
def quit_chat():
    return demo.close()

#  ================================================================================================================================ 

def chat(
    chatbot,
    message,
    max_tokens,
    temperature,
    top_p,
):
    question= message
    chatbot.append((question,""))
    yield ("" , chatbot) + (disable_btn,) * 5
    messages = [{"role": "system", "content": system_message}]
    history= state.get_history()
    state.save_question(message)

    for val in history:
        messages.append(val)

    messages.append({"role": "user", "content": run_rag(message)})
    response = "This is a response to the question"
    chatbot.append((question,""))
    # for msg in client.chat_completion(
    #     messages,
    #     max_tokens=max_tokens,
    #     stream=True,
    #     temperature=temperature,
    #     top_p=top_p,
    # ):
       
    #     token = msg.choices[0].delta.content
    #     response += str(token)
    #     # chatbot.append(( response, response))
    #     # yield "" , chatbot 
    for msg in client.text_generation(
        prompt=run_rag(message),
        temperature=temperature,
        max_new_tokens=max_tokens,
        top_p=top_p,
        stream=False,
    ):
 
        # token = msg.choices[0].delta.content
        response += str(msg)
        chatbot.append(( response, response))

        
    chatbot.clear()
    chatbot.append((question , response))
    state.save_response(response)
    yield ("" , chatbot) + (enable_btn,) * 5

 
  

#  ================================================================================================================================

theme = gr.themes.Base(
    primary_hue=colors.emerald,
    secondary_hue=colors.cyan,
    neutral_hue=colors.stone, 
    radius_size=sizes.radius_lg,
    spacing_size=sizes.spacing_sm,
    font=[gr.themes.GoogleFont('Poppins'), gr.themes.GoogleFont('Reddit Sans'), 'system-ui', 'sans-serif'],
)
EXAMPLES = [
    [ "Tell me about the latest news in the world ?"],
    [ "Tell me about the increase in the price of Bitcoin ?"],
    [ "Tell me about the actual situation in Ukraine ?"],
    [ "Tell me about current situation in palestine ?"],
]
 
#  ================================================================================================================================
 

block_css = """
#buttons button {
    min-width: min(120px,100%);
}
"""
#  ================================================================================================================================
textbox = gr.Textbox(show_label=False,
                        placeholder="Enter a question or message...",
                        container=False,
                        show_copy_button=True
                        )

with gr.Blocks(title="RAG", theme=theme, css=block_css , fill_height=True) as demo:
    
    gr.Markdown("# **Retrieval Augmented Generation (RAG) Chatbot**" )
    gr.Markdown("This is a demo of a chatbot that uses the RAG system to generate responses to user queries. RAG is a combination of a retriever and a generator, which allows it to generate responses based on the context of the conversation. The chatbot can be used to answer questions, provide information, and engage in conversation with users.")
    with gr.Row(variant="panel"):
 
            
   
        with gr.Column(scale=10):
            chatbot = gr.Chatbot(
                elem_id="chatbot",
                label="Retrieval Augmented Generation (RAG) Chatbot",
                height=300,
                layout="bubble",
                min_width=1200,
                show_copy_button=True,
                show_share_button=True,
                placeholder="Ask a question or type a message...",
            )
            with gr.Row():
                with gr.Column(scale=8):
                    textbox.render()

            with gr.Column(scale=1, min_width=100):
                submit_btn = gr.Button(value="Submit", variant="primary", interactive=True)
    
        
            with gr.Row(elem_id="buttons") as button_row:
                upvote_btn = gr.Button(value="๐Ÿ‘  Upvote", interactive=False , variant="secondary")
                downvote_btn = gr.Button(value="๐Ÿ‘Ž  Downvote", interactive=False , variant="secondary")
                flag_btn = gr.Button(value="โš ๏ธ  Flag", interactive=False , variant="secondary")
                #stop_btn = gr.Button(value="โน๏ธ  Stop Generation", interactive=False)
                regenerate_btn = gr.Button(value="๐Ÿ”„  Regenerate", interactive=False ,variant="secondary")
            with gr.Column(scale=3):
                clear_btn = gr.Button(value="๐Ÿ—‘๏ธ  Clear", interactive=False , variant="stop")
    
            with gr.Accordion("Examples", open=True) as Examples_row:
                gr.Examples(examples=[
                [f"Tell me about the latest news in the world ?"],
                [f"Tell me about the increase in the price of Bitcoin ?"],
                [f"Tell me about the actual situation in Ukraine ?"],
                [f"How true is the news about the increase in the price of oil ?"],
                [f"Tell me about current situation in palestinian ?"],
                [f"Tell me about the current situation in Afghanistan ?"],
                [f"what are the agenda of the United Nations ?"],
                ["how trump's compain going ?"],
            ],inputs=[textbox], label="Examples")


                
            with gr.Accordion("Parameters", open=False) as parameter_row:
                    temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, interactive=True, label="Temperature",)
                    top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, interactive=True, label="Top P",)
                    max_output_tokens = gr.Slider(minimum=0, maximum=4096, value=480, step=64, interactive=True, label="Max output tokens",)
        

#  ================================================================================================================================
    btn_list = [upvote_btn, downvote_btn, flag_btn, regenerate_btn, clear_btn]

    upvote_btn.click(
        upvote_last_response,
        [],
        btn_list,
    )

    downvote_btn.click(
        downvote_last_response,
        [],
        btn_list
    )
    flag_btn.click(
        flag_last_response,
        [],
        btn_list,
    )
    regenerate_btn.click(
        remove_last_response,
        [chatbot],
        [textbox , chatbot] + btn_list,
    ).then(
        chat,
        [ chatbot, textbox, max_output_tokens, temperature, top_p],
        [textbox, chatbot] + btn_list
    )

    clear_btn.click(
        clear_chat,
        [chatbot],
        [textbox , chatbot] + btn_list,
    )

    submit_btn.click(    
        chat ,
        [ chatbot, textbox , max_output_tokens, temperature, top_p],
        [textbox ,chatbot] + btn_list , 
                )
 
 #  ================================================================================================================================
demo.launch()



#  ================================================================================================================================