Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext, load_index_from_storage, PromptTemplate
|
| 5 |
+
from llama_index.core import Settings
|
| 6 |
+
from llama_index.llms.openai import OpenAI
|
| 7 |
+
from llama_index.core.llms import ChatMessage, MessageRole
|
| 8 |
+
from llama_index.core.chat_engine.types import ChatMode
|
| 9 |
+
import base64
|
| 10 |
+
|
| 11 |
+
#from theme import CustomTheme
|
| 12 |
+
|
| 13 |
+
#Trainigsdaten
|
| 14 |
+
path_modulhandbuch = "./hm_daten"
|
| 15 |
+
path_persist = os.path.join(path_modulhandbuch, "persist")
|
| 16 |
+
|
| 17 |
+
Settings.llm = OpenAI(temperature=0.1, model="gpt-4o-mini")
|
| 18 |
+
|
| 19 |
+
if not os.path.exists(path_persist):
|
| 20 |
+
documents = SimpleDirectoryReader("./hm_daten/").load_data()
|
| 21 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 22 |
+
index.storage_context.persist(persist_dir=path_persist)
|
| 23 |
+
else:
|
| 24 |
+
storage_context = StorageContext.from_defaults(persist_dir=path_persist)
|
| 25 |
+
index = load_index_from_storage(storage_context)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
#prompt für anfragen
|
| 30 |
+
custom_system_prompt = """
|
| 31 |
+
"We have provided context information below. \n"
|
| 32 |
+
# "---------------------\n"
|
| 33 |
+
# "{context_str}"
|
| 34 |
+
# "\n---------------------\n"
|
| 35 |
+
You are an expert assistant providing detailed and accurate information.
|
| 36 |
+
You help students finding important infromation about their university.
|
| 37 |
+
You always answer professional but friendly, encouraging, youthful und funny.
|
| 38 |
+
If the question is in german give answer in german , else give answer in english: {query_str}
|
| 39 |
+
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
chat_engine = index.as_chat_engine(
|
| 43 |
+
chat_mode=ChatMode.CONDENSE_PLUS_CONTEXT,
|
| 44 |
+
system_prompt=custom_system_prompt,
|
| 45 |
+
streaming=True
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def response(message, history):
|
| 50 |
+
chat_history = []
|
| 51 |
+
for i, msg in enumerate(history):
|
| 52 |
+
if i % 2 == 0:
|
| 53 |
+
history_message = ChatMessage(role=MessageRole.ASSISTANT, content=msg["content"])
|
| 54 |
+
else:
|
| 55 |
+
history_message = ChatMessage(role=MessageRole.USER, content=msg["content"])
|
| 56 |
+
chat_history.append(history_message)
|
| 57 |
+
|
| 58 |
+
streaming_response = chat_engine.stream_chat(message, chat_history=chat_history)
|
| 59 |
+
|
| 60 |
+
answer = ""
|
| 61 |
+
for text in streaming_response.response_gen:
|
| 62 |
+
time.sleep(0.05)
|
| 63 |
+
answer += text
|
| 64 |
+
yield answer
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
#theme = CustomTheme()
|
| 68 |
+
|
| 69 |
+
#background
|
| 70 |
+
with open("./images/bg_hell.jpg", "rb") as image_file:
|
| 71 |
+
encoded_string = base64.b64encode(image_file.read()).decode()
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
custom_css = f"""
|
| 75 |
+
.gradio-container {{
|
| 76 |
+
background: url("data:image/jpeg;base64,{encoded_string}") !important;
|
| 77 |
+
background-size: cover !important;
|
| 78 |
+
background-position: center !important;
|
| 79 |
+
max-width: 100% !important;
|
| 80 |
+
height: auto !important;
|
| 81 |
+
}}
|
| 82 |
+
"""
|
| 83 |
+
#test01
|
| 84 |
+
|
| 85 |
+
def main():
|
| 86 |
+
with gr.Blocks(css = custom_css, css_paths = "./style.css") as demo:
|
| 87 |
+
with gr.Row(equal_height=True):
|
| 88 |
+
with gr.Column(scale=1):
|
| 89 |
+
gr.Image("./images/hochi6.jpg", show_label = False, show_download_button = False, show_share_button = False, show_fullscreen_button = False)
|
| 90 |
+
with gr.Column(scale=8):
|
| 91 |
+
gr.Markdown("")
|
| 92 |
+
with gr.Row(equal_height=True):
|
| 93 |
+
with gr.Column(scale=1, variant = "default"):
|
| 94 |
+
gr.Markdown("")
|
| 95 |
+
with gr.Column(scale=1):
|
| 96 |
+
gr.Image("./images/scroll_test.jpg", show_label = False, show_download_button = False, show_share_button = False, show_fullscreen_button = False)
|
| 97 |
+
with gr.Column(scale=3):
|
| 98 |
+
chatbot = gr.Chatbot(
|
| 99 |
+
value=[{"role": "assistant", "content": "Hi. Du schon wieder. wie kann ich dir helfen?"}],
|
| 100 |
+
type="messages",
|
| 101 |
+
show_label=False,
|
| 102 |
+
avatar_images=("./images/avatar_images/human_2.png", "./images/avatar_images/hochi.png"),
|
| 103 |
+
elem_id="CHATBOT"
|
| 104 |
+
)
|
| 105 |
+
chat_interface = gr.ChatInterface(
|
| 106 |
+
fn=response,
|
| 107 |
+
chatbot=chatbot,
|
| 108 |
+
type="messages"
|
| 109 |
+
)
|
| 110 |
+
with gr.Column(scale=1, variant = "default"):
|
| 111 |
+
gr.Markdown("")
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
demo.launch(inbrowser= True)
|
| 115 |
+
|
| 116 |
+
if __name__ == "__main__":
|
| 117 |
+
main()
|