bupa1018 commited on
Commit
936d603
·
verified ·
1 Parent(s): 9a381ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -24
app.py CHANGED
@@ -13,6 +13,7 @@ from kadiApy_ragchain import KadiApyRagchain
13
  load_dotenv()
14
 
15
  vectorstore_path = "data/vectorstore"
 
16
  GROQ_API_KEY = os.environ["GROQ_API_KEY"]
17
  HF_TOKEN = os.environ["HF_Token"]
18
 
@@ -20,42 +21,47 @@ with open("config.json", "r") as file:
20
  config = json.load(file)
21
 
22
  login(HF_TOKEN)
 
23
 
24
  # Access the values
25
  LLM_MODEL_NAME = config["llm_model_name"]
26
  LLM_MODEL_TEMPERATURE = float(config["llm_model_temperature"])
27
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- # A class to encapsulate the bot logic
30
- class KadiBot:
31
- def __init__(self, hf_token: str, groq_api_key: str, config: dict, vectorstore_path: str):
32
- self.vectorstore = get_chroma_vectorstore(get_SFR_Code_embedding_model(), vectorstore_path)
33
- self.llm = get_groq_llm(config["llm_model_name"], float(config["llm_model_temperature"]), groq_api_key)
34
- self.kadiAPY_ragchain = KadiApyRagchain(self.llm, self.vectorstore)
35
 
36
- def process_query(self, user_query, chat_history):
37
- response = self.kadiAPY_ragchain.process_query(user_query, chat_history)
38
- chat_history[-1] = (user_query, response)
39
- return chat_history
40
 
 
 
 
 
41
 
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
-
47
  def show_history(chat_history):
48
  return chat_history
49
-
50
-
51
  def reset_all():
52
  return [], "", ""
53
-
54
-
55
  def main():
56
- # Initialize the KadiBot
57
- kadi_bot = KadiBot(HF_TOKEN, GROQ_API_KEY, config, vectorstore_path)
58
-
59
  with gr.Blocks() as demo:
60
  gr.Markdown("## KadiAPY - AI Coding-Assistant")
61
  gr.Markdown("AI assistant for KadiAPY based on RAG architecture powered by LLM")
@@ -87,12 +93,12 @@ def main():
87
  cache_examples=False,
88
  examples_per_page=3,
89
  )
90
-
91
  # Use the state to persist chat history between interactions
92
- user_txt.submit(add_text_to_chat_history, [chat_history, user_txt], [chat_history, user_txt]).then(show_history, [chat_history], [chatbot])\
93
- .then(kadi_bot.process_query, [chat_history], [chatbot])
94
- submit_btn.click(add_text_to_chat_history, [chat_history, user_txt], [chat_history, user_txt]).then(show_history, [chat_history], [chatbot])\
95
- .then(kadi_bot.process_query, [chat_history], [chatbot])
96
  clear_btn.click(
97
  reset_all,
98
  None,
@@ -101,6 +107,5 @@ def main():
101
  )
102
  demo.launch()
103
 
104
-
105
  if __name__ == "__main__":
106
  main()
 
13
  load_dotenv()
14
 
15
  vectorstore_path = "data/vectorstore"
16
+
17
  GROQ_API_KEY = os.environ["GROQ_API_KEY"]
18
  HF_TOKEN = os.environ["HF_Token"]
19
 
 
21
  config = json.load(file)
22
 
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
+ kadiAPY_ragchain = KadiApyRagchain(llm, vectorstore)
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
+ import gradio as gr
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")
 
93
  cache_examples=False,
94
  examples_per_page=3,
95
  )
96
+
97
  # Use the state to persist chat history between interactions
98
+ user_txt.submit(add_text_to_chat_history, [chat_history, user_txt], [chat_history, user_txt]).then(show_history,[chat_history], [chatbot])\
99
+ .then(bot_kadi, [chat_history], [chatbot])
100
+ submit_btn.click(add_text_to_chat_history, [chat_history, user_txt], [chat_history, user_txt]).then(show_history,[chat_history], [chatbot])\
101
+ .then(bot_kadi, [chat_history], [chatbot])
102
  clear_btn.click(
103
  reset_all,
104
  None,
 
107
  )
108
  demo.launch()
109
 
 
110
  if __name__ == "__main__":
111
  main()