Download src/main.zig from RohanVashisht/ZigistrySmartSemanticSearch: direct link, hf CLI and curl.
- Browser
- Download file 1.72 kB
-
https://huggingface.co/spaces/RohanVashisht/ZigistrySmartSemanticSearch/resolve/main/src/main.zig
- Command line
-
hf download hf://spaces/RohanVashisht/ZigistrySmartSemanticSearch/src/main.zig
-
curl -L -o main.zig https://huggingface.co/spaces/RohanVashisht/ZigistrySmartSemanticSearch/resolve/main/src/main.zig
1.72 kB
| const std = ("std"); | |
| const zap = ("zap"); | |
| const ZigSmartSearch = ("ZigSmartSearch").ZigSmartSearch; | |
| const database = ("database.json"); | |
| const allocator = std.heap.c_allocator; | |
| var instance: ZigSmartSearch = undefined; | |
| fn on_request(r: zap.Request) !void { | |
| try r.setHeader("Content-Type", "application/json"); | |
| try r.setHeader("Access-Control-Allow-Origin", "*"); | |
| if (r.query) |the_query| { | |
| var results = try instance.search(the_query); | |
| defer results.deinit(); | |
| if (results.items.len == 0) { | |
| r.sendJson("[]") catch return; | |
| return; | |
| } | |
| const res = try results.toOwnedSlice(); | |
| var arraylist = std.ArrayList(u8).init(allocator); | |
| errdefer arraylist.deinit(); | |
| try arraylist.append('['); | |
| for (res) |result| { | |
| try arraylist.append('"'); | |
| try arraylist.appendSlice(result.doc.get("title").?.string); | |
| try arraylist.append('"'); | |
| try arraylist.append(','); | |
| } | |
| _ = arraylist.pop(); // remove last comma | |
| try arraylist.append(']'); | |
| const result = try arraylist.toOwnedSlice(); | |
| defer allocator.free(result); | |
| r.sendJson(result) catch return; | |
| } | |
| } | |
| pub fn main() !void { | |
| instance = try ZigSmartSearch.init( | |
| allocator, | |
| database, | |
| ZigSmartSearch.default_config, | |
| ); | |
| var listener = zap.HttpListener.init(.{ | |
| .port = 7860, | |
| .on_request = on_request, | |
| .log = true, | |
| }); | |
| try listener.listen(); | |
| std.debug.print("Listening on 0.0.0.0:7860\n", .{}); | |
| // start worker threads | |
| zap.start(.{ | |
| .threads = 2, | |
| .workers = 2, | |
| }); | |
| } | |