bupa1018 commited on
Commit
5cebc05
·
1 Parent(s): a6ef685

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -34
app.py CHANGED
@@ -18,18 +18,15 @@ vectorstore_path = "data/vectorstore"
18
  GROQ_API_KEY = os.environ["GROQ_API_KEY"]
19
  HF_TOKEN = os.environ["HF_Token"]
20
 
21
-
22
- with open('config.json', 'r') as file:
23
  config = json.load(file)
24
 
25
-
26
  login(HF_TOKEN)
27
  hf_api = HfApi()
28
 
29
  # Access the values
30
- LLM_MODEL_NAME = config['llm_model_name']
31
- LLM_MODEL_TEMPERATURE = float(config['llm_model_temperature'])
32
-
33
 
34
  def initialize():
35
  global kadiAPY_bot
@@ -41,36 +38,48 @@ def initialize():
41
 
42
  initialize()
43
 
44
-
45
-
46
-
47
- def bot_kadi(history, session_state):
48
- user_query = history[-1][0]
49
-
50
- response = kadiAPY_bot.process_query({
51
- "query": user_query,
52
- "history": session_state["conversation"] # Pass full conversation history
53
- })
54
-
55
- # Update the session history
 
 
 
56
  history[-1] = (user_query, response)
57
- session_state["conversation"].append({"query": user_query, "response": response})
58
-
59
  yield history
60
 
61
- # Gradio utils with session state
 
 
 
 
 
 
 
 
 
 
62
  def main():
63
  with gr.Blocks() as demo:
64
  gr.Markdown("## KadiAPY - AI Coding-Assistant")
65
  gr.Markdown("AI assistant for KadiAPY based on RAG architecture powered by LLM")
66
-
67
- session_state = gr.State({"conversation": []})
68
 
69
  with gr.Tab("KadiAPY - AI Assistant"):
70
  with gr.Row():
71
  with gr.Column(scale=10):
72
  chatbot = gr.Chatbot([], elem_id="chatbot", label="Kadi Bot", bubble_full_width=False, show_copy_button=True, height=600)
73
  user_txt = gr.Textbox(label="Question", placeholder="Type in your question and press Enter or click Submit")
 
 
 
74
 
75
  with gr.Row():
76
  with gr.Column(scale=1):
@@ -80,7 +89,7 @@ def main():
80
 
81
  gr.Examples(
82
  examples=[
83
- "Write me a python script which can convert plain JSON to a Kadi4Mat-compatible extra metadata structure",
84
  "I need a method to upload a file to a record. The id of the record is 3",
85
  ],
86
  inputs=user_txt,
@@ -91,16 +100,9 @@ def main():
91
  examples_per_page=3,
92
  )
93
 
94
- # Bind input and button to modified bot_kadi
95
- user_txt.submit(check_input_text, user_txt, None).success(add_text, [chatbot, user_txt], [chatbot, user_txt]).then(
96
- bot_kadi, [chatbot, session_state], [chatbot]
97
- )
98
- submit_btn.click(check_input_text, user_txt, None).success(add_text, [chatbot, user_txt], [chatbot, user_txt]).then(
99
- bot_kadi, [chatbot, session_state], [chatbot]
100
- )
101
- clear_btn.click(lambda: None, None, chatbot, queue=False).then(
102
- lambda: {"conversation": []}, None, session_state, queue=False # Clear session state
103
- )
104
 
105
  demo.launch()
106
 
 
18
  GROQ_API_KEY = os.environ["GROQ_API_KEY"]
19
  HF_TOKEN = os.environ["HF_Token"]
20
 
21
+ with open("config.json", "r") as file:
 
22
  config = json.load(file)
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
 
38
 
39
  initialize()
40
 
41
+ def bot_kadi(history, state):
42
+ """
43
+ Handle user input and generate bot response using gr.State().
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
+
67
+ history = history + [(text, None)]
68
+ yield history, ""
69
+
70
  def main():
71
  with gr.Blocks() as demo:
72
  gr.Markdown("## KadiAPY - AI Coding-Assistant")
73
  gr.Markdown("AI assistant for KadiAPY based on RAG architecture powered by LLM")
 
 
74
 
75
  with gr.Tab("KadiAPY - AI Assistant"):
76
  with gr.Row():
77
  with gr.Column(scale=10):
78
  chatbot = gr.Chatbot([], elem_id="chatbot", label="Kadi Bot", bubble_full_width=False, show_copy_button=True, height=600)
79
  user_txt = gr.Textbox(label="Question", placeholder="Type in your question and press Enter or click Submit")
80
+
81
+ # Create session-specific state with gr.State
82
+ session_state = gr.State()
83
 
84
  with gr.Row():
85
  with gr.Column(scale=1):
 
89
 
90
  gr.Examples(
91
  examples=[
92
+ "Write me a python script with which can convert plain JSON to a Kadi4Mat-compatible extra metadata structure",
93
  "I need a method to upload a file to a record. The id of the record is 3",
94
  ],
95
  inputs=user_txt,
 
100
  examples_per_page=3,
101
  )
102
 
103
+ user_txt.submit(add_text, [chatbot, user_txt, session_state], [chatbot, user_txt]).then(bot_kadi, [chatbot, session_state], [chatbot])
104
+ submit_btn.click(add_text, [chatbot, user_txt, session_state], [chatbot, user_txt]).then(bot_kadi, [chatbot, session_state], [chatbot])
105
+ clear_btn.click(lambda: None, None, chatbot, queue=False)
 
 
 
 
 
 
 
106
 
107
  demo.launch()
108