Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,50 +23,39 @@ with open("config.json", "r") as file:
|
|
| 23 |
login(HF_TOKEN)
|
| 24 |
hf_api = HfApi()
|
| 25 |
|
| 26 |
-
# Access the values
|
| 27 |
LLM_MODEL_NAME = config["llm_model_name"]
|
| 28 |
LLM_MODEL_TEMPERATURE = float(config["llm_model_temperature"])
|
| 29 |
|
| 30 |
def initialize():
|
| 31 |
-
global kadiAPY_ragchain
|
| 32 |
-
|
| 33 |
vectorstore = get_chroma_vectorstore(get_SFR_Code_embedding_model(), vectorstore_path)
|
| 34 |
llm = get_groq_llm(LLM_MODEL_NAME, LLM_MODEL_TEMPERATURE, GROQ_API_KEY)
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
initialize()
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
def bot_kadi(chat_history):
|
| 43 |
user_query = chat_history[-1][0]
|
| 44 |
response = kadiAPY_ragchain.process_query(user_query, chat_history)
|
| 45 |
chat_history[-1] = (user_query, response)
|
| 46 |
-
|
| 47 |
return chat_history
|
| 48 |
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
def add_text_to_chat_history(chat_history, user_input):
|
| 53 |
-
|
| 54 |
chat_history = chat_history + [(user_input, None)]
|
| 55 |
return chat_history, ""
|
| 56 |
|
| 57 |
-
|
| 58 |
def show_history(chat_history):
|
| 59 |
return chat_history
|
| 60 |
-
|
| 61 |
def reset_all():
|
| 62 |
return [], "", ""
|
| 63 |
-
|
| 64 |
def main():
|
|
|
|
|
|
|
| 65 |
with gr.Blocks() as demo:
|
| 66 |
gr.Markdown("## KadiAPY - AI Coding-Assistant")
|
| 67 |
gr.Markdown("AI assistant for KadiAPY based on RAG architecture powered by LLM")
|
| 68 |
|
| 69 |
-
# Create a state for session management
|
| 70 |
chat_history = gr.State([])
|
| 71 |
|
| 72 |
with gr.Tab("KadiAPY - AI Assistant"):
|
|
@@ -74,13 +63,13 @@ def main():
|
|
| 74 |
with gr.Column(scale=10):
|
| 75 |
chatbot = gr.Chatbot([], elem_id="chatbot", label="Kadi Bot", bubble_full_width=False, show_copy_button=True, height=600)
|
| 76 |
user_txt = gr.Textbox(label="Question", placeholder="Type in your question and press Enter or click Submit")
|
| 77 |
-
|
| 78 |
with gr.Row():
|
| 79 |
with gr.Column(scale=1):
|
| 80 |
submit_btn = gr.Button("Submit", variant="primary")
|
| 81 |
with gr.Column(scale=1):
|
| 82 |
clear_btn = gr.Button("Clear", variant="stop")
|
| 83 |
-
|
| 84 |
gr.Examples(
|
| 85 |
examples=[
|
| 86 |
"Write me a python script with which can convert plain JSON to a Kadi4Mat-compatible extra metadata structure",
|
|
@@ -93,18 +82,22 @@ def main():
|
|
| 93 |
cache_examples=False,
|
| 94 |
examples_per_page=3,
|
| 95 |
)
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
.then(bot_kadi, [chat_history], [chatbot])
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
| 102 |
clear_btn.click(
|
| 103 |
reset_all,
|
| 104 |
None,
|
| 105 |
[chat_history, chatbot, user_txt],
|
| 106 |
queue=False
|
| 107 |
)
|
|
|
|
| 108 |
demo.launch()
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
|
|
|
| 23 |
login(HF_TOKEN)
|
| 24 |
hf_api = HfApi()
|
| 25 |
|
|
|
|
| 26 |
LLM_MODEL_NAME = config["llm_model_name"]
|
| 27 |
LLM_MODEL_TEMPERATURE = float(config["llm_model_temperature"])
|
| 28 |
|
| 29 |
def initialize():
|
|
|
|
|
|
|
| 30 |
vectorstore = get_chroma_vectorstore(get_SFR_Code_embedding_model(), vectorstore_path)
|
| 31 |
llm = get_groq_llm(LLM_MODEL_NAME, LLM_MODEL_TEMPERATURE, GROQ_API_KEY)
|
| 32 |
+
return KadiApyRagchain(llm, vectorstore)
|
| 33 |
|
| 34 |
+
def bot_kadi(chat_history, kadiAPY_ragchain):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
user_query = chat_history[-1][0]
|
| 36 |
response = kadiAPY_ragchain.process_query(user_query, chat_history)
|
| 37 |
chat_history[-1] = (user_query, response)
|
|
|
|
| 38 |
return chat_history
|
| 39 |
|
| 40 |
|
| 41 |
+
#gradio utils
|
|
|
|
| 42 |
def add_text_to_chat_history(chat_history, user_input):
|
|
|
|
| 43 |
chat_history = chat_history + [(user_input, None)]
|
| 44 |
return chat_history, ""
|
| 45 |
|
|
|
|
| 46 |
def show_history(chat_history):
|
| 47 |
return chat_history
|
| 48 |
+
|
| 49 |
def reset_all():
|
| 50 |
return [], "", ""
|
| 51 |
+
|
| 52 |
def main():
|
| 53 |
+
kadiAPY_ragchain = initialize() # Initialize inside main()
|
| 54 |
+
|
| 55 |
with gr.Blocks() as demo:
|
| 56 |
gr.Markdown("## KadiAPY - AI Coding-Assistant")
|
| 57 |
gr.Markdown("AI assistant for KadiAPY based on RAG architecture powered by LLM")
|
| 58 |
|
|
|
|
| 59 |
chat_history = gr.State([])
|
| 60 |
|
| 61 |
with gr.Tab("KadiAPY - AI Assistant"):
|
|
|
|
| 63 |
with gr.Column(scale=10):
|
| 64 |
chatbot = gr.Chatbot([], elem_id="chatbot", label="Kadi Bot", bubble_full_width=False, show_copy_button=True, height=600)
|
| 65 |
user_txt = gr.Textbox(label="Question", placeholder="Type in your question and press Enter or click Submit")
|
| 66 |
+
|
| 67 |
with gr.Row():
|
| 68 |
with gr.Column(scale=1):
|
| 69 |
submit_btn = gr.Button("Submit", variant="primary")
|
| 70 |
with gr.Column(scale=1):
|
| 71 |
clear_btn = gr.Button("Clear", variant="stop")
|
| 72 |
+
|
| 73 |
gr.Examples(
|
| 74 |
examples=[
|
| 75 |
"Write me a python script with which can convert plain JSON to a Kadi4Mat-compatible extra metadata structure",
|
|
|
|
| 82 |
cache_examples=False,
|
| 83 |
examples_per_page=3,
|
| 84 |
)
|
| 85 |
+
|
| 86 |
+
user_txt.submit(add_text_to_chat_history, [chat_history, user_txt], [chat_history, user_txt])\
|
| 87 |
+
.then(show_history, [chat_history], [chatbot])\
|
| 88 |
+
.then(bot_kadi, [chat_history, kadiAPY_ragchain], [chatbot])
|
| 89 |
+
|
| 90 |
+
submit_btn.click(add_text_to_chat_history, [chat_history, user_txt], [chat_history, user_txt])\
|
| 91 |
+
.then(show_history, [chat_history], [chatbot])\
|
| 92 |
+
.then(bot_kadi, [chat_history, kadiAPY_ragchain], [chatbot])
|
| 93 |
+
|
| 94 |
clear_btn.click(
|
| 95 |
reset_all,
|
| 96 |
None,
|
| 97 |
[chat_history, chatbot, user_txt],
|
| 98 |
queue=False
|
| 99 |
)
|
| 100 |
+
|
| 101 |
demo.launch()
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|