HemanM commited on
Commit
1dec93e
Β·
verified Β·
1 Parent(s): c1de334

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -21
app.py CHANGED
@@ -1,35 +1,54 @@
1
  import gradio as gr
2
- from inference import evo_chat_predict
 
3
  import subprocess
4
 
5
- # Global chat history buffer
6
  chat_history = []
7
 
8
- # 🧠 Main chat handler
9
- def chat_fn(user_input, option1, option2):
10
  global chat_history
11
 
12
- # Validate input
13
  if not user_input or not option1 or not option2:
14
- return "Please enter a message and both options.", chat_history
15
 
16
  options = [option1.strip(), option2.strip()]
17
- result = evo_chat_predict(chat_history, user_input, options)
18
 
19
- # Format Evo reply
20
- evo_response = f"**Answer:** {result['answer']} \n**Reasoning:** {result['reasoning']}"
21
- chat_history.append(f"User: {user_input}")
22
- chat_history.append(f"Evo: {evo_response}")
23
 
24
- return evo_response, chat_history
 
25
 
26
- # πŸ” Reset chat history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  def clear_fn():
28
  global chat_history
29
  chat_history = []
30
- return "", "", "", []
31
 
32
- # πŸ” Trigger Evo retraining
33
  def retrain_model():
34
  try:
35
  subprocess.run(["python", "retrain_from_feedback.py"], check=True)
@@ -37,16 +56,17 @@ def retrain_model():
37
  except Exception as e:
38
  return f"❌ Retraining failed: {str(e)}"
39
 
40
- # 🧠 Gradio UI layout
41
  with gr.Blocks(title="EvoRAG – Real-Time Adaptive Reasoning AI") as demo:
42
- gr.Markdown("## 🧬 EvoRAG – The Evolving Reasoning AI")
43
- gr.Markdown("Ask a question, give two options, and Evo will decide with confidence. Then, retrain it live.")
44
 
45
  with gr.Row():
46
  with gr.Column(scale=4):
47
  user_input = gr.Textbox(label="Your Question", lines=2)
48
  option1 = gr.Textbox(label="Option 1")
49
  option2 = gr.Textbox(label="Option 2")
 
50
  submit = gr.Button("🧠 Ask Evo")
51
  clear = gr.Button("πŸ” Clear")
52
  retrain = gr.Button("πŸ“ˆ Retrain Evo from Feedback")
@@ -55,9 +75,9 @@ with gr.Blocks(title="EvoRAG – Real-Time Adaptive Reasoning AI") as demo:
55
  evo_reply = gr.Markdown()
56
  chat_display = gr.HighlightedText(label="Conversation History")
57
 
58
- submit.click(fn=chat_fn, inputs=[user_input, option1, option2],
59
  outputs=[evo_reply, chat_display])
60
- clear.click(fn=clear_fn, inputs=[], outputs=[user_input, option1, option2, chat_display])
61
- retrain.click(fn=retrain_model, inputs=[], outputs=evo_reply)
62
 
63
  demo.launch()
 
1
  import gradio as gr
2
+ from inference import evo_chat_predict, get_gpt_response
3
+ from logger import log_feedback
4
  import subprocess
5
 
6
+ # Global chat history
7
  chat_history = []
8
 
9
+ # 🧠 Handle main chat logic
10
+ def chat_fn(user_input, option1, option2, user_vote=None):
11
  global chat_history
12
 
13
+ # Validate
14
  if not user_input or not option1 or not option2:
15
+ return "❗ Please enter your question and both options.", chat_history
16
 
17
  options = [option1.strip(), option2.strip()]
 
18
 
19
+ # Evo prediction
20
+ evo_result = evo_chat_predict(chat_history, user_input, options)
 
 
21
 
22
+ # GPT fallback (background comparison)
23
+ gpt_response = get_gpt_response(user_input)
24
 
25
+ # Format response
26
+ evo_msg = f"**Answer:** {evo_result['answer']} \n**Reasoning:** {evo_result['reasoning']}"
27
+ chat_history.append(f"πŸ‘€ User: {user_input}")
28
+ chat_history.append(f"πŸ€– Evo: {evo_msg}")
29
+ chat_history.append(f"🧠 GPT: {gpt_response}")
30
+
31
+ # Logging for Evo retraining
32
+ log_feedback(
33
+ question=user_input,
34
+ option1=option1,
35
+ option2=option2,
36
+ context=evo_result['context'],
37
+ evo_output=evo_result['answer'],
38
+ gpt_output=gpt_response,
39
+ evo_reasoning=evo_result['reasoning'],
40
+ user_preference=user_vote
41
+ )
42
+
43
+ return evo_msg, chat_history
44
+
45
+ # πŸ” Clear chat state
46
  def clear_fn():
47
  global chat_history
48
  chat_history = []
49
+ return "", "", "", None, []
50
 
51
+ # πŸ“ˆ Live retrain
52
  def retrain_model():
53
  try:
54
  subprocess.run(["python", "retrain_from_feedback.py"], check=True)
 
56
  except Exception as e:
57
  return f"❌ Retraining failed: {str(e)}"
58
 
59
+ # 🌐 Gradio UI
60
  with gr.Blocks(title="EvoRAG – Real-Time Adaptive Reasoning AI") as demo:
61
+ gr.Markdown("## 🧬 EvoRAG – Real-Time Adaptive Reasoning AI")
62
+ gr.Markdown("Ask Evo a question, give two options. Evo chooses with reasoning. Compare with GPT. Feedback fuels evolution.")
63
 
64
  with gr.Row():
65
  with gr.Column(scale=4):
66
  user_input = gr.Textbox(label="Your Question", lines=2)
67
  option1 = gr.Textbox(label="Option 1")
68
  option2 = gr.Textbox(label="Option 2")
69
+ user_vote = gr.Radio(["Evo", "GPT"], label="πŸ—³οΈ Who gave the better answer?", info="Optional – improves Evo.")
70
  submit = gr.Button("🧠 Ask Evo")
71
  clear = gr.Button("πŸ” Clear")
72
  retrain = gr.Button("πŸ“ˆ Retrain Evo from Feedback")
 
75
  evo_reply = gr.Markdown()
76
  chat_display = gr.HighlightedText(label="Conversation History")
77
 
78
+ submit.click(fn=chat_fn, inputs=[user_input, option1, option2, user_vote],
79
  outputs=[evo_reply, chat_display])
80
+ clear.click(fn=clear_fn, outputs=[user_input, option1, option2, user_vote, chat_display])
81
+ retrain.click(fn=retrain_model, outputs=evo_reply)
82
 
83
  demo.launch()