Upload 4 files
Browse files- app_RAG.py +140 -0
- requirements.txt +4 -0
- style.css +13 -0
- theme.py +13 -0
app_RAG.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
import base64
|
| 4 |
+
import os
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
|
| 7 |
+
# OpenAI API Key aus Hugging Face Secret
|
| 8 |
+
client = OpenAI(api_key="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")
|
| 9 |
+
|
| 10 |
+
# --- Funktion für Bot-Antwort ---
|
| 11 |
+
def response(user_message, history):
|
| 12 |
+
messages = [{"role": "system", "content": """System Prompt – Five Stars (freundlich, gemütlich, interaktiv)...
|
| 13 |
+
"""}]
|
| 14 |
+
|
| 15 |
+
for msg in history:
|
| 16 |
+
messages.append({"role": msg["role"], "content": msg["content"]})
|
| 17 |
+
|
| 18 |
+
messages.append({"role": "user", "content": user_message})
|
| 19 |
+
|
| 20 |
+
response = client.chat.completions.create(
|
| 21 |
+
model="gpt-4o-mini",
|
| 22 |
+
messages=messages,
|
| 23 |
+
temperature=0.7,
|
| 24 |
+
max_tokens=2000
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
return response.choices[0].message.content
|
| 28 |
+
|
| 29 |
+
# --- Hintergrundbilder als Base64 ---
|
| 30 |
+
def get_base64_image(image_path):
|
| 31 |
+
try:
|
| 32 |
+
with open(image_path, "rb") as f:
|
| 33 |
+
encoded = base64.b64encode(f.read()).decode()
|
| 34 |
+
return f"data:image/png;base64,{encoded}"
|
| 35 |
+
except:
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
# --- Haupt-App ---
|
| 39 |
+
def main():
|
| 40 |
+
background_images = [
|
| 41 |
+
"./background_images/nosferatu.png",
|
| 42 |
+
"./background_images/aftersun.png",
|
| 43 |
+
"./background_images/anora.png",
|
| 44 |
+
"./background_images/arrival.png",
|
| 45 |
+
"./background_images/batman.png",
|
| 46 |
+
"./background_images/blackswan.png",
|
| 47 |
+
"./background_images/bladerunner.png",
|
| 48 |
+
"./background_images/bonesandall.png",
|
| 49 |
+
"./background_images/city-of-god.jpg",
|
| 50 |
+
"./background_images/drive.png",
|
| 51 |
+
"./background_images/dune.png",
|
| 52 |
+
"./background_images/fightclub.png",
|
| 53 |
+
"./background_images/handmaiden.png",
|
| 54 |
+
"./background_images/her.png",
|
| 55 |
+
"./background_images/lalaland.png",
|
| 56 |
+
"./background_images/lighthouse.png",
|
| 57 |
+
"./background_images/littlewomen.png",
|
| 58 |
+
"./background_images/memento.png",
|
| 59 |
+
"./background_images/pastlives.png",
|
| 60 |
+
"./background_images/spiderman.png",
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
selected_bg = random.choice(background_images)
|
| 64 |
+
bg_base64 = get_base64_image(selected_bg)
|
| 65 |
+
|
| 66 |
+
background_style = f"background: url('{bg_base64}') center/cover no-repeat !important;" if bg_base64 else "background: #4a0000 !important;"
|
| 67 |
+
|
| 68 |
+
# --- CSS ---
|
| 69 |
+
custom_css = f"""
|
| 70 |
+
* {{
|
| 71 |
+
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif !important;
|
| 72 |
+
}}
|
| 73 |
+
.gradio-container {{
|
| 74 |
+
{background_style}
|
| 75 |
+
min-height: 100vh;
|
| 76 |
+
}}
|
| 77 |
+
#chat-container {{
|
| 78 |
+
max-width: 800px;
|
| 79 |
+
margin: 0 auto;
|
| 80 |
+
padding: 40px 20px;
|
| 81 |
+
height: 500px;
|
| 82 |
+
}}
|
| 83 |
+
#logo-container {{
|
| 84 |
+
text-align: center;
|
| 85 |
+
margin-bottom: 30px;
|
| 86 |
+
}}
|
| 87 |
+
#chatbot-box {{
|
| 88 |
+
background: rgba(255, 255, 255, 0.95);
|
| 89 |
+
border-radius: 25px;
|
| 90 |
+
padding: 30px;
|
| 91 |
+
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
|
| 92 |
+
backdrop-filter: blur(10px);
|
| 93 |
+
}}
|
| 94 |
+
footer {{ display: none !important; }}
|
| 95 |
+
"""
|
| 96 |
+
|
| 97 |
+
with gr.Blocks() as demo:
|
| 98 |
+
with gr.Column(elem_id="chat-container"):
|
| 99 |
+
# Logo
|
| 100 |
+
with gr.Row(elem_id="logo-container"):
|
| 101 |
+
gr.Image(
|
| 102 |
+
"./logo/five-star.png",
|
| 103 |
+
show_label=False,
|
| 104 |
+
height=130,
|
| 105 |
+
width=130,
|
| 106 |
+
interactive=False
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# Chatbot
|
| 110 |
+
with gr.Column(elem_id="chatbot-box"):
|
| 111 |
+
chatbot = gr.Chatbot(
|
| 112 |
+
value=[{"role":"assistant","content":"Hey! Willkommen bei Fivestars! Was kann ich für dich tun?"}],
|
| 113 |
+
type="messages",
|
| 114 |
+
show_label=False,
|
| 115 |
+
height=400,
|
| 116 |
+
elem_id="chatbot"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
with gr.Row():
|
| 120 |
+
msg = gr.Textbox(placeholder="...Stelle eine Frage", show_label=False, scale=11)
|
| 121 |
+
submit = gr.Button("➤", scale=1)
|
| 122 |
+
|
| 123 |
+
# --- Event Handling ---
|
| 124 |
+
def user_message(user_msg, history):
|
| 125 |
+
return "", history + [{"role": "user", "content": user_msg}]
|
| 126 |
+
|
| 127 |
+
def bot_response(history):
|
| 128 |
+
user_msg = history[-1]["content"]
|
| 129 |
+
bot_msg = response(user_msg, history)
|
| 130 |
+
history.append({"role": "assistant", "content": bot_msg})
|
| 131 |
+
return history
|
| 132 |
+
|
| 133 |
+
msg.submit(user_message, [msg, chatbot], [msg, chatbot]).then(bot_response, chatbot, chatbot)
|
| 134 |
+
submit.click(user_message, [msg, chatbot], [msg, chatbot]).then(bot_response, chatbot, chatbot)
|
| 135 |
+
|
| 136 |
+
# --- Launch mit CSS & Theme für Gradio 6.x ---
|
| 137 |
+
demo.launch(css=custom_css, theme=gr.themes.Soft())
|
| 138 |
+
|
| 139 |
+
if __name__ == "__main__":
|
| 140 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
gradio==5.49.1
|
| 3 |
+
PyPDF2
|
| 4 |
+
chromadb
|
style.css
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
.message.bot {
|
| 3 |
+
background: #e7e7e7 !important;
|
| 4 |
+
font-family: "Helvetica Neue" !important;
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
.message.user {
|
| 8 |
+
background: #e7e7e7 !important;
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
#CHATBOT {
|
| 12 |
+
background: #ffffff !important;
|
| 13 |
+
}
|
theme.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from gradio.themes.base import Base
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class CustomTheme(Base):
|
| 5 |
+
def __init__(self):
|
| 6 |
+
super().__init__()
|
| 7 |
+
|
| 8 |
+
super().set(
|
| 9 |
+
body_background_fill="#942828",
|
| 10 |
+
body_background_fill_dark="#737070",
|
| 11 |
+
input_background_fill="#ffffff",
|
| 12 |
+
input_background_fill_dark="#9d9b9b"
|
| 13 |
+
)
|