File size: 6,321 Bytes
a176aa6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import gradio as gr
import json
from Database.mongodb import DatabaseManager
import traceback

def create_gradio_interface():

    db_manager = DatabaseManager()

    def safe_execute(func, *args, **kwargs):
        """
        Wrapper to handle exceptions gracefully
        """
        try:
            return func(*args, **kwargs)
        except Exception as e:
            return f"Error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
    # Brand Record Methods

    def get_brand_records(filter_brand='', sort_by='timestamp', ascending=False):
        filter_criteria = {}
        if filter_brand:
            filter_criteria['brand'] = {'$regex': filter_brand, '$options': 'i'}
        
        return safe_execute(db_manager.get_brand_records, filter_criteria, sort_by, ascending)
    
    def get_freshness_records(filter_freshness='', sort_by='timestamp', ascending=False):
        filter_criteria = {}
        if filter_freshness:
            filter_criteria['Eatable'] = {'$regex': filter_freshness, '$options': 'i'}
            
        return safe_execute(db_manager.get_freshness_records, filter_criteria, sort_by, ascending)
            

    # Analysis Methods
    def analyze_brand_trends(time_period):
        return safe_execute(db_manager.analyze_brand_trends, int(time_period))

    # Advanced Search
    def perform_advanced_search(collection, search_criteria, projection):
        try:
            # Parse search criteria and projection as JSON
            search_dict = json.loads(search_criteria) if search_criteria else {}
            proj_dict = json.loads(projection) if projection else {}
            
            return safe_execute(db_manager.advanced_search, collection, search_dict, proj_dict)
        except json.JSONDecodeError:
            return "Invalid JSON format for search criteria or projection"

    # Export Methods
    def export_collection(collection_name):
        return safe_execute(db_manager.export_collection_to_json, collection_name)

    # Gradio Interface Construction
    with gr.Blocks(title="Database Management System", theme=gr.themes.Soft()) as demo:
        gr.Markdown("# πŸ“Š Advanced Database Management System")
        
        with gr.Tab("Brand Records"):
            with gr.Row():

                with gr.Column():
                    gr.Markdown("## πŸ“‹ Get Brand Records")
                    filter_brand = gr.Textbox(label="Filter by Brand (optional)")
                    sort_by = gr.Dropdown(
                        ["timestamp", "count"], 
                        label="Sort By", 
                        value="timestamp"
                    )
                    ascending = gr.Checkbox(label="Ascending Order")
                    get_brands_btn = gr.Button("Retrieve Brand Records")
                    brand_records_output = gr.JSON(label="Brand Records")

          
            get_brands_btn.click(
                get_brand_records, 
                inputs=[filter_brand, sort_by, ascending], 
                outputs=brand_records_output
            )

        

        with gr.Tab("Freshness Records"):
            with gr.Row():
                with gr.Column():
                    gr.Markdown("## 🍎 Get Freshness Record")
                    filter_freshness = gr.Textbox(label="Filter by Freshness (optional)")
                    sort_by = gr.Dropdown(
                        ["timestamp", "count"], 
                        label="Sort By", 
                        value="timestamp"
                    )
                    ascending = gr.Checkbox(label="Ascending Order")
                    get_freshness_btn = gr.Button("Retrieve Freshness Records")
                    freshness_records_output = gr.JSON(label="Freshness Records")

            get_freshness_btn.click(
                get_freshness_records, 
                inputs=[filter_freshness, sort_by, ascending], 
                outputs=freshness_records_output
            )
            
                

        with gr.Tab("Analytics & Search"):
            with gr.Row():
                with gr.Column():
                    gr.Markdown("## πŸ“ˆ Brand Trend Analysis")
                    time_period = gr.Number(label="Time Period (Days)", value=30)
                    analyze_trends_btn = gr.Button("Analyze Brand Trends")
                    trends_output = gr.JSON(label="Brand Trends")

                with gr.Column():
                    gr.Markdown("## πŸ”¬ Advanced Search")
                    collection_dropdown = gr.Dropdown(
                        ["brand", "ocr", "freshness"], 
                        label="Collection"
                    )
                    search_criteria = gr.Textbox(
                        label="Search Criteria (JSON)", 
                        placeholder='e.g., {"count": {"$gt": 100}}'
                    )
                    projection = gr.Textbox(
                        label="Projection (JSON)", 
                        placeholder='e.g., {"brand": 1, "count": 1}'
                    )
                    search_btn = gr.Button("Perform Search")
                    search_output = gr.JSON(label="Search Results")

            # Analytics Interactions
            analyze_trends_btn.click(
                analyze_brand_trends, 
                inputs=time_period, 
                outputs=trends_output
            )
            search_btn.click(
                perform_advanced_search, 
                inputs=[collection_dropdown, search_criteria, projection], 
                outputs=search_output
            )

        with gr.Tab("Export"):
            with gr.Row():
                with gr.Column():
                    gr.Markdown("## πŸ“€ Export Collections")
                    export_collection_dropdown = gr.Dropdown(
                        ["brand", "ocr", "freshness"], 
                        label="Select Collection to Export"
                    )
                    export_btn = gr.Button("Export to JSON")
                    export_output = gr.Textbox(label="Export Result")

            # Export Interactions
            export_btn.click(
                export_collection, 
                inputs=export_collection_dropdown, 
                outputs=export_output
            )

    return demo


db_interface = create_gradio_interface()