Spaces:
Runtime error
Runtime error
File size: 2,430 Bytes
04722c5 |
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 |
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()
|