Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
6 |
chat_history = []
|
7 |
|
8 |
-
# π§
|
9 |
-
def chat_fn(user_input, option1, option2):
|
10 |
global chat_history
|
11 |
|
12 |
-
# Validate
|
13 |
if not user_input or not option1 or not option2:
|
14 |
-
return "Please enter
|
15 |
|
16 |
options = [option1.strip(), option2.strip()]
|
17 |
-
result = evo_chat_predict(chat_history, user_input, options)
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
chat_history.append(f"User: {user_input}")
|
22 |
-
chat_history.append(f"Evo: {evo_response}")
|
23 |
|
24 |
-
|
|
|
25 |
|
26 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
def clear_fn():
|
28 |
global chat_history
|
29 |
chat_history = []
|
30 |
-
return "", "", "", []
|
31 |
|
32 |
-
#
|
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 |
-
#
|
41 |
with gr.Blocks(title="EvoRAG β Real-Time Adaptive Reasoning AI") as demo:
|
42 |
-
gr.Markdown("## 𧬠EvoRAG β
|
43 |
-
gr.Markdown("Ask a question, give two options
|
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,
|
61 |
-
retrain.click(fn=retrain_model,
|
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()
|