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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import os
|
| 4 |
+
load_dotenv()
|
| 5 |
+
import openai
|
| 6 |
+
|
| 7 |
+
from gptcall import generate
|
| 8 |
+
# Set your OpenAI API key
|
| 9 |
+
api_key = os.environ.get('OPEN_AI_KEY') # Replace with your actual API key
|
| 10 |
+
openai.api_key = api_key
|
| 11 |
+
|
| 12 |
+
hf = os.environ.get("HF_TOKEN")
|
| 13 |
+
|
| 14 |
+
server_error_msg = "**NETWORK ERROR DUE TO HIGH TRAFFIC. PLEASE REGENERATE OR REFRESH THIS PAGE.**"
|
| 15 |
+
|
| 16 |
+
def clear_history(request: gr.Request):
|
| 17 |
+
state = None
|
| 18 |
+
return ([], state, "")
|
| 19 |
+
|
| 20 |
+
def post_process_code(code):
|
| 21 |
+
sep = "\n```"
|
| 22 |
+
if sep in code:
|
| 23 |
+
blocks = code.split(sep)
|
| 24 |
+
if len(blocks) % 2 == 1:
|
| 25 |
+
for i in range(1, len(blocks), 2):
|
| 26 |
+
blocks[i] = blocks[i].replace("\\_", "_")
|
| 27 |
+
code = sep.join(blocks)
|
| 28 |
+
return code
|
| 29 |
+
|
| 30 |
+
def post_process_answer(answer):
|
| 31 |
+
answer += f"<br><br>"
|
| 32 |
+
answer = answer.replace("\n", "<br>")
|
| 33 |
+
return answer
|
| 34 |
+
|
| 35 |
+
def predict(
|
| 36 |
+
question: str,
|
| 37 |
+
system_content: str,
|
| 38 |
+
use_api: bool,
|
| 39 |
+
chatbot: list = [],
|
| 40 |
+
history: list = [],
|
| 41 |
+
):
|
| 42 |
+
try:
|
| 43 |
+
if use_api: # Check if API call is requested
|
| 44 |
+
history.append(question)
|
| 45 |
+
answer = generate(question)
|
| 46 |
+
history.append(answer)
|
| 47 |
+
else:
|
| 48 |
+
pass
|
| 49 |
+
|
| 50 |
+
# Ensure history has an even number of elements
|
| 51 |
+
if len(history) % 2 != 0:
|
| 52 |
+
history.append("")
|
| 53 |
+
|
| 54 |
+
chatbot = [(history[i], history[i + 1]) for i in range(0, len(history), 2)]
|
| 55 |
+
return chatbot, history
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
history.append("")
|
| 59 |
+
answer = server_error_msg + f" (error_code: 503)"
|
| 60 |
+
history.append(answer)
|
| 61 |
+
|
| 62 |
+
# Ensure history has an even number of elements
|
| 63 |
+
if len(history) % 2 != 0:
|
| 64 |
+
history.append("")
|
| 65 |
+
|
| 66 |
+
chatbot = [(history[i], history[i + 1]) for i in range(0, len(history), 2)]
|
| 67 |
+
return chatbot, history
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def reset_textbox(): return gr.update(value="")
|
| 72 |
+
|
| 73 |
+
def main():
|
| 74 |
+
title = """
|
| 75 |
+
<h1 align="center">Chat with TxGpt 🤖</h1>"""
|
| 76 |
+
|
| 77 |
+
css = """
|
| 78 |
+
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&display=swap');
|
| 79 |
+
|
| 80 |
+
/* Hide the footer */
|
| 81 |
+
footer .svelte-1lyswbr {
|
| 82 |
+
display: none !important;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
/* Center the column container */
|
| 86 |
+
#prompt_container {
|
| 87 |
+
margin-left: auto;
|
| 88 |
+
margin-right: auto;
|
| 89 |
+
background: linear-gradient(to right, #48c6ef, #6f86d6); /* Gradient background */
|
| 90 |
+
padding: 20px; /* Decreased padding */
|
| 91 |
+
border-radius: 10px;
|
| 92 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 93 |
+
color: black;
|
| 94 |
+
font-family: 'Poppins', sans-serif; /* Poppins font */
|
| 95 |
+
font-weight: 600; /* Bold font */
|
| 96 |
+
resize: none;
|
| 97 |
+
font-size: 18px;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
/* Chatbot container styling */
|
| 101 |
+
#chatbot_container {
|
| 102 |
+
margin: 0 auto; /* Remove left and right margins */
|
| 103 |
+
max-width: 80%; /* Adjust the maximum width as needed */
|
| 104 |
+
background: linear-gradient(to right, #ff7e5f, #feb47b); /* Gradient background */
|
| 105 |
+
padding: 20px;
|
| 106 |
+
border-radius: 10px;
|
| 107 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
/* Chatbot message area styling */
|
| 111 |
+
#chatbot .wrap.svelte-13f7djk {
|
| 112 |
+
height: 60vh; /* Adjusted height */
|
| 113 |
+
max-height: 60vh; /* Adjusted height */
|
| 114 |
+
border: 2px solid #007bff;
|
| 115 |
+
border-radius: 10px;
|
| 116 |
+
overflow-y: auto;
|
| 117 |
+
padding: 20px;
|
| 118 |
+
background-color: #e9f5ff;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
/* User message styling */
|
| 122 |
+
#chatbot .message.user.svelte-13f7djk.svelte-13f7djk {
|
| 123 |
+
width: fit-content;
|
| 124 |
+
background: #007bff;
|
| 125 |
+
color: white;
|
| 126 |
+
border-bottom-right-radius: 0;
|
| 127 |
+
border-top-left-radius: 10px;
|
| 128 |
+
border-top-right-radius: 10px;
|
| 129 |
+
border-bottom-left-radius: 10px;
|
| 130 |
+
margin-bottom: 10px;
|
| 131 |
+
padding: 10px 15px;
|
| 132 |
+
font-size: 14px;
|
| 133 |
+
font-family: 'Poppins', sans-serif; /* Poppins font */
|
| 134 |
+
font-weight: 700; /* Bold font */
|
| 135 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
/* Bot message styling */
|
| 139 |
+
#chatbot .message.bot.svelte-13f7djk.svelte-13f7djk {
|
| 140 |
+
width: fit-content;
|
| 141 |
+
background: #e1e1e1;
|
| 142 |
+
color: black;
|
| 143 |
+
border-bottom-left-radius: 0;
|
| 144 |
+
border-top-right-radius: 10px;
|
| 145 |
+
border-top-left-radius: 10px;
|
| 146 |
+
border-bottom-right-radius: 10px;
|
| 147 |
+
margin-bottom: 10px;
|
| 148 |
+
padding: 10px 15px;
|
| 149 |
+
font-size: 14px;
|
| 150 |
+
font-family: 'Poppins', sans-serif; /* Poppins font */
|
| 151 |
+
font-weight: 700; /* Bold font */
|
| 152 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
/* Preformatted text styling */
|
| 156 |
+
#chatbot .pre {
|
| 157 |
+
border: 2px solid #f1f1f1;
|
| 158 |
+
padding: 10px;
|
| 159 |
+
border-radius: 5px;
|
| 160 |
+
background-color: #ffffff;
|
| 161 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
|
| 162 |
+
font-family: 'Poppins', sans-serif; /* Poppins font */
|
| 163 |
+
font-size: 14px;
|
| 164 |
+
font-weight: 400; /* Regular font */
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
/* General preformatted text styling */
|
| 168 |
+
pre {
|
| 169 |
+
white-space: pre-wrap; /* Since CSS 2.1 */
|
| 170 |
+
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
|
| 171 |
+
white-space: -pre-wrap; /* Opera 4-6 */
|
| 172 |
+
white-space: -o-pre-wrap; /* Opera 7 */
|
| 173 |
+
word-wrap: break-word; /* Internet Explorer 5.5+ */
|
| 174 |
+
font-family: 'Poppins', sans-serif; /* Poppins font */
|
| 175 |
+
font-size: 14px;
|
| 176 |
+
font-weight: 400; /* Regular font */
|
| 177 |
+
line-height: 1.5;
|
| 178 |
+
color: #333;
|
| 179 |
+
background-color: #f8f9fa;
|
| 180 |
+
padding: 10px;
|
| 181 |
+
border-radius: 5px;
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
/* Styling for accordion sections */
|
| 185 |
+
.accordion.svelte-1lyswbr {
|
| 186 |
+
background-color: #e9f5ff; /* Light blue background for accordions */
|
| 187 |
+
border: 1px solid #007bff;
|
| 188 |
+
border-radius: 10px;
|
| 189 |
+
padding: 10px;
|
| 190 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 191 |
+
resize: both;
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
/* Prompt styling */
|
| 195 |
+
#prompt_title {
|
| 196 |
+
font-size: 24px;
|
| 197 |
+
margin-bottom: 10px;
|
| 198 |
+
resize= none;
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
/* Styling for Copy button */
|
| 202 |
+
.copy_button {
|
| 203 |
+
display: inline-block;
|
| 204 |
+
padding: 5px 10px;
|
| 205 |
+
margin: 5px 0;
|
| 206 |
+
font-size: 14px;
|
| 207 |
+
cursor: pointer;
|
| 208 |
+
color: #007bff;
|
| 209 |
+
border: 1px solid #007bff;
|
| 210 |
+
border-radius: 5px;
|
| 211 |
+
background-color: #ffffff;
|
| 212 |
+
transition: background-color 0.3s;
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
.copy_button:hover {
|
| 216 |
+
background-color: #007bff;
|
| 217 |
+
color: #ffffff;
|
| 218 |
+
}
|
| 219 |
+
"""
|
| 220 |
+
with gr.Blocks(css=css) as demo:
|
| 221 |
+
gr.HTML(title)
|
| 222 |
+
with gr.Row():
|
| 223 |
+
with gr.Column(elem_id="prompt_container", scale=0.3): # Separate column for prompt
|
| 224 |
+
with gr.Accordion("Description", open=True):
|
| 225 |
+
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)
|
| 226 |
+
|
| 227 |
+
with gr.Column(elem_id="chatbot_container", scale=0.7): # Right column for chatbot interface
|
| 228 |
+
chatbot = gr.Chatbot(elem_id="chatbot", label="TxGpt")
|
| 229 |
+
question = gr.Textbox(placeholder="Ask something", show_label=False, value="")
|
| 230 |
+
state = gr.State([])
|
| 231 |
+
use_api_toggle = gr.Checkbox(label="Enable ChatGpt", default=False, key="use_api")
|
| 232 |
+
with gr.Row():
|
| 233 |
+
with gr.Column():
|
| 234 |
+
submit_btn = gr.Button(value="🚀 Send")
|
| 235 |
+
with gr.Column():
|
| 236 |
+
clear_btn = gr.Button(value="🗑️ Clear history")
|
| 237 |
+
|
| 238 |
+
question.submit(
|
| 239 |
+
predict,
|
| 240 |
+
[question, system_content, use_api_toggle, chatbot, state],
|
| 241 |
+
[chatbot, state],
|
| 242 |
+
)
|
| 243 |
+
submit_btn.click(
|
| 244 |
+
predict,
|
| 245 |
+
[question, system_content, chatbot, state],
|
| 246 |
+
[chatbot, state],
|
| 247 |
+
)
|
| 248 |
+
submit_btn.click(reset_textbox, [], [question])
|
| 249 |
+
clear_btn.click(clear_history, None, [chatbot, state, question])
|
| 250 |
+
question.submit(reset_textbox, [], [question])
|
| 251 |
+
demo.queue(concurrency_count=10, status_update_rate="auto")
|
| 252 |
+
#demo.launch(server_name=args.server_name, server_port=args.server_port, share=args.share, debug=args.debug)
|
| 253 |
+
demo.launch(share=True, server_name='0.0.0.0')
|
| 254 |
+
|
| 255 |
+
if __name__ == '__main__':
|
| 256 |
+
""" import argparse
|
| 257 |
+
parser = argparse.ArgumentParser()
|
| 258 |
+
parser.add_argument("--server-name", default="0.0.0.0")
|
| 259 |
+
parser.add_argument("--server-port", default=8071)
|
| 260 |
+
parser.add_argument("--share", action="store_true")
|
| 261 |
+
parser.add_argument("--debug", action="store_true")
|
| 262 |
+
parser.add_argument("--verbose", action="store_true")
|
| 263 |
+
args = parser.parse_args() """
|
| 264 |
+
|
| 265 |
+
main()
|