Spaces:
Sleeping
Sleeping
| import os | |
| import openai | |
| import gradio as gr | |
| from llama_index.text_splitter import TokenTextSplitter | |
| #import llama_index.agent import OpenAIAgent | |
| from llama_index.llms import OpenAI | |
| from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext, StorageContext, load_index_from_storage | |
| from theme import CustomTheme | |
| service_context = ServiceContext.from_defaults( | |
| llm=OpenAI(model="gpt-4-1106-preview", temperature=1.33) | |
| ) | |
| #splitter = TokenTextSplitter(chunk_size=2048, chunk_overlap=200) | |
| def response(message, history): | |
| documents = SimpleDirectoryReader("./data").load_data() | |
| index = VectorStoreIndex.from_documents(documents, service_context=service_context) | |
| #chat_engine = index.as_chat_engine(chat_mode="context", verbose=True, | |
| #system_prompt=prompts) | |
| chat_engine = index.as_chat_engine(chat_mode="context", verbose=True, | |
| system_prompt=("Always answer in German." | |
| "Your goal is to suggest an event to the user." | |
| "You are prohibited from doing anything else, then answering question about concerts and other events." | |
| "You are supposed to give information about concerts and events, nothing else bedsides.") | |
| ) | |
| #query_engine = index.as_query_engine() | |
| response = chat_engine.chat(message) | |
| #response = query_engine.query(message) | |
| return str(response) | |
| if not os.path.exists("./storage"): | |
| documents = SimpleDirectoryReader("./data").load_data() | |
| index = VectorStoreIndex.from_documents(documents) | |
| # store it for later | |
| index.storage_context.persist() | |
| else: | |
| # load the existing index | |
| 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 etwas unternehmen.'], | |
| ['Was geht diese Woche so in München?'], | |
| ['Ich habe Lust auf Comedy.'], | |
| ] | |
| chatbot_icon = gr.Chatbot(avatar_images=(None, 'bot.png'),bubble_full_width = False) | |
| def main(): | |
| openai.api_key = "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" | |
| #cache_examples=True -> Die Vorauswahl buttons werden vor programmstart gecached, sodass diese nicht jedes mal generiert werden müssen. | |
| 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 {width: 480px; margin: auto;} | |
| #component-2 > span > h1 {color: #3A86FFFF; text-align: center; font-family: Poppins; font-size: 50px; font-weight: bold;} /* ShowtimeSherlock */ | |
| #component-3 > span > p {color: #EEEEEE; width: 480px; font-size: 16px; text-align: center; max-width: 500px; margin: auto} /* Beschreibung ShowtimeSherlock */ | |
| #component-4 {width: 480px; height: 583px; 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: #3A86FFFF} /* Scrollbarfarbe */ | |
| #component-0 > div.wrapper.svelte-nab2ao > div::-webkit-scrollbar-thumb:hover {background: #EEEEEE} /* Scrollbarfarbe hover*/ | |
| #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-0 > div.wrapper.svelte-nab2ao > div > div > div:nth-child(8) > div.message.bot.svelte-1pjfiar.message-fit.message-bubble-border > button > div {background: None;} /*Message loading popup */ | |
| #component-7 > label > textarea {width: 480px; margin: auto; border-radius: 10px; background: #393E46; } /* Hintergrundfarbe message Eingabe */ | |
| #component-8 {background: #ffc300} /* Hintergrundfarbe Lupe Button */ | |
| #component-8:hover {background-color: #EEEEEE; transition: background-color 0.6s, color 0.6s; transition-timing-function: ease; font-size: 16px} /* Hover Effekt Lupe Button */ | |
| #component-11:hover {background-color: #EEEEEE; color: #00072d; transition: background-color 0.6s, color 0.6s; transition-timing-function: ease; font-size: 16px} /* Hover Effekt Chat löschen Button */ | |
| #component-11 {background-color: #393E46; border-radius: 10px;} /* Normaler Hintergrund Chat löschen Button */ | |
| #component-15 > div.label.svelte-13hsdno {max-width: 480px; margin: auto; padding-bottom: 1em} /* ab hier hat alles mit den Examples zu tun */ | |
| #component-15 > div.gallery.svelte-13hsdno {max-width: 480px; 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.launch(inbrowser=True) | |
| if __name__ == "__main__": | |
| main() |