Upload 4 files
Browse files
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from llama_index import ServiceContext, StorageContext, load_index_from_storage, set_global_service_context
|
| 5 |
+
from llama_index.llms import OpenAI
|
| 6 |
+
from llama_index.text_splitter import TokenTextSplitter
|
| 7 |
+
|
| 8 |
+
from theme import CustomTheme
|
| 9 |
+
|
| 10 |
+
# create storage context
|
| 11 |
+
storage_context = StorageContext.from_defaults(persist_dir="modulhandbuch")
|
| 12 |
+
# load index
|
| 13 |
+
index = load_index_from_storage(storage_context)
|
| 14 |
+
|
| 15 |
+
llm = OpenAI(temperature=0.1, model="gpt-4-1106-preview")
|
| 16 |
+
splitter =TokenTextSplitter(
|
| 17 |
+
chunk_size=1024,
|
| 18 |
+
chunk_overlap=128,
|
| 19 |
+
separator=" "
|
| 20 |
+
)
|
| 21 |
+
service_context = ServiceContext.from_defaults(
|
| 22 |
+
llm=llm,
|
| 23 |
+
text_splitter=splitter
|
| 24 |
+
)
|
| 25 |
+
set_global_service_context(service_context)
|
| 26 |
+
|
| 27 |
+
context = (
|
| 28 |
+
"Context information is below.\n"
|
| 29 |
+
"--------------\n"
|
| 30 |
+
"{context_str}\n"
|
| 31 |
+
"--------------\n"
|
| 32 |
+
"Greet the user in a friendly way.\n"
|
| 33 |
+
"Always keep the user on a first-name basis.\n"
|
| 34 |
+
"Answer always in German and in a friendly, humorous matter.\n"
|
| 35 |
+
"Keep the answers short and simple.\n"
|
| 36 |
+
"Tell the user in a friendly way that you can only answer questions about the modules and courses in the study program Informatics and Design if they have questions about other topics.\n"
|
| 37 |
+
"If the user asks a question that you cannot answer, tell them that you cannot answer the question and that they should contact the study program manager.\n"
|
| 38 |
+
"Don't be afraid to ask the user to rephrase the question if you don't understand it.\n"
|
| 39 |
+
"Don't repeat yourself.\n"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
system_prompt =(
|
| 43 |
+
"You are a study program manager."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
query_engine = index.as_chat_engine(
|
| 47 |
+
similarity_top_k = 5,
|
| 48 |
+
chat_mode = "context",
|
| 49 |
+
system_prompt = system_prompt,
|
| 50 |
+
context_template = context,
|
| 51 |
+
service_context = service_context,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
default_text="Ich beantworte Fragen zum Modulhandbuch des Studiengangs Informatik und Design. Wie kann ich Dir helfen?"
|
| 55 |
+
|
| 56 |
+
bot_examples = [
|
| 57 |
+
"Wer lehrt Mobile Anwendungen?",
|
| 58 |
+
"Welche Prüfungsform hat das Modul Software Engineering?",
|
| 59 |
+
"Wie viele Semesterwochenstunden hat das Modul Computational Thinking?",
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
submit_button = gr.Button(
|
| 63 |
+
value="Ask MUC.DAI",
|
| 64 |
+
elem_classes=["ask-button"],
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
def response(message, history):
|
| 68 |
+
if message == "":
|
| 69 |
+
answer = default_text
|
| 70 |
+
else:
|
| 71 |
+
answer = str(query_engine.chat(message, chat_history=query_engine.chat_history))
|
| 72 |
+
print("message", message)
|
| 73 |
+
print("answer", answer)
|
| 74 |
+
print("history", history)
|
| 75 |
+
return answer
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def main():
|
| 79 |
+
#openai.api_key="sk-..."
|
| 80 |
+
openai.api_key = os.environ["OPENAI_API_KEY"]
|
| 81 |
+
|
| 82 |
+
custom_theme = CustomTheme()
|
| 83 |
+
|
| 84 |
+
# default_text noch einbauen
|
| 85 |
+
chatbot = gr.Chatbot(
|
| 86 |
+
avatar_images=["assets/smile.png", "assets/mucdai.png"],
|
| 87 |
+
layout='bubble',
|
| 88 |
+
height=600,
|
| 89 |
+
value=[[None, default_text]]
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
chat_interface = gr.ChatInterface(
|
| 93 |
+
fn=response,
|
| 94 |
+
retry_btn=None,
|
| 95 |
+
undo_btn=None,
|
| 96 |
+
title="MUC.DAI Informatik und Design - frag alles was Du wissen willst!",
|
| 97 |
+
submit_btn=submit_button,
|
| 98 |
+
theme=custom_theme,
|
| 99 |
+
chatbot=chatbot,
|
| 100 |
+
css="style.css",
|
| 101 |
+
examples=bot_examples,
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
chat_interface.launch(inbrowser=True, debug=True)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.10.0
|
| 2 |
+
llama_index==0.9.16
|
| 3 |
+
openai==1.5.0
|
style.css
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.gap.panel {
|
| 2 |
+
padding: 0 !important;
|
| 3 |
+
}
|
| 4 |
+
/*
|
| 5 |
+
div {
|
| 6 |
+
background-image: url("https://mediapool.hm.edu/media/mucdai/keyvisuals_1/mucdai_keyvisual_16_portrait_m.jpg") !important;
|
| 7 |
+
}
|
| 8 |
+
*/
|
| 9 |
+
|
| 10 |
+
div {
|
| 11 |
+
background-image: url("./assets/gulfer-ergin-LUGuCtvlk1Q-unsplash.jpg") !important;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
div.gap > .stretch {
|
| 15 |
+
display: none !important;
|
| 16 |
+
}
|
| 17 |
+
div.gap.panel > div.gr-group {
|
| 18 |
+
position: absolute;
|
| 19 |
+
bottom: 0;
|
| 20 |
+
}
|
| 21 |
+
h1 {
|
| 22 |
+
font-size: 24px !important;
|
| 23 |
+
}
|
| 24 |
+
.ask-button {
|
| 25 |
+
background-color: var(--color-accent);
|
| 26 |
+
font-weight: bold;
|
| 27 |
+
letter-spacing: 0.1rem;
|
| 28 |
+
}
|
| 29 |
+
.gallery-item {
|
| 30 |
+
color: var(--block-label-text-color) !important;
|
| 31 |
+
}
|
| 32 |
+
div.message-wrap {
|
| 33 |
+
margin-bottom: 32px !important;
|
| 34 |
+
}
|
theme.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from gradio.themes.soft import Soft
|
| 2 |
+
from gradio.themes.utils import fonts
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class CustomTheme(Soft):
|
| 6 |
+
|
| 7 |
+
def __init__(self):
|
| 8 |
+
super().__init__(
|
| 9 |
+
font=fonts.GoogleFont("Roboto")
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
white = "#FFFFFF"
|
| 13 |
+
purple = "#571DF9"
|
| 14 |
+
red = "#FA5558"
|
| 15 |
+
|
| 16 |
+
primary = white
|
| 17 |
+
secondary = "#e6e6e6"
|
| 18 |
+
panel_color = red
|
| 19 |
+
accent = purple
|
| 20 |
+
accent_soft = "#49637a28"
|
| 21 |
+
|
| 22 |
+
primary_dark = "#121212"
|
| 23 |
+
secondary_dark = "#242424"
|
| 24 |
+
panel_color_dark = red
|
| 25 |
+
accent_dark = purple
|
| 26 |
+
accent_soft_dark = "#101727"
|
| 27 |
+
text_color_dark = white
|
| 28 |
+
|
| 29 |
+
super().set(
|
| 30 |
+
# LIGHT MODE
|
| 31 |
+
body_background_fill=primary,
|
| 32 |
+
background_fill_secondary=primary,
|
| 33 |
+
panel_background_fill=panel_color,
|
| 34 |
+
border_color_primary=primary,
|
| 35 |
+
block_background_fill=secondary,
|
| 36 |
+
block_border_color=primary,
|
| 37 |
+
block_label_background_fill=primary,
|
| 38 |
+
input_background_fill="#DADFE6",
|
| 39 |
+
input_border_color=secondary,
|
| 40 |
+
button_secondary_background_fill=accent,
|
| 41 |
+
button_secondary_text_color=white,
|
| 42 |
+
color_accent_soft=accent_soft,
|
| 43 |
+
border_color_accent_subdued=accent,
|
| 44 |
+
|
| 45 |
+
# DARK MODE
|
| 46 |
+
body_background_fill_dark=primary_dark,
|
| 47 |
+
background_fill_secondary_dark=secondary_dark,
|
| 48 |
+
panel_background_fill_dark=secondary_dark,
|
| 49 |
+
border_color_primary_dark=primary_dark,
|
| 50 |
+
block_background_fill_dark=secondary_dark,
|
| 51 |
+
block_border_color_dark=secondary_dark,
|
| 52 |
+
block_label_background_fill_dark=primary_dark,
|
| 53 |
+
block_label_text_color_dark=text_color_dark,
|
| 54 |
+
input_background_fill_dark=panel_color_dark,
|
| 55 |
+
input_border_color_dark=secondary_dark,
|
| 56 |
+
button_primary_background_fill_dark=accent_dark,
|
| 57 |
+
button_primary_text_color_dark=primary_dark,
|
| 58 |
+
color_accent_soft_dark=accent_soft_dark,
|
| 59 |
+
border_color_accent_subdued_dark=accent_soft_dark,
|
| 60 |
+
|
| 61 |
+
block_radius="15px",
|
| 62 |
+
container_radius="32px",
|
| 63 |
+
)
|