Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,98 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import pandas as pd
|
| 4 |
from inference import get_evo_response, get_gpt_response
|
|
|
|
|
|
|
| 5 |
|
| 6 |
LOG_PATH = "feedback_log.csv"
|
| 7 |
-
if
|
| 8 |
-
os.makedirs(os.path.dirname(LOG_PATH), exist_ok=True)
|
| 9 |
-
|
| 10 |
-
def log_feedback(question, context, evo_answer, feedback):
|
| 11 |
-
df = pd.DataFrame([[question, context, evo_answer, feedback]],
|
| 12 |
-
columns=["question", "context", "evo_answer", "feedback"])
|
| 13 |
-
if os.path.exists(LOG_PATH):
|
| 14 |
-
df.to_csv(LOG_PATH, mode="a", header=False, index=False)
|
| 15 |
-
else:
|
| 16 |
-
df.to_csv(LOG_PATH, index=False)
|
| 17 |
-
|
| 18 |
-
def load_history():
|
| 19 |
if not os.path.exists(LOG_PATH):
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
def
|
| 25 |
options = [opt.strip() for opt in options_input.strip().split("\n") if opt.strip()]
|
| 26 |
if len(options) != 2:
|
| 27 |
-
return "Please enter exactly two options (one per line).", "", ""
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
return (
|
| 33 |
-
f"
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
history
|
| 36 |
)
|
| 37 |
|
| 38 |
-
def
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
with gr.Blocks(title="EvoRAG β General-Purpose Adaptive AI with Web Reasoning") as demo:
|
| 43 |
gr.Markdown("## π§ EvoRAG β General-Purpose Adaptive AI with Web Reasoning")
|
|
|
|
| 44 |
|
| 45 |
with gr.Row():
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
options = gr.Textbox(label="π§ Options (2 lines)", lines=2, placeholder="Option 1\nOption 2")
|
| 50 |
-
submit = gr.Button("π Run Advisors")
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
feedback_status = gr.Textbox(interactive=False)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from inference import get_evo_response, get_gpt_response
|
| 3 |
+
import csv
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
LOG_PATH = "feedback_log.csv"
|
| 7 |
+
if LOG_PATH:
|
| 8 |
+
os.makedirs(os.path.dirname(LOG_PATH) or ".", exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
if not os.path.exists(LOG_PATH):
|
| 10 |
+
with open(LOG_PATH, "w", newline="") as f:
|
| 11 |
+
writer = csv.writer(f)
|
| 12 |
+
writer.writerow(["question", "context", "evo_answer", "gpt_answer", "feedback"])
|
| 13 |
+
|
| 14 |
+
history = []
|
| 15 |
|
| 16 |
+
def run_advisors(query, options_input, context_input):
|
| 17 |
options = [opt.strip() for opt in options_input.strip().split("\n") if opt.strip()]
|
| 18 |
if len(options) != 2:
|
| 19 |
+
return "Please enter exactly two options (one per line).", "", "", "", "", history
|
| 20 |
|
| 21 |
+
evo_ans, reasoning, confidence, evo_context = get_evo_response(query, options, context_input)
|
| 22 |
+
gpt_ans = get_gpt_response(query, context_input)
|
| 23 |
+
|
| 24 |
+
entry = {
|
| 25 |
+
"Question": query,
|
| 26 |
+
"Context": context_input,
|
| 27 |
+
"Options": options,
|
| 28 |
+
"Evo Answer": evo_ans,
|
| 29 |
+
"Evo Score": reasoning,
|
| 30 |
+
"Confidence": f"{confidence:.2f}",
|
| 31 |
+
"Context Used": evo_context,
|
| 32 |
+
"GPT Answer": gpt_ans
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
history.insert(0, entry)
|
| 36 |
return (
|
| 37 |
+
f"β
Answer: **{evo_ans}** (Confidence: {confidence:.2f})",
|
| 38 |
+
reasoning,
|
| 39 |
+
evo_context,
|
| 40 |
+
gpt_ans,
|
| 41 |
+
"", # feedback placeholder
|
| 42 |
history
|
| 43 |
)
|
| 44 |
|
| 45 |
+
def log_feedback(feedback, question, context, evo_answer, gpt_answer):
|
| 46 |
+
with open(LOG_PATH, "a", newline="") as f:
|
| 47 |
+
writer = csv.writer(f)
|
| 48 |
+
writer.writerow([question, context, evo_answer, gpt_answer, feedback])
|
| 49 |
+
return "β
Feedback submitted."
|
| 50 |
+
|
| 51 |
+
def render_history(history):
|
| 52 |
+
if not history:
|
| 53 |
+
return "No previous questions."
|
| 54 |
+
html = ""
|
| 55 |
+
for h in history[:5]:
|
| 56 |
+
html += f"<b>Q:</b> {h['Question']}<br>"
|
| 57 |
+
html += f"<b>Options:</b> {', '.join(h['Options'])}<br>"
|
| 58 |
+
html += f"<b>Evo:</b> {h['Evo Answer']} ({h['Evo Score']})<br>"
|
| 59 |
+
html += f"<b>GPT:</b> {h['GPT Answer']}<br><hr>"
|
| 60 |
+
return html
|
| 61 |
|
| 62 |
+
with gr.Blocks(title="π§ EvoRAG β General-Purpose Adaptive AI with Web Reasoning") as demo:
|
| 63 |
gr.Markdown("## π§ EvoRAG β General-Purpose Adaptive AI with Web Reasoning")
|
| 64 |
+
gr.Markdown("Ask any question and compare Evoβs evolved reasoning with GPT-3.5. Evo uses online context + live evolution.")
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
+
query = gr.Textbox(label="π Ask anything", placeholder="e.g. Who is the president of France?")
|
| 68 |
+
options_input = gr.Textbox(lines=2, label="π§ Options (2 lines)", placeholder="Option 1\nOption 2")
|
| 69 |
+
context_input = gr.Textbox(lines=2, label="π Optional Context or Notes", placeholder="Paste extra info or leave blank")
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
run_btn = gr.Button("π Run Advisors")
|
| 72 |
+
|
| 73 |
+
evo_output = gr.Markdown()
|
| 74 |
+
reasoning_output = gr.Textbox(label="π Evo Reasoning")
|
| 75 |
+
evo_context = gr.Textbox(label="π Web Context Used", lines=4)
|
| 76 |
+
|
| 77 |
+
gpt_output = gr.Textbox(label="π€ GPT-3.5 Suggestion")
|
| 78 |
|
| 79 |
with gr.Row():
|
| 80 |
+
feedback_radio = gr.Radio(["π Helpful", "π Not Helpful"], label="Was Evoβs answer useful?")
|
| 81 |
+
submit_btn = gr.Button("π¬ Submit Feedback")
|
|
|
|
| 82 |
|
| 83 |
+
feedback_status = gr.Markdown()
|
| 84 |
+
history_display = gr.HTML(label="π Recent History")
|
| 85 |
|
| 86 |
+
run_btn.click(
|
| 87 |
+
run_advisors,
|
| 88 |
+
inputs=[query, options_input, context_input],
|
| 89 |
+
outputs=[evo_output, reasoning_output, evo_context, gpt_output, feedback_radio, history_display]
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
submit_btn.click(
|
| 93 |
+
log_feedback,
|
| 94 |
+
inputs=[feedback_radio, query, context_input, evo_output, gpt_output],
|
| 95 |
+
outputs=feedback_status
|
| 96 |
+
)
|
| 97 |
|
| 98 |
demo.launch()
|