File size: 4,696 Bytes
79a07f2 d0ac380 b974c1c 79a07f2 | 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 | import gradio as gr
import random
import base64
import os
from openai import OpenAI
# OpenAI API Key aus Environment Variable oder Hugging Face Secret
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# --- Funktion für Bot-Antwort ---
def response(user_message, history):
messages = [{"role": "system", "content": """System Prompt – Five Stars (freundlich, gemütlich, interaktiv)...
"""}]
for msg in history:
messages.append({"role": msg["role"], "content": msg["content"]})
messages.append({"role": "user", "content": user_message})
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
temperature=0.7,
max_tokens=2000
)
return response.choices[0].message.content
# --- Hintergrundbilder als Base64 ---
def get_base64_image(image_path):
try:
with open(image_path, "rb") as f:
encoded = base64.b64encode(f.read()).decode()
return f"data:image/png;base64,{encoded}"
except:
return None
# --- Haupt-App ---
def main():
background_images = [
"./background_images/nosferatu.png",
"./background_images/aftersun.png",
"./background_images/anora.png",
"./background_images/arrival.png",
"./background_images/batman.png",
"./background_images/blackswan.png",
"./background_images/bladerunner.png",
"./background_images/bonesandall.png",
"./background_images/city-of-god.jpg",
"./background_images/drive.png",
"./background_images/dune.png",
"./background_images/fightclub.png",
"./background_images/handmaiden.png",
"./background_images/her.png",
"./background_images/lalaland.png",
"./background_images/lighthouse.png",
"./background_images/littlewomen.png",
"./background_images/memento.png",
"./background_images/pastlives.png",
"./background_images/spiderman.png",
]
selected_bg = random.choice(background_images)
bg_base64 = get_base64_image(selected_bg)
background_style = f"background: url('{bg_base64}') center/cover no-repeat !important;" if bg_base64 else "background: #4a0000 !important;"
# --- CSS ---
custom_css = f"""
* {{
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif !important;
}}
.gradio-container {{
{background_style}
min-height: 100vh;
}}
#chat-container {{
max-width: 800px;
margin: 0 auto;
padding: 40px 20px;
height: 500px;
}}
#logo-container {{
text-align: center;
margin-bottom: 30px;
}}
#chatbot-box {{
background: rgba(255, 255, 255, 0.95);
border-radius: 25px;
padding: 30px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(10px);
}}
footer {{ display: none !important; }}
"""
with gr.Blocks() as demo:
with gr.Column(elem_id="chat-container"):
# Logo
with gr.Row(elem_id="logo-container"):
gr.Image(
"./logo/five-star.png",
show_label=False,
height=130,
width=130,
interactive=False
)
# Chatbot
with gr.Column(elem_id="chatbot-box"):
chatbot = gr.Chatbot(
value=[{"role":"assistant","content":"Hey! Willkommen bei Fivestars! Was kann ich für dich tun?"}],
show_label=False,
height=400,
elem_id="chatbot"
)
with gr.Row():
msg = gr.Textbox(placeholder="...Stelle eine Frage", show_label=False, scale=11)
submit = gr.Button("➤", scale=1)
# --- Event Handling ---
def user_message(user_msg, history):
return "", history + [{"role": "user", "content": user_msg}]
def bot_response(history):
user_msg = history[-1]["content"]
bot_msg = response(user_msg, history)
history.append({"role": "assistant", "content": bot_msg})
return history
msg.submit(user_message, [msg, chatbot], [msg, chatbot]).then(bot_response, chatbot, chatbot)
submit.click(user_message, [msg, chatbot], [msg, chatbot]).then(bot_response, chatbot, chatbot)
# --- Launch mit CSS & Theme für Gradio 6.x ---
demo.launch(css=custom_css, theme=gr.themes.Soft())
if __name__ == "__main__":
main()
|