import gradio as gr import os import time import json qs = {} theme = gr.themes.Base( primary_hue="gray", secondary_hue="gray", ).set( background_fill_primary_dark='*neutral_950', background_fill_secondary_dark='*neutral_950' ) from urllib.parse import parse_qs def parse_query_params(query_string): parsed_params = parse_qs(query_string) simplified_params = {k: v[0] if len(v) == 1 else v for k, v in parsed_params.items()} return simplified_params def app_inits(): global qs # List of JSON file names filenames = ['fileone.json', 'filesecond.json', 'filethird.json', 'filefourth.json', 'filefifth.json'] # Load data from each JSON file for filename in filenames: with open(filename, 'r') as file: data = json.load(file) qs[filename] = data print("Loaded Q & A") print("JSON Files:", filenames) return with gr.Blocks(theme=theme) as demo: chatbot = gr.Chatbot(elem_id="chatbot", layout="panel", avatar_images=("images/user.jpg", "images/bot.jpg"),) def get_params(request: gr.Request): params = request.query_params return str(params) with gr.Row(equal_height=True): msg = gr.Textbox(show_label=False, placeholder="Message ChatGPT...", max_lines=5, container=False,) btn = gr.Button(value="", min_width=80, size="lg", icon="images/button.jpg", scale=0) url_params = gr.State() demo.load(get_params, None, url_params, queue=False) def user(user_message, history): return "", history + [[user_message, None]] def bot(history, url_params): global qs DelayBetweenWords = 0.1 DelayBetweenLetters = 0.0001 if url_params != "": params = parse_query_params(url_params) DelayBetweenWords = float(params.get("dw", DelayBetweenWords)) DelayBetweenLetters = float(params.get("dl", DelayBetweenLetters)) text = history[-1][0] bot_message = "Sorry, I couldn't find an answer to your question." for filename, data in qs.items(): if text in data: bot_message = data[text] break history[-1][1] = "" for character in bot_message: history[-1][1] += character if character == " ": time.sleep(DelayBetweenWords) else: time.sleep(DelayBetweenLetters) yield history btn.click(user, [msg, chatbot], [msg, chatbot], queue=False, show_progress=False).then( bot, [chatbot, url_params], chatbot, concurrency_limit=50 ) msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False, show_progress=False).then( bot, [chatbot, url_params], chatbot, concurrency_limit=50 ) from datetime import datetime filename = 'images/log_data.txt' def log_message(param): timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') log_msg = f'{timestamp}: : {param}\n' with open(filename, 'a') as log_file: log_file.write(log_msg) app_inits() demo.queue() demo.launch(allowed_paths=["."], max_threads=40)