File size: 8,170 Bytes
062277a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from dotenv import load_dotenv
import os
load_dotenv()
import openai

from gptcall import generate
# Set your OpenAI API key
api_key = os.environ.get('OPEN_AI_KEY')  # Replace with your actual API key
openai.api_key = api_key

hf = os.environ.get("HF_TOKEN")

server_error_msg = "**NETWORK ERROR DUE TO HIGH TRAFFIC. PLEASE REGENERATE OR REFRESH THIS PAGE.**"

def clear_history(request: gr.Request):
    state = None
    return ([], state, "")

def post_process_code(code):
    sep = "\n```"
    if sep in code:
        blocks = code.split(sep)
        if len(blocks) % 2 == 1:
            for i in range(1, len(blocks), 2):
                blocks[i] = blocks[i].replace("\\_", "_")
        code = sep.join(blocks)
    return code

def post_process_answer(answer):
    answer += f"<br><br>"
    answer = answer.replace("\n", "<br>")
    return answer

def predict(
    question: str,
    system_content: str,
    use_api: bool,
    chatbot: list = [],
    history: list = [],
):
    try:
        if use_api:  # Check if API call is requested
            history.append(question)
            answer = generate(question)
            history.append(answer)
        else:
            pass

        # Ensure history has an even number of elements
        if len(history) % 2 != 0:
            history.append("")

        chatbot = [(history[i], history[i + 1]) for i in range(0, len(history), 2)]
        return chatbot, history

    except Exception as e:
        history.append("")
        answer = server_error_msg + f" (error_code: 503)"
        history.append(answer)

        # Ensure history has an even number of elements
        if len(history) % 2 != 0:
            history.append("")

        chatbot = [(history[i], history[i + 1]) for i in range(0, len(history), 2)]
        return chatbot, history



def reset_textbox():    return gr.update(value="")

def main():
    title = """
<h1 align="center">Chat with TxGpt πŸ€–</h1>"""

    css = """
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&display=swap');

/* Hide the footer */
footer .svelte-1lyswbr {
    display: none !important;
}

/* Center the column container */
#prompt_container {
    margin-left: auto;
    margin-right: auto;
    background: linear-gradient(to right, #48c6ef, #6f86d6); /* Gradient background */
    padding: 20px; /* Decreased padding */
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    color: black;
    font-family: 'Poppins', sans-serif; /* Poppins font */
    font-weight: 600; /* Bold font */
    resize: none;
    font-size: 18px;
}

/* Chatbot container styling */
#chatbot_container {
    margin: 0 auto; /* Remove left and right margins */
    max-width: 80%; /* Adjust the maximum width as needed */
    background: linear-gradient(to right, #ff7e5f, #feb47b); /* Gradient background */
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

/* Chatbot message area styling */
#chatbot .wrap.svelte-13f7djk {
    height: 60vh; /* Adjusted height */
    max-height: 60vh; /* Adjusted height */
    border: 2px solid #007bff;
    border-radius: 10px;
    overflow-y: auto;
    padding: 20px;
    background-color: #e9f5ff;
}

/* User message styling */
#chatbot .message.user.svelte-13f7djk.svelte-13f7djk {
    width: fit-content;
    background: #007bff;
    color: white;
    border-bottom-right-radius: 0;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    border-bottom-left-radius: 10px;
    margin-bottom: 10px;
    padding: 10px 15px;
    font-size: 14px;
    font-family: 'Poppins', sans-serif; /* Poppins font */
    font-weight: 700; /* Bold font */
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

/* Bot message styling */
#chatbot .message.bot.svelte-13f7djk.svelte-13f7djk {
    width: fit-content;
    background: #e1e1e1;
    color: black;
    border-bottom-left-radius: 0;
    border-top-right-radius: 10px;
    border-top-left-radius: 10px;
    border-bottom-right-radius: 10px;
    margin-bottom: 10px;
    padding: 10px 15px;
    font-size: 14px;
    font-family: 'Poppins', sans-serif; /* Poppins font */
    font-weight: 700; /* Bold font */
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

/* Preformatted text styling */
#chatbot .pre {
    border: 2px solid #f1f1f1;
    padding: 10px;
    border-radius: 5px;
    background-color: #ffffff;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
    font-family: 'Poppins', sans-serif; /* Poppins font */
    font-size: 14px;
    font-weight: 400; /* Regular font */
}

/* General preformatted text styling */
pre {
    white-space: pre-wrap;       /* Since CSS 2.1 */
    white-space: -moz-pre-wrap;  /* Mozilla, since 1999 */
    white-space: -pre-wrap;      /* Opera 4-6 */
    white-space: -o-pre-wrap;    /* Opera 7 */
    word-wrap: break-word;       /* Internet Explorer 5.5+ */
    font-family: 'Poppins', sans-serif; /* Poppins font */
    font-size: 14px;
    font-weight: 400; /* Regular font */
    line-height: 1.5;
    color: #333;
    background-color: #f8f9fa;
    padding: 10px;
    border-radius: 5px;
}

/* Styling for accordion sections */
.accordion.svelte-1lyswbr {
    background-color: #e9f5ff; /* Light blue background for accordions */
    border: 1px solid #007bff;
    border-radius: 10px;
    padding: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    resize: both;
}

/* Prompt styling */
#prompt_title {
    font-size: 24px;
    margin-bottom: 10px;
    resize= none;
}

/* Styling for Copy button */
.copy_button {
    display: inline-block;
    padding: 5px 10px;
    margin: 5px 0;
    font-size: 14px;
    cursor: pointer;
    color: #007bff;
    border: 1px solid #007bff;
    border-radius: 5px;
    background-color: #ffffff;
    transition: background-color 0.3s;
}

.copy_button:hover {
    background-color: #007bff;
    color: #ffffff;
}
"""
    with gr.Blocks(css=css) as demo:
        gr.HTML(title)
        with gr.Row():
            with gr.Column(elem_id="prompt_container", scale=0.3):  # Separate column for prompt
                with gr.Accordion("Description", open=True):
                    system_content = gr.Textbox(value="TxGpt talk to your local documents without internet. If you need information on public data, please enable the ChatGpt checkbox and start querying!",show_label=False,lines=5)

            with gr.Column(elem_id="chatbot_container", scale=0.7):  # Right column for chatbot interface
                chatbot = gr.Chatbot(elem_id="chatbot", label="TxGpt")
                question = gr.Textbox(placeholder="Ask something", show_label=False, value="")
                state = gr.State([])
                use_api_toggle = gr.Checkbox(label="Enable ChatGpt", default=False, key="use_api")
                with gr.Row():
                    with gr.Column():
                        submit_btn = gr.Button(value="πŸš€ Send")
                    with gr.Column():
                        clear_btn = gr.Button(value="πŸ—‘οΈ Clear history")

        question.submit(
            predict,
            [question, system_content, use_api_toggle, chatbot, state],
            [chatbot, state],
        )
        submit_btn.click(
            predict,
            [question, system_content, chatbot, state],
            [chatbot, state],
        )
        submit_btn.click(reset_textbox, [], [question])
        clear_btn.click(clear_history, None, [chatbot, state, question])
        question.submit(reset_textbox, [], [question])
        demo.queue(concurrency_count=10, status_update_rate="auto")
        #demo.launch(server_name=args.server_name, server_port=args.server_port, share=args.share, debug=args.debug)
        demo.launch(share=True, server_name='0.0.0.0')

if __name__ == '__main__':
    """ import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("--server-name", default="0.0.0.0")
    parser.add_argument("--server-port", default=8071)
    parser.add_argument("--share", action="store_true")
    parser.add_argument("--debug", action="store_true")
    parser.add_argument("--verbose", action="store_true")
    args = parser.parse_args() """

    main()