Spaces:
Runtime error
Runtime error
File size: 7,192 Bytes
8846768 6e1a58f 8846768 6e1a58f 8846768 6e1a58f 8846768 6e1a58f 8846768 6e1a58f 8846768 6e1a58f 8846768 6e1a58f 8846768 6e1a58f 8846768 6e1a58f 8846768 db77dfe 6e1a58f 8846768 | 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | import os
import time
import gradio as gr
import base64
import random
#from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext, load_index_from_storage, PromptTemplate
#from llama_index.llms.openai import OpenAI
#from llama_index.core import Settings
#from llama_index.core.llms import ChatMessage, MessageRole
#from llama_index.core.chat_engine.types import ChatMode
from openai import OpenAI
from theme import CustomTheme
import Blackwell
#from openai import OpenAI
client = OpenAI()
path_modulhandbuch = "./dokumente"
path_persist = os.path.join(path_modulhandbuch, "persist")
"""
Settings.llm = OpenAI(temperature=0.1, model="gpt-4o-mini")
if not os.path.exists(path_persist):
documents = SimpleDirectoryReader(path_modulhandbuch).load_data()
index = VectorStoreIndex.from_documents(documents)
index.storage_context.persist(persist_dir=path_persist)
else:
storage_context = StorageContext.from_defaults(persist_dir=path_persist)
index = load_index_from_storage(storage_context)
"""
template = (
"We have provided context information below. \n"
"---------------------\n"
"{context_str}"
"\n---------------------\n"
"Given only this information and without using your general knowledge, "
"please answer the question in the style of an aggressive detective and always answer in English. "
" Maximum 5 sentences answers: {query_str}\n"
)
qa_template = {"template": template}
def visible():
text_area = gr.TextArea(value="TEXTAREA", visible=True)
return text_area
def response(history):
chat_history = []
for i, msg in enumerate(history):
if i % 2 == 0:
history_message = {"role": "assistant", "content": msg["content"]}
else:
history_message = {"role": "user", "content": msg["content"]}
chat_history.append(history_message)
# Streaming der Antwort mit der Chat-Engine
message = history[-1]["content"]
completion = client.chat.completions.create(
model="gpt-4o-mini",
stream=True,
messages=[
{ "role": "system",
"content": Blackwell.SystemPrompt},
{ "role": "user",
"content": message}
]
)
#streaming_response = chat_engine.stream_chat(message, chat_history=chat_history)
history.append({"role": "assistant", "content": ""})
for chunk in completion:
text = ""
if chunk.choices and chunk.choices[0].delta.content:
text = chunk.choices[0].delta.content
# # Verzögerung von 50 ms pro Antwortteil, um das Streaming zu simulieren
time.sleep(0.05)
history[-1]["content"] += text
yield history # Gibt die Antwort in Teilen zurück
def user(message, history):
return "", history + [{"role": "user", "content": message}]
is_visible_up = None
is_visible_mid = None
is_visible_down = None
def show_hint(history):
global is_visible_up
global is_visible_mid
global is_visible_down
if len(history) == 3:
hint_index = random.randint(1, 4)
is_visible_up = f"./hint_pics/hint_{hint_index}.jpg"
elif len(history) == 5:
hint_index = random.randint(4, 7)
is_visible_mid = f"./hint_pics/hint_{hint_index}.jpg"
elif len(history) == 7:
hint_index = random.randint(7, 10)
is_visible_down = f"./hint_pics/hint_{hint_index}.jpg"
up_image = gr.Image(value=is_visible_up, elem_classes="fixed-image-up", show_label= False, show_download_button= False, visible=False if not is_visible_up else True)
mid_image = gr.Image(value=is_visible_mid, elem_classes="fixed-image-mid", show_label= False, show_download_button= False, visible=False if not is_visible_mid else True)
down_image = gr.Image(value=is_visible_down, elem_classes="fixed-image-down", show_label= False, show_download_button= False, visible=False if not is_visible_down else True)
return up_image, mid_image, down_image
def main():
with open("./avatar_images/background_intergorationroom.png", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode()
theme = CustomTheme()
# Dynamic CSS for background
custom_css = f"""
.gradio-container {{
background: url("data:image/png;base64,{encoded_string}") !important;
background-size: cover !important;
background-position: center !important;
}}
"""
# Layout
with gr.Blocks(css=custom_css, css_paths="./style.css", theme=theme, fill_height=True) as chatinterface:
with gr.Row(equal_height=True): # No equal_width argument
with gr.Column(): # First column for hints
text_area = gr.TextArea(value="TEXTAREA", visible=False) # First empty column
with gr.Row(elem_classes="white-box", visible=True):
gr.Markdown("## Here are your tips:", elem_classes="heading"),
up_image = gr.Image(value="./hint_pics/hint_3.jpg", elem_classes="fixed-image-up", show_label= False, show_download_button= False, visible=False)
mid_image = gr.Image(value="./hint_pics/hint_6.jpg", elem_classes="fixed-image-mid", show_label= False, show_download_button= False, visible=False)
down_image = gr.Image(value="./hint_pics/hint_7.jpg", elem_classes="fixed-image-down", show_label= False, show_download_button= False, visible=False)
with gr.Column(): # Second column for picture of detectiv, timer and scale
timer = gr.Textbox(
lines=1,
value=" messages left",
show_label=False,
container=False
)
scale = gr.Textbox(lines=1, value="scale", show_label=False, container=False)
picture_of_detectiv = gr.Image(
value="avatar_images/Detective_animated.gif",
show_label=False,
show_download_button=False,
show_fullscreen_button=False,
height="38vw",
container=False
)
with gr.Column(): # Third column for chatbot and input box
chatbot = gr.Chatbot(
value=[{"role": "assistant", "content": "Well, well... look who decided to wake up."}],
type="messages",
show_label=False,
elem_id="CHATBOT",
height="42vw"
)
input_box = gr.Textbox(
show_label= False,
elem_id="USER_INPUT",
placeholder="Type your message here...",
interactive=True,
submit_btn=True
)
input_box.submit(user, inputs=[input_box, chatbot], outputs=[input_box,chatbot]).then(response, inputs=[chatbot], outputs=[chatbot]).then(show_hint, chatbot, [up_image, mid_image, down_image])
chatinterface.launch(show_api=False)
if __name__ == "__main__":
main() |