Spaces:
Runtime error
Runtime error
File size: 12,416 Bytes
374c4cd d037cdf a4b89be d037cdf a4b89be d037cdf a4b89be 374c4cd a4b89be d037cdf 374c4cd d037cdf a4b89be 374c4cd a4b89be d037cdf 374c4cd a4b89be d037cdf a4b89be 2cf751d a4b89be d037cdf a4b89be d037cdf 2cf751d a4b89be 2cf751d d037cdf a4b89be d037cdf a4b89be 2cf751d a4b89be d037cdf a4b89be d037cdf a4b89be d037cdf a4b89be d037cdf a4b89be 2cf751d d037cdf a4b89be d037cdf a4b89be d037cdf a4b89be d037cdf a4b89be 374c4cd a4b89be 374c4cd d037cdf a4b89be |
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 |
import gradio as gr
from utils import *
from chains.openai_model import OpenAIModel
from config import SEVER, PORT, DEBUG, DEPLOYMENT_ID
from vector_db import delete_all, delete_file, handle_upload_file, load_files_blob
from theme_dropdown import create_theme_dropdown
# Get and load new model
def get_model(llm_model_name, temperature=0., top_p=1.0):
model = OpenAIModel(llm_model_name=llm_model_name,
condense_model_name=llm_model_name, temperature=temperature, top_p=top_p)
return model
def create_new_model():
return get_model(llm_model_name=DEPLOYMENT_ID)
def update_database(files_src):
message = handle_upload_file(files_src)
available_files = load_files_blob()
return gr.update(choices=available_files), message
def get_available_files():
available_files = load_files_blob()
return gr.update(choices=available_files), gr.update(visible=True)
def update_example(chatbot, set_save_file_name):
from chains.related_question import RelatedQuestion
from chains.create_topic import CreateTopic
related_question = RelatedQuestion()
outputs = chatbot[-1][1].split("<div")[0]
res = related_question.predict(inputs=chatbot[-1][0], outputs=outputs)
out = list(map(lambda x: x.split('- ')[-1], res.split('\n')))
samples = [[a] for a in out]
if len(chatbot) == 1:
topic_chain = CreateTopic()
topic = topic_chain.predict(inputs=chatbot[-1][0], outputs=outputs)
set_save_file_name = topic
return chatbot, gr.Dataset.update(samples=samples), samples, set_save_file_name
def load_example(example_id, samples):
return samples[example_id][0]
# Gradio app
with open("custom.css", "r", encoding="utf-8") as f:
customCSS = f.read()
dropdown, js = create_theme_dropdown()
head = """
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>FPT Bot</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
</body>
</html>
"""
checkbox_js = """
async () => {
// Select all checkboxes with the class 'svelte-1ojmf70'
const checkboxes = document.querySelectorAll('.svelte-1ojmf70[type="checkbox"]');
// Add a click event listener to each checkbox
checkboxes.forEach(checkbox => {
checkbox.addEventListener('click', function() {
// If this checkbox was checked, uncheck all others
if (this.checked) {
checkboxes.forEach(otherCheckbox => {
if (otherCheckbox !== this) {
otherCheckbox.checked = false;
}
});
}
});
});
}
"""
title = """<h1 align="left" style="min-width:200px; margin-top:6px; white-space: nowrap;">AI Assistant 🤖</h1>"""
logo = """
<div class="logo"></div>
"""
user_input = gr.Textbox()
with gr.Blocks(css=customCSS, theme='minatosnow/qaigpt') as demo:
samples = gr.State()
user_name = gr.State("")
history = gr.State([])
current_model = gr.State(create_new_model)
gr.HTML(head)
with gr.Row(elem_classes="status-div"):
with gr.Column():
gr.HTML(logo)
user_info = gr.Markdown(value="getting user info...", elem_id="user_info")
with gr.Column():
status_text = ""
status_display = gr.Markdown(status_text, elem_id="status_display")
with gr.Row().style(equal_height=True):
with gr.Column(scale=1):
with gr.Tab(label="Database"):
with gr.Accordion("Upload file", open=True, visible=False) as acc:
with gr.Row():
index_files = gr.Files(label="Files", type="file")
all_files = gr.Dropdown(
label=None, show_label=False, multiselect=True, choices=load_files_blob(), interactive=True
)
with gr.Row():
with gr.Column(min_width=42, scale=1):
delete_btn = gr.Button("", elem_classes="btn btn-del tooltip-btn tooltip-del")
with gr.Column(min_width=42, scale=1):
delete_all_btn = gr.Button("", elem_classes="btn btn-del-all tooltip-btn tooltip-del-all")
upload_files_btn = gr.Checkbox(label="Upload files", value=False, elem_classes="switch_checkbox")
local_db = gr.Checkbox(label="Local knowledge DB", value=False, elem_classes="switch_checkbox")
custom_websearch = gr.Checkbox(label="FPT web search", value=False, elem_classes="switch_checkbox")
local_db.change(None, _js=checkbox_js)
upload_files_btn.change(None, _js=checkbox_js)
custom_websearch.change(None, _js=checkbox_js)
with gr.Tab(label="History"):
with gr.Accordion("Save/Load conversation history"):
with gr.Column():
with gr.Row():
with gr.Column(scale=6):
history_file_dropdown = gr.Dropdown(
label="Load conversation from list",
choices=get_history_names(plain=True),
multiselect=False,
container=False,
)
with gr.Row():
with gr.Column(min_width=42, scale=1):
historyRefreshBtn = gr.Button("🔄 Refresh")
with gr.Column(min_width=42, scale=1):
historyDeleteBtn = gr.Button("🗑️ Delete")
with gr.Row():
with gr.Column(scale=6):
set_save_file_name = gr.Textbox(
show_label=True,
placeholder=None,
label="Topic (File name)",
)
with gr.Column(scale=1):
saveHistoryBtn = gr.Button("💾 Save History")
with gr.Tab(label="Theme"):
toggle_dark = gr.Button(value="Toggle Light/Dark")
toggle_dark.click(
None,
_js="""
() => {
document.body.classList.toggle('dark');
}
""",
)
with gr.Column(scale=9):
with gr.Row():
chatbot = gr.Chatbot(show_label=False, elem_classes="chatbot", show_share_button=False, height=650)
with gr.Row():
examples_questions = gr.Dataset(samples=[], components=[user_input], type="index",
elem_classes="examples")
with gr.Row(elem_classes="chatrow"):
with gr.Column(min_width=225, scale=10):
user_input = gr.Textbox(show_label=False, placeholder="Ask me anything...", container=False,
elem_classes="user-input")
with gr.Column(min_width=42, scale=1):
submitBtn = gr.Button("", elem_classes="btn btn-send tooltip-btn tooltip-content-send")
with gr.Column(min_width=42, scale=1):
record_audio = gr.Audio(source="microphone",
show_label=False,
elem_classes="audio-btn btn",
type="filepath")
with gr.Column(min_width=42, scale=1):
emptyBtn = gr.Button(
"", elem_classes="btn btn-clear tooltip-btn tooltip-content-clear")
with gr.Row(elem_classes="footer"):
gr.HTML("""<footer>🤖 QGPT – Developed by FPT.QAI</footer>""")
def create_greeting(request: gr.Request):
if hasattr(request, "username") and request.username: # is not None or is not ""
print(f"User Name: {request.username}")
user_info, user_name = gr.Markdown.update(value=f"Hi {request.username}!"), request.username
else:
user_info, user_name = gr.Markdown.update(value="", visible=False), ""
current_model = get_model(llm_model_name=DEPLOYMENT_ID)
current_model.set_user_identifier(user_name)
return user_info, user_name, current_model, get_history_names(False, user_name)
demo.load(create_greeting, inputs=None, outputs=[user_info, user_name, current_model, history_file_dropdown])
examples_questions.click(load_example, inputs=[examples_questions, samples], outputs=[user_input])
index_files.change(update_database, [index_files], [all_files, status_display])
upload_files_btn.change(get_available_files, None, [all_files, acc])
delete_all_btn.click(delete_all, None, [all_files, status_display, index_files])
delete_btn.click(delete_file, [all_files], [all_files, status_display, index_files])
# update_btn.click(update_fb, None, [status_display])
emptyBtn.click(
reset,
inputs=[current_model],
outputs=[chatbot],
show_progress=True,
)
# retryBtn.click(retry, [chatbot, current_model, use_websearch, custom_websearch], [chatbot, status_display])
saveHistoryBtn.click(save_chat_history, [current_model, chatbot, set_save_file_name], [status_display])
historyRefreshBtn.click(get_history_names, [gr.State(False), user_name], [history_file_dropdown])
historyDeleteBtn.click(delete_chat_history, [current_model, history_file_dropdown],
[status_display, history_file_dropdown, chatbot])
history_file_dropdown.change(load_chat_history, [current_model, history_file_dropdown],
[set_save_file_name, chatbot])
record_audio.start_recording(None, None, None,
_js="""
async () => {
document.querySelectorAll('.sm.secondary').forEach(function(element) {
element.classList.remove('secondary');
element.classList.add('tertiary');
});
}
"""
)
record_audio.stop_recording(transcribe, [current_model, record_audio], [user_input, record_audio])
# user_identifier.change(set_user_identifier, [current_model, user_identifier], None)
user_input.submit(predict, [chatbot, current_model, user_input, upload_files_btn, custom_websearch, local_db],
[chatbot, status_display], show_progress=True).then(update_example, [chatbot, set_save_file_name],
[chatbot, examples_questions, samples,
set_save_file_name])
user_input.submit(lambda: "", None, user_input)
submitBtn.click(predict, [chatbot, current_model, user_input, upload_files_btn, custom_websearch, local_db],
[chatbot, status_display], show_progress=True).then(update_example, [chatbot, set_save_file_name],
[chatbot, examples_questions, samples,
set_save_file_name])
submitBtn.click(lambda: "", None, user_input)
demo.queue(concurrency_count=10).launch(
server_name=SEVER, server_port=PORT, auth=get_auth(), debug=DEBUG)
|