Download app.py from Titat/FiveStars: direct link, hf CLI and curl.
- Browser
- Download file 4.7 kB
-
https://huggingface.co/spaces/Titat/FiveStars/resolve/main/app.py
- Command line
-
hf download hf://spaces/Titat/FiveStars/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Titat/FiveStars/resolve/main/app.py
4.7 kB
| 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() | |