Spaces:
Running
Running
Raghu
commited on
Commit
·
6fad358
1
Parent(s):
3716fe1
Add feedback form + monochrome theme; keep API working
Browse files
app.py
CHANGED
|
@@ -550,7 +550,7 @@ def process_receipt(image):
|
|
| 550 |
if image is None:
|
| 551 |
return (
|
| 552 |
"<div style='padding: 20px; text-align: center;'>Upload an image to begin</div>",
|
| 553 |
-
None, "", ""
|
| 554 |
)
|
| 555 |
|
| 556 |
if not isinstance(image, Image.Image):
|
|
@@ -677,7 +677,8 @@ def process_receipt(image):
|
|
| 677 |
{fields_html}
|
| 678 |
"""
|
| 679 |
|
| 680 |
-
|
|
|
|
| 681 |
|
| 682 |
|
| 683 |
# ============================================================================
|
|
@@ -685,9 +686,14 @@ def process_receipt(image):
|
|
| 685 |
# ============================================================================
|
| 686 |
|
| 687 |
CUSTOM_CSS = """
|
| 688 |
-
.gradio-container { max-width: 1200px !important; }
|
| 689 |
-
.main-header { text-align: center; padding: 20px; background: linear-gradient(135deg, #
|
| 690 |
-
border-radius: 12px; color:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 691 |
"""
|
| 692 |
|
| 693 |
with gr.Blocks(title="Receipt Processing Agent", theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
|
|
@@ -729,11 +735,61 @@ with gr.Blocks(title="Receipt Processing Agent", theme=gr.themes.Soft(), css=CUS
|
|
| 729 |
|
| 730 |
with gr.Accordion("Raw Results (JSON)", open=False):
|
| 731 |
results_json = gr.Textbox(label="Full Results", lines=15)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 732 |
|
| 733 |
process_btn.click(
|
| 734 |
fn=process_receipt,
|
| 735 |
inputs=[input_image],
|
| 736 |
-
outputs=[agent_summary, ocr_image_output, ocr_text_output, results_json]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 737 |
)
|
| 738 |
|
| 739 |
gr.Markdown("""
|
|
|
|
| 550 |
if image is None:
|
| 551 |
return (
|
| 552 |
"<div style='padding: 20px; text-align: center;'>Upload an image to begin</div>",
|
| 553 |
+
None, "", "", "<div style='padding: 40px; text-align: center; color: #6c757d;'>Upload an image to begin</div>"
|
| 554 |
)
|
| 555 |
|
| 556 |
if not isinstance(image, Image.Image):
|
|
|
|
| 677 |
{fields_html}
|
| 678 |
"""
|
| 679 |
|
| 680 |
+
safe_results = json.dumps(to_jsonable(results), indent=2)
|
| 681 |
+
return summary_html, ocr_image, ocr_text, safe_results, summary_html
|
| 682 |
|
| 683 |
|
| 684 |
# ============================================================================
|
|
|
|
| 686 |
# ============================================================================
|
| 687 |
|
| 688 |
CUSTOM_CSS = """
|
| 689 |
+
.gradio-container { max-width: 1200px !important; background: #0b0c0e; color: #e5e7eb; }
|
| 690 |
+
.main-header { text-align: center; padding: 20px; background: linear-gradient(135deg, #0f172a 0%, #1f2937 100%);
|
| 691 |
+
border-radius: 12px; color: #e5e7eb; margin-bottom: 20px; border: 1px solid #1f2937; }
|
| 692 |
+
.gr-button { border-radius: 10px; background: #111827; color: #e5e7eb; border: 1px solid #1f2937; }
|
| 693 |
+
.gr-button-primary { background: #111827; border: 1px solid #22c55e; color: #e5e7eb; }
|
| 694 |
+
.gr-box { border: 1px solid #1f2937; background: #111827; }
|
| 695 |
+
.gradio-accordion { border: 1px solid #1f2937 !important; background: #0f172a !important; }
|
| 696 |
+
.gr-markdown { color: #e5e7eb; }
|
| 697 |
"""
|
| 698 |
|
| 699 |
with gr.Blocks(title="Receipt Processing Agent", theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
|
|
|
|
| 735 |
|
| 736 |
with gr.Accordion("Raw Results (JSON)", open=False):
|
| 737 |
results_json = gr.Textbox(label="Full Results", lines=15)
|
| 738 |
+
|
| 739 |
+
with gr.Accordion("Feedback", open=True):
|
| 740 |
+
gr.Markdown("Review the agent output below and submit a quick assessment.")
|
| 741 |
+
feedback_summary = gr.HTML(label="Last Agent Response (read-only)")
|
| 742 |
+
with gr.Row():
|
| 743 |
+
feedback_assessment = gr.Radio(
|
| 744 |
+
choices=["Correct", "Incorrect"],
|
| 745 |
+
label="Is the response correct?",
|
| 746 |
+
value=None
|
| 747 |
+
)
|
| 748 |
+
feedback_notes = gr.Textbox(
|
| 749 |
+
label="Notes (optional)",
|
| 750 |
+
placeholder="What should be improved or fixed?",
|
| 751 |
+
lines=3
|
| 752 |
+
)
|
| 753 |
+
feedback_status = gr.Markdown(value="")
|
| 754 |
+
submit_feedback = gr.Button("Submit Feedback", variant="primary")
|
| 755 |
|
| 756 |
process_btn.click(
|
| 757 |
fn=process_receipt,
|
| 758 |
inputs=[input_image],
|
| 759 |
+
outputs=[agent_summary, ocr_image_output, ocr_text_output, results_json, feedback_summary]
|
| 760 |
+
)
|
| 761 |
+
|
| 762 |
+
def save_feedback(assessment, notes, results_json_str):
|
| 763 |
+
try:
|
| 764 |
+
parsed = json.loads(results_json_str) if results_json_str else {}
|
| 765 |
+
except Exception:
|
| 766 |
+
parsed = {"raw": results_json_str}
|
| 767 |
+
entry = {
|
| 768 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 769 |
+
"assessment": assessment or "",
|
| 770 |
+
"notes": notes or "",
|
| 771 |
+
"results": parsed,
|
| 772 |
+
}
|
| 773 |
+
# Append to local CSV; safe for Spaces ephemeral storage
|
| 774 |
+
import csv
|
| 775 |
+
fieldnames = ["timestamp", "assessment", "notes", "results"]
|
| 776 |
+
file_exists = os.path.exists("feedback_logs.csv")
|
| 777 |
+
with open("feedback_logs.csv", "a", newline="", encoding="utf-8") as f:
|
| 778 |
+
writer = csv.DictWriter(f, fieldnames=fieldnames)
|
| 779 |
+
if not file_exists:
|
| 780 |
+
writer.writeheader()
|
| 781 |
+
writer.writerow({
|
| 782 |
+
"timestamp": entry["timestamp"],
|
| 783 |
+
"assessment": entry["assessment"],
|
| 784 |
+
"notes": entry["notes"],
|
| 785 |
+
"results": json.dumps(entry["results"]),
|
| 786 |
+
})
|
| 787 |
+
return "✅ Feedback saved. (Stored in feedback_logs.csv)"
|
| 788 |
+
|
| 789 |
+
submit_feedback.click(
|
| 790 |
+
fn=save_feedback,
|
| 791 |
+
inputs=[feedback_assessment, feedback_notes, results_json],
|
| 792 |
+
outputs=feedback_status
|
| 793 |
)
|
| 794 |
|
| 795 |
gr.Markdown("""
|