Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import base64
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext, load_index_from_storage, PromptTemplate
|
| 6 |
+
from llama_index.llms.openai import OpenAI
|
| 7 |
+
from llama_index.core import Settings
|
| 8 |
+
|
| 9 |
+
from theme import CustomTheme
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
path_modulhandbuch = "./dokumente"
|
| 13 |
+
path_persist = os.path.join(path_modulhandbuch, "persist")
|
| 14 |
+
|
| 15 |
+
Settings.llm = OpenAI(temperature=0.1, model="gpt-4o-mini")
|
| 16 |
+
|
| 17 |
+
if not os.path.exists(path_persist):
|
| 18 |
+
documents = SimpleDirectoryReader("./dokumente/").load_data()
|
| 19 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 20 |
+
index.storage_context.persist(persist_dir=path_persist)
|
| 21 |
+
else:
|
| 22 |
+
storage_context = StorageContext.from_defaults(persist_dir=path_persist)
|
| 23 |
+
index = load_index_from_storage(storage_context)
|
| 24 |
+
|
| 25 |
+
template = (
|
| 26 |
+
"We have provided context information below. \n"
|
| 27 |
+
"---------------------\n"
|
| 28 |
+
"{context_str}"
|
| 29 |
+
"\n---------------------\n"
|
| 30 |
+
"Given only this information and without using ur general knowledge, please answer in german and address with 'du': {query_str}\n"
|
| 31 |
+
)
|
| 32 |
+
qa_template = PromptTemplate(template)
|
| 33 |
+
query_engine = index.as_query_engine(
|
| 34 |
+
streaming=True, text_qa_template=qa_template)
|
| 35 |
+
|
| 36 |
+
background_path = os.path.join("background", "closet.png")
|
| 37 |
+
with open("background/closet.png", "rb") as image_file:
|
| 38 |
+
encoded_string = base64.b64encode(image_file.read()).decode()
|
| 39 |
+
custom_css = f"""
|
| 40 |
+
.gradio-container {{
|
| 41 |
+
background: url("data:image/png;base64,{encoded_string}") !important;
|
| 42 |
+
background-size: cover !important;
|
| 43 |
+
background-position: center !important;
|
| 44 |
+
max-width: 100% !important;
|
| 45 |
+
height: auto !important;
|
| 46 |
+
}}
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def response(message, history):
|
| 51 |
+
import random
|
| 52 |
+
|
| 53 |
+
# Keywords indicating dissatisfaction with any outfit
|
| 54 |
+
negative_outfit_phrases = ["hässlich", "ugly", "schlecht",
|
| 55 |
+
"nicht gut", "nicht schön", "grässlich", "furchtbar", "katastrophe"]
|
| 56 |
+
|
| 57 |
+
# Keywords indicating interest in new outfits
|
| 58 |
+
buy_new_phrases = ["neu kaufen", "buy new", "neues kaufen", "new outfit"]
|
| 59 |
+
|
| 60 |
+
if any(phrase in message.lower() for phrase in negative_outfit_phrases):
|
| 61 |
+
uplifting_responses = [
|
| 62 |
+
"Es tut mir leid, dass du dich gerade so fühlst. Lass uns zusammen schauen, wie wir dein Outfit aufwerten können – vielleicht mit Accessoires oder einem neuen Styling-Twist! 😊",
|
| 63 |
+
"Mode ist, wie du dich darin fühlst – nicht nur das Kleidungsstück selbst. Ich bin sicher, wir finden etwas, das dich zum Strahlen bringt! 💖",
|
| 64 |
+
"Manchmal machen kleine Details einen großen Unterschied. Vielleicht können wir dein Outfit mit einem Gürtel, einer Jacke oder Schmuck aufpeppen? Soll ich dir helfen? 🌟",
|
| 65 |
+
"Dein Stil ist einzigartig, und das ist etwas Besonderes. Wenn du magst, können wir das Outfit so anpassen, dass es sich mehr wie 'du' anfühlt!",
|
| 66 |
+
"Wir alle haben Tage, an denen wir uns unsicher fühlen. Aber dein Outfit hat Potenzial! Lass uns gemeinsam überlegen, was dir daran gefallen könnte oder wie wir es optimieren können. 💡"
|
| 67 |
+
]
|
| 68 |
+
yield random.choice(uplifting_responses)
|
| 69 |
+
|
| 70 |
+
elif any(keyword in message.lower() for keyword in ["anziehen", "wear", "style"]) and not any(keyword in message.lower() for keyword in ["size", "größe", "fit"]):
|
| 71 |
+
# Check if the user is explicitly asking for new items
|
| 72 |
+
if any(phrase in message.lower() for phrase in buy_new_phrases):
|
| 73 |
+
follow_up = "Suchst du etwas Bestimmtes, das wir neu kaufen könnten? Ich kann dir ein paar trendige Ideen vorschlagen! 😊"
|
| 74 |
+
else:
|
| 75 |
+
follow_up = "Was hast du bereits in deinem Kleiderschrank? Vielleicht können wir etwas kombinieren, anstatt etwas Neues zu kaufen. 😊"
|
| 76 |
+
yield follow_up
|
| 77 |
+
|
| 78 |
+
else:
|
| 79 |
+
# Standard query engine response
|
| 80 |
+
streaming_response = query_engine.query(message)
|
| 81 |
+
|
| 82 |
+
answer = ""
|
| 83 |
+
for text in streaming_response.response_gen:
|
| 84 |
+
time.sleep(0.05)
|
| 85 |
+
answer += text
|
| 86 |
+
yield answer
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
theme = CustomTheme()
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def main():
|
| 93 |
+
chatbot = gr.Chatbot(
|
| 94 |
+
value=[{"role": "assistant",
|
| 95 |
+
"content": "Hey! Was steht heute an? Brauchst du Outfit-Ideen oder Styling-Tipps?"}],
|
| 96 |
+
type="messages",
|
| 97 |
+
show_label=False,
|
| 98 |
+
avatar_images=("./avatar_images/avatar-person.jpeg",
|
| 99 |
+
"./avatar_images/avatar-bot.png"),
|
| 100 |
+
elem_id="CHATBOT"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
with open("background_new.jpg", "rb") as image_file:
|
| 104 |
+
encoded_string = base64.b64encode(image_file.read()).decode()
|
| 105 |
+
custom_css = f"""
|
| 106 |
+
.gradio-container {{
|
| 107 |
+
background: url("data:image/png;base64,{encoded_string}") !important;
|
| 108 |
+
background-size: cover !important;
|
| 109 |
+
background-position: center !important;
|
| 110 |
+
max-width: 100% !important;
|
| 111 |
+
height: auto !important;
|
| 112 |
+
}}"""
|
| 113 |
+
|
| 114 |
+
chatinterface = gr.ChatInterface(
|
| 115 |
+
fn=response,
|
| 116 |
+
chatbot=chatbot,
|
| 117 |
+
type="messages",
|
| 118 |
+
theme=theme,
|
| 119 |
+
css=custom_css,
|
| 120 |
+
css_paths="./styles.css"
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
chatinterface.launch(inbrowser=True)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
if __name__ == "__main__":
|
| 127 |
+
main()
|