Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import os | |
| from time import sleep | |
| from hugchat import hugchat | |
| from rich import print as rp | |
| from hugchat.login import Login | |
| from gradio_client import Client | |
| from dotenv import load_dotenv,find_dotenv | |
| load_dotenv(find_dotenv()) | |
| # Retrieve the hidden authentication and initialisation code from the environment variable | |
| unclonables=['HIDDEN_CODE', | |
| 'INIT_CLIENTS', | |
| 'CHATBOT_CODE', | |
| 'IMAGE_CODE' | |
| ] | |
| # Ensure the unclonables are valid and not exposed! execute 'TheServerlessUnclonables' | |
| for unclonable in unclonables: | |
| env_var=os.getenv(unclonable) | |
| #rp(env_var) | |
| exec(os.getenv(unclonable)) | |
| sleep(1) | |
| # Predefined functions for chatbot actions | |
| def switch_model(model_index): | |
| print(f"Switching to model {model_index}...") | |
| chatbot.switch_llm(model_index) | |
| def new_conversation(model_index, system_prompt): | |
| print(f"Starting new conversation with model {model_index} and system prompt '{system_prompt}'...") | |
| chatbot.new_conversation(model_index=model_index, system_prompt=system_prompt, goto=True) | |
| def process_image(prompt): | |
| rp(f"running fluxcapacitor...") | |
| fluxcapacitor(prompt) | |
| def run_chatbot(prompt ,system_prompt="You are a good assistant", model_index=0, switch=False): | |
| chatbot = hugchat.ChatBot(cookies=cookies.get_dict(),system_prompt=system_prompt) | |
| chatbot.new_conversation(model_index, system_prompt, switch) | |
| chatbot.chat(prompt) | |
| def test(): | |
| if fluxcapacitor: | |
| flux_prompt="A astronaut riding a lollipop in lala land holding a sign with 'TEST' on it!." | |
| process_image(flux_prompt) | |
| # Execute sanitized chatbot logic (no direct exec() here) | |
| if chatbot: | |
| # Start a new conversation with a given system prompt | |
| chat_prompt="Hello make a python game" | |
| run_chatbot(chat_prompt) | |
| test() | |
| def gradio_interface(): | |
| with gr.Blocks() as app: | |
| with gr.Tab("Chatbot"): | |
| chatbot_input = gr.Textbox(placeholder="Enter your message") | |
| chatbot_output = gr.Textbox() | |
| chatbot_input.submit(run_chatbot, chatbot_input, chatbot_output) | |
| with gr.Tab("Image Processing"): | |
| image_input = gr.Image() | |
| image_output = gr.Image() | |
| image_input.change(process_image, image_input, image_output) | |
| app.launch() | |
| if __name__ == "__main__": | |
| gradio_interface() | |