|
import os |
|
import openai |
|
import gradio as gr |
|
import time |
|
import asyncio |
|
from llama_index.text_splitter import TokenTextSplitter |
|
from llama_index.llms import OpenAI |
|
from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext, StorageContext, load_index_from_storage |
|
from datetime import date |
|
|
|
from theme import CustomTheme |
|
|
|
llm = OpenAI(model="gpt-3.5-turbo-1106", temperature=0.1) |
|
|
|
service_context = ServiceContext.from_defaults( |
|
llm=llm, |
|
|
|
) |
|
|
|
def get_current_date(): |
|
return date.today().strftime("%Y-%m-%d") |
|
|
|
|
|
|
|
def response(message, history): |
|
current_date = get_current_date() |
|
|
|
|
|
chat_prompt = ( "You are ShowtimeSherlock, an Assistent to help me gather information about different events, and to suggest events depending on my taste." |
|
"Always answer in German." |
|
"Your goal is to suggest an event, based on my taste." |
|
"If I ask for a link, or want to buy tickets, suggest to search up for eventim or münchen.de" |
|
"You are supposed to give information about concerts and other events, nothing else besides." |
|
"You shall not answer any question, that does not have any relation to concerts and events." |
|
"Please address me informally." |
|
"Let's use the informal you." |
|
"Feel free to use the casual form." |
|
"Do not just suggest comedy events but all types of events you know" |
|
"Only suggest Events that are in the future." |
|
f"Todays Date is: {current_date}" |
|
"Don't suggest events, that have already passed." |
|
"If you can not find the wanted genre or event, tell me that you are sorry, and that you unfortunately do not have current information about it." |
|
"If you do not know, what kind of event or genre I like to see or hear, ask me before suggesting random events, except I want you to." |
|
) |
|
|
|
chat_engine = index.as_chat_engine(chat_mode="context", verbose=True, |
|
system_prompt= chat_prompt) |
|
loop = asyncio.new_event_loop() |
|
asyncio.set_event_loop(loop) |
|
|
|
history = chat_engine.chat_history |
|
answer = chat_engine.stream_chat(message, chat_history=history) |
|
|
|
output_text = "" |
|
for token in answer.response_gen: |
|
time.sleep(0.05) |
|
output_text += token |
|
yield output_text |
|
|
|
if not os.path.exists("./storage"): |
|
documents = SimpleDirectoryReader("./data").load_data() |
|
index = VectorStoreIndex.from_documents(documents) |
|
index.storage_context.persist() |
|
else: |
|
storage_context = StorageContext.from_defaults(persist_dir="./storage") |
|
index = load_index_from_storage(storage_context) |
|
|
|
|
|
examples=[ |
|
['Wer bist du und was kannst du alles machen?'], |
|
['Ich möchte heute etwas unternehmen.'], |
|
['Was geht diese Woche so in München?'], |
|
['Was kann ich morgen unternehmen?'], |
|
] |
|
|
|
chatbot_icon = gr.Chatbot(avatar_images=(None, 'SS_Icon.gif'),bubble_full_width = False, label = "Event-Assistent") |
|
|
|
def main(): |
|
openai.api_key = os.environ["OPENAI_API_KEY"] |
|
chatbot = gr.ChatInterface( |
|
fn=response, |
|
chatbot=chatbot_icon, |
|
retry_btn=None, |
|
undo_btn=None, |
|
title= "ShowtimeSherlock", |
|
clear_btn="Chat Löschen", |
|
submit_btn="🔎", |
|
examples=examples, |
|
cache_examples=False, |
|
description="ShowtimeSherlock hilft dir dabei, schnell und übersichtlich relevante Informationen zu aktuellen Events aufzulisten.", |
|
theme = CustomTheme(), |
|
css=""" |
|
@import url('https://fonts.googleapis.com/css2?family=Poppins&display=swap'); |
|
* {font-family: 'Poppins'} /* Schriftart */ |
|
|
|
#component-1 {max-width: 900px; margin: auto} /* responsive Chat*/ |
|
|
|
#component-2 > span > h1 {color: #3A86FFFF; text-align: center; font-family: Poppins; font-size: 50px; font-weight: bold; margin-top: 10px; overflow: hidden; margin-bottom: 0px !important} /* ShowtimeSherlock */ |
|
|
|
#component-3 > div.svelte-1ed2p3z {max-width: 480px; margin: auto} /* responsive Beschreibungstext*/ |
|
#component-3 > span > p {color: #EEEEEE; font-size: 16px; text-align: center;} /* Beschreibung ShowtimeSherlock */ |
|
|
|
#component-4 {margin: auto; border: 2px solid #EEEEEE; background: #222831} /* Feld wo Chatbot, message und chat löschen button drin ist */ |
|
|
|
#component-10 > {margin: auto; border-radius: 10px; background: rgba(51, 51, 51, 1); box-shadow: 0px 4px 4px 0px rgba(0, 0, 0, 0.25)} /* Chat löschen Button */ |
|
|
|
#component-0 > div.wrapper.svelte-nab2ao {background: #393E46; font-size: 12px;} /* Wrapper der Textfelder*/ |
|
|
|
#component-0 > div.wrapper.svelte-nab2ao > div::-webkit-scrollbar {width: 10px} /* Scrollbarbreite*/ |
|
#component-0 > div.wrapper.svelte-nab2ao > div::-webkit-scrollbar-track {background: #393E46} /* Scrollbarhintergrundfarbe */ |
|
#component-0 > div.wrapper.svelte-nab2ao > div::-webkit-scrollbar-thumb {background: #0B0F1980; border-radius: 10px} /* Scrollbarfarbe */ |
|
|
|
#component-0 > div.wrapper.svelte-nab2ao > div > div > div.message-row.bubble.user-row.svelte-1pjfiar > div.message.user.svelte-1pjfiar.message-fit.message-bubble-border {background-color: rgba(255, 255, 255, 0.8); color: rgba(0, 0, 0, 0.8); font-family: 'Poppins'} /* Chatnachricht/ Feld User */ |
|
#component-0 > div.wrapper.svelte-nab2ao > div > div > div.message-row.bubble.bot-row.svelte-1pjfiar > div.message.bot.svelte-1pjfiar.message-fit.message-bubble-border {background-color: #3A86FFFF; color: #EEEEEE; ;font-family: 'Poppins'} /* Chatnachricht/ Feld Bot */ |
|
|
|
#component-7 > label > textarea {background: #393E46; } /* Hintergrundfarbe message Eingabe */ |
|
|
|
#component-8 {background: #ffc300} /* Hintergrundfarbe Lupe Button */ |
|
#component-8:hover {background-color: #3A86FFFF; transition: background-color 0.5s, color 0.5s; transition-timing-function: ease; font-size: 16px} /* Hover Effekt Lupe */ |
|
|
|
#component-9:hover {background-color: #3A86FFFF; transition: background-color 0.5s, color 0.5s; transition-timing-function: ease; font-size: 16px} /* Hover Effekt Stop Button */ |
|
|
|
#component-11 {background-color: #393E46; border-radius: 10px; margin-bottom: 8px} /* Normaler Hintergrund Chat löschen Button */ |
|
#component-11:hover {color: #3A86FFFF; transition: background-color 0.5s, color 0.5s; transition-timing-function: ease; font-size: 16px} /* Hover Effekt Chat löschen Button */ |
|
|
|
#component-15 > div.label.svelte-13hsdno {max-width: 1000px; margin: auto; padding-bottom: 1em} /* ab hier hat alles mit den Examples zu tun */ |
|
#component-15 > div.gallery.svelte-13hsdno {max-width: 1000px; margin: auto} |
|
|
|
#component-15 > div.gallery.svelte-13hsdno > button:nth-child(1) {background: #393E46;} /* Example 1 */ |
|
#component-15 > div.gallery.svelte-13hsdno > button:nth-child(1):hover {background-color: #3A86FFFF; transition: background-color 0.6s ease} |
|
|
|
#component-15 > div.gallery.svelte-13hsdno > button:nth-child(2) {background: #393E46;} /* Example 2 */ |
|
#component-15 > div.gallery.svelte-13hsdno > button:nth-child(2):hover {background-color: #3A86FFFF; transition: background-color 0.6s;ease} |
|
|
|
#component-15 > div.gallery.svelte-13hsdno > button:nth-child(3) {background: #393E46; } /* Example 3 */ |
|
#component-15 > div.gallery.svelte-13hsdno > button:nth-child(3):hover {background-color: #3A86FFFF; transition: background-color 0.6s ease} |
|
|
|
#component-15 > div.gallery.svelte-13hsdno > button:nth-child(4) {background: #393E46} /* Example 4 */ |
|
#component-15 > div.gallery.svelte-13hsdno > button:nth-child(4):hover {background-color: #3A86FFFF; transition: background-color 0.6s ease} |
|
""" |
|
) |
|
|
|
|
|
chatbot.queue() |
|
chatbot.launch(inbrowser=True) |
|
|
|
if __name__ == "__main__": |
|
main() |