Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,76 +5,100 @@ import streamlit as st
|
|
| 5 |
from huggingface_hub import HfApi, login
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from llm import get_groq_llm
|
| 9 |
from vectorstore import get_chroma_vectorstore
|
| 10 |
from embeddings import get_SFR_Code_embedding_model
|
| 11 |
from kadi_apy_bot import KadiAPYBot
|
|
|
|
| 12 |
|
| 13 |
# Load environment variables from .env file
|
| 14 |
load_dotenv()
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
GROQ_API_KEY = os.environ["GROQ_API_KEY"]
|
| 19 |
HF_TOKEN = os.environ["HF_Token"]
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
login(HF_TOKEN)
|
| 25 |
hf_api = HfApi()
|
| 26 |
|
| 27 |
-
# Access the values
|
| 28 |
-
LLM_MODEL_NAME = config["llm_model_name"]
|
| 29 |
-
LLM_MODEL_TEMPERATURE = float(config["llm_model_temperature"])
|
| 30 |
|
| 31 |
def initialize():
|
| 32 |
global kadiAPY_bot
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
llm = get_groq_llm(LLM_MODEL_NAME, LLM_MODEL_TEMPERATURE, GROQ_API_KEY)
|
| 36 |
|
| 37 |
kadiAPY_bot = KadiAPYBot(llm, vectorstore)
|
| 38 |
|
| 39 |
initialize()
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
user_query = history[-1][0]
|
| 46 |
-
|
| 47 |
-
# Add user query to the bot's state for session-specific history
|
| 48 |
-
state["history"].append({"query": user_query, "response": None})
|
| 49 |
-
|
| 50 |
-
# Process the query with the bot and generate a response
|
| 51 |
response = kadiAPY_bot.process_query(user_query)
|
| 52 |
-
|
| 53 |
-
# Save the response back to session state
|
| 54 |
-
state["history"][-1]["response"] = response
|
| 55 |
-
|
| 56 |
history[-1] = (user_query, response)
|
| 57 |
-
yield history
|
| 58 |
-
|
| 59 |
-
# Gradio UI
|
| 60 |
-
def add_text(history, text, state):
|
| 61 |
-
"""
|
| 62 |
-
Add user text to history and initialize state if needed.
|
| 63 |
-
"""
|
| 64 |
-
if "history" not in state:
|
| 65 |
-
state["history"] = [] # Initialize session-specific state
|
| 66 |
-
if history is None or len(history) == 0:
|
| 67 |
-
history = [] # Initialize empty history list
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
|
|
|
|
|
|
|
| 72 |
def check_input_text(text):
|
| 73 |
if not text:
|
| 74 |
gr.Warning("Please input a question.")
|
| 75 |
raise TypeError
|
| 76 |
return True
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
def main():
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
gr.Markdown("## KadiAPY - AI Coding-Assistant")
|
|
@@ -85,9 +109,6 @@ def main():
|
|
| 85 |
with gr.Column(scale=10):
|
| 86 |
chatbot = gr.Chatbot([], elem_id="chatbot", label="Kadi Bot", bubble_full_width=False, show_copy_button=True, height=600)
|
| 87 |
user_txt = gr.Textbox(label="Question", placeholder="Type in your question and press Enter or click Submit")
|
| 88 |
-
|
| 89 |
-
# Create session-specific state with gr.State
|
| 90 |
-
session_state = gr.State()
|
| 91 |
|
| 92 |
with gr.Row():
|
| 93 |
with gr.Column(scale=1):
|
|
@@ -108,14 +129,12 @@ def main():
|
|
| 108 |
examples_per_page=3,
|
| 109 |
)
|
| 110 |
|
| 111 |
-
user_txt.submit(check_input_text, user_txt, None)
|
| 112 |
-
.success(add_text, [chatbot, user_txt,
|
| 113 |
-
.then(bot_kadi, [chatbot, session_state], [chatbot])
|
| 114 |
-
submit_btn.click(check_input_text, user_txt, None)
|
| 115 |
-
.success(add_text, [chatbot, user_txt, session_state], [chatbot, user_txt])
|
| 116 |
-
.then(bot_kadi, [chatbot, session_state], [chatbot])
|
| 117 |
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
| 118 |
-
demo.launch()
|
| 119 |
|
|
|
|
|
|
|
|
|
|
| 120 |
if __name__ == "__main__":
|
| 121 |
main()
|
|
|
|
| 5 |
from huggingface_hub import HfApi, login
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
| 8 |
+
from download_repo import download_gitlab_repo_to_hfspace
|
| 9 |
+
from process_repo import extract_repo_files
|
| 10 |
+
from chunking import chunk_pythoncode_and_add_metadata, chunk_text_and_add_metadata
|
| 11 |
+
from vectorstore import setup_vectorstore
|
| 12 |
from llm import get_groq_llm
|
| 13 |
from vectorstore import get_chroma_vectorstore
|
| 14 |
from embeddings import get_SFR_Code_embedding_model
|
| 15 |
from kadi_apy_bot import KadiAPYBot
|
| 16 |
+
from repo_versions import store_message_from_json
|
| 17 |
|
| 18 |
# Load environment variables from .env file
|
| 19 |
load_dotenv()
|
| 20 |
|
| 21 |
+
# Load configuration from JSON file
|
| 22 |
+
|
| 23 |
+
with open("config.json", "r") as file:
|
| 24 |
+
config = json.load(file)
|
| 25 |
|
| 26 |
GROQ_API_KEY = os.environ["GROQ_API_KEY"]
|
| 27 |
HF_TOKEN = os.environ["HF_Token"]
|
| 28 |
|
| 29 |
+
|
| 30 |
+
VECTORSTORE_DIRECTORY = config["vectorstore_directory"]
|
| 31 |
+
CHUNK_SIZE = config["chunking"]["chunk_size"]
|
| 32 |
+
CHUNK_OVERLAP = config["chunking"]["chunk_overlap"]
|
| 33 |
+
|
| 34 |
+
EMBEDDING_MODEL_NAME = config["embedding_model"]["name"]
|
| 35 |
+
EMBEDDING_MODEL_VERSION = config["embedding_model"]["version"]
|
| 36 |
+
|
| 37 |
+
LLM_MODEL_NAME = config["llm_model"]["name"]
|
| 38 |
+
LLM_MODEL_TEMPERATURE = config["llm_model"]["temperature"]
|
| 39 |
+
|
| 40 |
+
GITLAB_API_URL = config["gitlab"]["api_url"]
|
| 41 |
+
GITLAB_PROJECT_ID = config["gitlab"]["project id"]
|
| 42 |
+
GITLAB_PROJECT_VERSION = config["gitlab"]["project version"]
|
| 43 |
+
|
| 44 |
+
DATA_DIR = config["data_dir"]
|
| 45 |
+
HF_SPACE_NAME = config["hf_space_name"]
|
| 46 |
|
| 47 |
login(HF_TOKEN)
|
| 48 |
hf_api = HfApi()
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
def initialize():
|
| 52 |
global kadiAPY_bot
|
| 53 |
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# download_gitlab_repo_to_hfspace(GITLAB_API_URL, GITLAB_PROJECT_ID, GITLAB_PROJECT_VERSION, DATA_DIR, hf_api, HF_SPACE_NAME)
|
| 57 |
+
|
| 58 |
+
# code_texts, code_references = extract_repo_files(DATA_DIR, ['kadi_apy'], [])
|
| 59 |
+
# doc_texts, doc_references = extract_repo_files(DATA_DIR, ['docs'], [])
|
| 60 |
+
|
| 61 |
+
# print("Length of code_texts: ", len(code_texts))
|
| 62 |
+
# print("Length of doc_files: ", len(doc_texts))
|
| 63 |
+
|
| 64 |
+
# code_chunks = chunk_pythoncode_and_add_metadata(code_texts, code_references)
|
| 65 |
+
# doc_chunks = chunk_text_and_add_metadata(doc_texts, doc_references, CHUNK_SIZE, CHUNK_OVERLAP)
|
| 66 |
+
|
| 67 |
+
# print(f"Total number of code_chunks: {len(code_chunks)}")
|
| 68 |
+
# print(f"Total number of doc_chunks: {len(doc_chunks)}")
|
| 69 |
+
|
| 70 |
+
vectorstore = get_chroma_vectorstore(get_SFR_Code_embedding_model(), "data/vectorstore")
|
| 71 |
llm = get_groq_llm(LLM_MODEL_NAME, LLM_MODEL_TEMPERATURE, GROQ_API_KEY)
|
| 72 |
|
| 73 |
kadiAPY_bot = KadiAPYBot(llm, vectorstore)
|
| 74 |
|
| 75 |
initialize()
|
| 76 |
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def bot_kadi(history):
|
| 80 |
+
user_query = history[-1][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
response = kadiAPY_bot.process_query(user_query)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
history[-1] = (user_query, response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
yield history
|
| 85 |
+
|
| 86 |
|
| 87 |
+
|
| 88 |
+
# Gradio utils
|
| 89 |
def check_input_text(text):
|
| 90 |
if not text:
|
| 91 |
gr.Warning("Please input a question.")
|
| 92 |
raise TypeError
|
| 93 |
return True
|
| 94 |
+
|
| 95 |
+
def add_text(history, text):
|
| 96 |
+
history = history + [(text, None)]
|
| 97 |
+
yield history, ""
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
import gradio as gr
|
| 101 |
+
|
| 102 |
def main():
|
| 103 |
with gr.Blocks() as demo:
|
| 104 |
gr.Markdown("## KadiAPY - AI Coding-Assistant")
|
|
|
|
| 109 |
with gr.Column(scale=10):
|
| 110 |
chatbot = gr.Chatbot([], elem_id="chatbot", label="Kadi Bot", bubble_full_width=False, show_copy_button=True, height=600)
|
| 111 |
user_txt = gr.Textbox(label="Question", placeholder="Type in your question and press Enter or click Submit")
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
with gr.Row():
|
| 114 |
with gr.Column(scale=1):
|
|
|
|
| 129 |
examples_per_page=3,
|
| 130 |
)
|
| 131 |
|
| 132 |
+
user_txt.submit(check_input_text, user_txt, None).success(add_text, [chatbot, user_txt], [chatbot, user_txt]).then(bot_kadi, [chatbot], [chatbot])
|
| 133 |
+
submit_btn.click(check_input_text, user_txt, None).success(add_text, [chatbot, user_txt], [chatbot, user_txt]).then(bot_kadi, [chatbot], [chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
|
|
|
| 135 |
|
| 136 |
+
demo.launch()
|
| 137 |
+
|
| 138 |
+
|
| 139 |
if __name__ == "__main__":
|
| 140 |
main()
|