cyberandy/wl-chat
1
1import gradio as gr2import requests3import time4import ast5import os6 7## Adding the Theme here ##8 9wordlift_theme = gr.themes.Soft(10 primary_hue=gr.themes.Color(11 c50="#007AFF",12 c100="rgba(0, 122, 255, 0.2)",13 c200="#007AFF",14 c300="rgba(0, 122, 255, 0.32)",15 c400="rgba(0, 122, 255, 0.32)",16 c500="rgba(0, 122, 255, 1.0)",17 c600="rgba(0, 122, 255, 1.0)",18 c700="rgba(0, 122, 255, 0.32)",19 c800="rgba(0, 122, 255, 0.32)",20 c900="#007AFF",21 c950="#007AFF",22 ),23 secondary_hue=gr.themes.Color(24 c50="#576b95",25 c100="#576b95",26 c200="#576b95",27 c300="#576b95",28 c400="#576b95",29 c500="#576b95",30 c600="#576b95",31 c700="#576b95",32 c800="#576b95",33 c900="#576b95",34 c950="#576b95",35 ),36 neutral_hue=gr.themes.Color(37 name="gray",38 c50="#f9fafb",39 c100="#f3f4f6",40 c200="#e5e7eb",41 c300="#d1d5db",42 c400="#B2B2B2",43 c500="#808080",44 c600="#636363",45 c700="#515151",46 c800="#393939",47 c900="#272727",48 c950="#171717",49 ),50 radius_size=gr.themes.sizes.radius_sm,51).set(52 button_primary_background_fill="#2196F3",53 button_primary_background_fill_dark="#2196F3",54 button_primary_background_fill_hover="#007AFF",55 button_primary_border_color="#2196F3",56 button_primary_border_color_dark="#2196F3",57 button_primary_text_color="#FFFFFF",58 button_primary_text_color_dark="#FFFFFF",59 button_secondary_background_fill="#F2F2F2",60 button_secondary_background_fill_dark="#2B2B2B",61 button_secondary_text_color="#393939",62 button_secondary_text_color_dark="#FFFFFF",63 # background_fill_primary="#F7F7F7",64 # background_fill_primary_dark="#1F1F1F",65 block_title_text_color="*primary_500",66 block_title_background_fill="*primary_100",67 input_background_fill="#F6F6F6",68)69 70# _________________________________________________________________#71 72 73def main():74 # Load CSS75 css_path = os.path.join(os.getcwd(), "assets", "custom.css")76 app = gr.Interface(fn=response, inputs=[msg, chatbot], outputs=[msg, chatbot], server_name="localhost")77 app.load_css(css_path)78 app.launch(share=True, inline=True)79 80def bot(user_message, history):81 # POST request to the API82 response = requests.post(83 'https://langchain-cd369f3121.wolf.jina.ai/ask',84 headers={'accept': 'application/json', 'Content-Type': 'application/json'},85 json={"input": user_message, "envs": {}}86 )87 # Extract the bot's response from the API response88 data_str = response.json()['result']89 90 # Print out the response status and content for debugging91 print("Response status:", response.status_code)92 print("Response content:", response.content)93 94 # Try to parse the response as a tuple95 try:96 data = ast.literal_eval(data_str)97 # Check if the parsed data is a tuple with sources98 if isinstance(data, tuple) and len(data) == 2:99 bot_message = data[0]100 sources = ', '.join(data[1])101 # Update the chat history with the bot's response and sources102 history.append([user_message, f"{bot_message}\n\nLearn more: {sources}"])103 else:104 raise ValueError # Not a tuple with sources, treat it as a plain text message105 except (SyntaxError, ValueError):106 # The response is just a plain text message107 bot_message = data_str108 # Update the chat history with the bot's response109 history.append([user_message, bot_message])110 111 return history112 113#gr.Chatbot.postprocess = postprocess114 115with open("assets/custom.css", "r", encoding="utf-8") as f:116 customCSS = f.read()117 118with gr.Blocks(css=customCSS, theme=wordlift_theme) as demo:119 chatbot = gr.Chatbot(elem_id="chuanhu_chatbot").style(height="100%")120 msg = gr.Textbox(label=" 🪄", placeholder="Type a message to the bot and press enter").style(container=False)121 clear = gr.Button("🧹 Start fresh")122 123 response = msg.submit(bot, [msg, chatbot], [chatbot], queue=False)124 response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)125 126 clear.click(lambda: None, None, chatbot, queue=False)127 128#demo = gr.Interface(fn=bot, inputs=msg, outputs=chatbot, theme="dark", title="WL-Chat", description="A chatbot to interact with WordLift's blog.")129 130demo.queue()131demo.launch(inline=True)132 133if __name__ == "__main__":134 main()