Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from time import sleep
|
4 |
+
from hugchat import hugchat
|
5 |
+
from rich import print as rp
|
6 |
+
from hugchat.login import Login
|
7 |
+
from gradio_client import Client
|
8 |
+
from dotenv import load_dotenv,find_dotenv
|
9 |
+
load_dotenv(find_dotenv())
|
10 |
+
# Retrieve the hidden authentication and initialisation code from the environment variable
|
11 |
+
unclonables=['HIDDEN_CODE',
|
12 |
+
'INIT_CLIENTS',
|
13 |
+
'CHATBOT_CODE',
|
14 |
+
'IMAGE_CODE'
|
15 |
+
]
|
16 |
+
|
17 |
+
# Ensure the unclonables are valid and not exposed! execute 'TheServerlessUnclonables'
|
18 |
+
for unclonable in unclonables:
|
19 |
+
env_var=os.getenv(unclonable)
|
20 |
+
#rp(env_var)
|
21 |
+
|
22 |
+
exec(os.getenv(unclonable))
|
23 |
+
sleep(1)
|
24 |
+
|
25 |
+
|
26 |
+
# Predefined functions for chatbot actions
|
27 |
+
def switch_model(model_index):
|
28 |
+
print(f"Switching to model {model_index}...")
|
29 |
+
chatbot.switch_llm(model_index)
|
30 |
+
|
31 |
+
def new_conversation(model_index, system_prompt):
|
32 |
+
print(f"Starting new conversation with model {model_index} and system prompt '{system_prompt}'...")
|
33 |
+
chatbot.new_conversation(model_index=model_index, system_prompt=system_prompt, goto=True)
|
34 |
+
|
35 |
+
|
36 |
+
def process_image(prompt):
|
37 |
+
rp(f"running fluxcapacitor...")
|
38 |
+
fluxcapacitor(prompt)
|
39 |
+
|
40 |
+
|
41 |
+
def run_chatbot(prompt ,system_prompt="You are a good assistant", model_index=0, switch=False):
|
42 |
+
chatbot = hugchat.ChatBot(cookies=cookies.get_dict(),system_prompt=system_prompt)
|
43 |
+
chatbot.new_conversation(model_index, system_prompt, switch)
|
44 |
+
chatbot.chat(prompt)
|
45 |
+
|
46 |
+
def test():
|
47 |
+
|
48 |
+
if fluxcapacitor:
|
49 |
+
flux_prompt="A astronaut riding a lollipop in lala land holding a sign with 'TEST' on it!."
|
50 |
+
process_image(flux_prompt)
|
51 |
+
|
52 |
+
# Execute sanitized chatbot logic (no direct exec() here)
|
53 |
+
if chatbot:
|
54 |
+
# Start a new conversation with a given system prompt
|
55 |
+
chat_prompt="Hello make a python game"
|
56 |
+
run_chatbot(chat_prompt)
|
57 |
+
|
58 |
+
test()
|
59 |
+
|
60 |
+
def gradio_interface():
|
61 |
+
with gr.Blocks() as app:
|
62 |
+
with gr.Tab("Chatbot"):
|
63 |
+
chatbot_input = gr.Textbox(placeholder="Enter your message")
|
64 |
+
chatbot_output = gr.Textbox()
|
65 |
+
chatbot_input.submit(run_chatbot, chatbot_input, chatbot_output)
|
66 |
+
|
67 |
+
with gr.Tab("Image Processing"):
|
68 |
+
image_input = gr.Image()
|
69 |
+
image_output = gr.Image()
|
70 |
+
image_input.change(process_image, image_input, image_output)
|
71 |
+
|
72 |
+
app.launch()
|
73 |
+
|
74 |
+
if __name__ == "__main__":
|
75 |
+
gradio_interface()
|