-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
511 lines (398 loc) · 11.5 KB
/
main.go
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/asim/reminder/api"
"github.com/asim/reminder/hadith"
"github.com/asim/reminder/html"
"github.com/asim/reminder/html/files"
"github.com/asim/reminder/names"
"github.com/asim/reminder/quran"
"github.com/asim/reminder/search"
)
var (
IndexFlag = flag.Bool("index", false, "Index data for search. Stored at $HOME/reminder.idx")
ExportFlag = flag.Bool("export", false, "Export the index data to $HOME/reminder.idx.gob.gz")
ImportFlag = flag.Bool("import", false, "Import the index data from $HOME/reminder.idx.gob.gz")
GenerateFlag = flag.Bool("generate", false, "Generate the html files")
ServerFlag = flag.Bool("serve", false, "Run the server")
)
func indexContent(idx *search.Index, md map[string]string, text string) {
// index the documents
// TODO: use original json
lines := strings.Split(text, "\n")
fmt.Println("Indexing")
if err := idx.Store(md, lines...); err != nil {
fmt.Println("Error indexing", err)
}
}
func indexQuran(idx *search.Index, q *quran.Quran) {
fmt.Println("Indexing Quran")
for _, chapter := range q.Chapters {
for _, verse := range chapter.Verses {
indexContent(idx, map[string]string{
"source": "quran",
"chapter": fmt.Sprintf("%v", chapter.Number),
"verse": fmt.Sprintf("%v", verse.Number),
"name": chapter.Name,
}, verse.Text)
}
}
}
func indexNames(idx *search.Index, n *names.Names) {
fmt.Println("Indexing Names")
for _, name := range *n {
indexContent(idx, map[string]string{
"source": "names",
"meaning": name.Meaning,
"english": name.English,
"arabic": name.Arabic,
}, strings.Join([]string{name.Meaning, name.English, name.Description}, " - "))
}
}
func indexHadith(idx *search.Index, b *hadith.Volumes) {
fmt.Println("Indexing Hadith")
for _, volume := range b.Contents {
for _, book := range volume.Books {
for _, hadith := range book.Hadiths {
indexContent(idx, map[string]string{
"source": "bukhari",
"volume": volume.Name,
"book": book.Name,
"by": hadith.By,
"info": hadith.Info,
}, hadith.Text)
}
}
}
}
func gen(idx *search.Index, q string) (string, []string) {
res, err := idx.Query(q)
if err != nil {
return "", nil
}
var contexts []string
for _, r := range res {
b, _ := json.Marshal(r)
// TODO: maybe just provide text
contexts = append(contexts, string(b))
}
return askLLM(context.TODO(), contexts, q), contexts
}
var questions = []string{
"What is the Reminder?",
"What is the Quran?",
"What is the Hadith?",
"Who is Allah?",
"Who is the prophet Muhammad",
"Why do we 'worship' Allah?",
"How do we 'worship' Allah?",
"What happens when we die?",
"How do I remember Allah?",
"How do I become Muslim?",
}
func main() {
flag.Parse()
// create a new index
idx := search.New("reminder", false)
// Load the pre-existing data
if err := idx.Load(); err != nil {
fmt.Println(err)
}
// load data
q := quran.Load()
n := names.Load()
b := hadith.Load()
// render the markdown as html
if *GenerateFlag {
fmt.Println("Loading data")
fmt.Println("Generating html")
text := q.HTML()
name := n.HTML()
books := b.Markdown()
vhtml := html.RenderTemplate("Hadith", books)
thtml := html.RenderTemplate("Quran", text)
nhtml := html.RenderHTML("Names", name)
shtml := html.RenderHTML("Search", html.Search)
var about string
for _, q := range questions {
a, _ := gen(idx, q)
about += fmt.Sprintf("# %s", q)
about += fmt.Sprintln()
about += fmt.Sprintf("%s", a)
about += fmt.Sprintln()
}
ihtml := html.RenderTemplate("Index", about)
ap := api.Load()
apiHtml := html.RenderTemplate("API", ap.Markdown())
// write html files
os.WriteFile(filepath.Join(".", "html", "files", "api.html"), []byte(apiHtml), 0644)
os.WriteFile(filepath.Join(".", "html", "files", "index.html"), []byte(ihtml), 0644)
os.WriteFile(filepath.Join(".", "html", "files", "search.html"), []byte(shtml), 0644)
os.WriteFile(filepath.Join(".", "html", "files", "quran.html"), []byte(thtml), 0644)
os.WriteFile(filepath.Join(".", "html", "files", "names.html"), []byte(nhtml), 0644)
os.WriteFile(filepath.Join(".", "html", "files", "hadith.html"), []byte(vhtml), 0644)
// write json files
os.WriteFile(filepath.Join(".", "html", "files", "quran.json"), q.JSON(), 0644)
os.WriteFile(filepath.Join(".", "html", "files", "names.json"), n.JSON(), 0644)
os.WriteFile(filepath.Join(".", "html", "files", "hadith.json"), b.JSON(), 0644)
return
}
// index the quran in english
indexed := make(chan bool, 1)
if *IndexFlag {
fmt.Println("Indexing data")
go func() {
indexQuran(idx, q)
indexNames(idx, n)
indexHadith(idx, b)
// done
close(indexed)
}()
} else {
close(indexed)
}
if *ExportFlag {
fmt.Println("Exporting index")
if err := idx.Export(); err != nil {
fmt.Println(err)
}
return
}
if *ImportFlag {
fmt.Println("Importing index")
if err := idx.Import(); err != nil {
fmt.Println(err)
}
}
// load the data from html
apiHtml := files.Get("api.html")
ihtml := files.Get("index.html")
shtml := files.Get("search.html")
//thtml := files.Get("quran.html")
//nhtml := files.Get("names.html")
//vhtml := files.Get("hadith.html")
otf := files.Get("arabic.otf")
qjson := files.Get("quran.json")
njson := files.Get("names.json")
hjson := files.Get("hadith.json")
http.HandleFunc("/files/arabic.otf", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(otf))
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(ihtml))
})
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(apiHtml))
})
http.HandleFunc("/quran", func(w http.ResponseWriter, r *http.Request) {
qhtml := html.RenderHTML("Quran", q.TOC())
w.Write([]byte(qhtml))
})
http.HandleFunc("/quran/{id}", func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if len(id) == 0 {
return
}
ch, _ := strconv.Atoi(id)
if ch < 1 || ch > 114 {
return
}
head := fmt.Sprintf("%d | Quran", ch)
qhtml := html.RenderHTML(head, q.Get(ch).HTML())
w.Write([]byte(qhtml))
})
http.HandleFunc("/quran/{id}/{ver}", func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if len(id) == 0 {
return
}
ver := r.PathValue("ver")
if len(ver) == 0 {
return
}
ch, _ := strconv.Atoi(id)
ve, _ := strconv.Atoi(ver)
if ch < 1 || ch > 114 {
return
}
cc := q.Get(ch)
if ve < 1 || ve > len(cc.Verses) {
return
}
vv := cc.Verses[ve-1]
head := fmt.Sprintf("%d:%d | Quran", ch, ve)
vhtml := html.RenderHTML(head, vv.HTML())
w.Write([]byte(vhtml))
})
http.HandleFunc("/names", func(w http.ResponseWriter, r *http.Request) {
qhtml := html.RenderHTML("Names", n.TOC())
w.Write([]byte(qhtml))
})
http.HandleFunc("/names/{id}", func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if len(id) == 0 {
return
}
name, _ := strconv.Atoi(id)
if name < 1 || name > len(*n) {
return
}
head := fmt.Sprintf("%d | Names", name)
qhtml := html.RenderHTML(head, n.Get(name).HTML())
w.Write([]byte(qhtml))
})
http.HandleFunc("/hadith", func(w http.ResponseWriter, r *http.Request) {
qhtml := html.RenderHTML("Hadith", b.TOC())
w.Write([]byte(qhtml))
})
http.HandleFunc("/hadith/{book}", func(w http.ResponseWriter, r *http.Request) {
book := r.PathValue("book")
if len(book) == 0 {
return
}
ch, _ := strconv.Atoi(book)
if ch < 1 || ch > len(b.Books) {
return
}
head := fmt.Sprintf("%d | Hadith", ch)
qhtml := html.RenderHTML(head, b.Get(ch).HTML())
w.Write([]byte(qhtml))
})
http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(shtml))
})
http.HandleFunc("/api/quran", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(qjson))
})
http.HandleFunc("/api/quran/{chapter}", func(w http.ResponseWriter, r *http.Request) {
ch := r.PathValue("chapter")
if len(ch) == 0 {
return
}
chapter, _ := strconv.Atoi(ch)
if chapter < 1 || chapter > 114 {
return
}
b := q.Get(chapter).JSON()
w.Write(b)
})
http.HandleFunc("/api/quran/{chapter}/{verse}", func(w http.ResponseWriter, r *http.Request) {
ch := r.PathValue("chapter")
if len(ch) == 0 {
return
}
chapter, _ := strconv.Atoi(ch)
if chapter < 1 || chapter > 114 {
return
}
ve := r.PathValue("verse")
if len(ch) == 0 {
return
}
cc := q.Get(chapter)
verse, _ := strconv.Atoi(ve)
if verse < 1 || verse > len(cc.Verses) {
return
}
vee := cc.Verses[verse-1]
b := vee.JSON()
w.Write(b)
})
http.HandleFunc("/api/names", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(njson))
})
http.HandleFunc("/api/hadith", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(hjson))
})
http.HandleFunc("/api/hadith/{book}", func(w http.ResponseWriter, r *http.Request) {
bk := r.PathValue("book")
if len(bk) == 0 {
return
}
book, _ := strconv.Atoi(bk)
if book < 1 || book > len(b.Books) {
return
}
b := b.Get(book).JSON()
w.Write(b)
})
http.HandleFunc("/api/generate", func(w http.ResponseWriter, r *http.Request) {
b, _ := ioutil.ReadAll(r.Body)
var data map[string]interface{}
json.Unmarshal(b, &data)
q := data["q"].(string)
prompt := `Generate a detailed summary for the following with it's meaning and origin, output the response as JSON with the fields:
name, description, summary. Each field itself should be a string.
%s
`
answer := askLLM(r.Context(), nil, fmt.Sprintf(prompt, q))
w.Write([]byte(answer))
})
http.HandleFunc("/api/translate", func(w http.ResponseWriter, r *http.Request) {
b, _ := ioutil.ReadAll(r.Body)
var data map[string]interface{}
json.Unmarshal(b, &data)
q := data["q"].(string)
prompt := `Translate the following into a modern interpretation, transliterate and then word by word.
For each word provide 3 alternatives and a transliteration in english. Generate the output as JSON.
The response will be served via an API so ensure it's entirely json compliant with no markdown.
Ensure consistency in the output using fields translation, transliteration and word_by_word.
The word_by_word field itself should be an array with word in english, arabic, translations and
transliteration.:
%s`
answer := askLLM(r.Context(), nil, fmt.Sprintf(prompt, q))
w.Write([]byte(answer))
})
http.HandleFunc("/api/search", func(w http.ResponseWriter, r *http.Request) {
select {
case <-indexed:
default:
// not indexed yet because blocked
w.Write([]byte(`{"error": "Indexing content"}`))
return
}
// indexed or no index of any kind
if r.Method == "POST" {
b, _ := ioutil.ReadAll(r.Body)
var data map[string]interface{}
json.Unmarshal(b, &data)
q := data["q"].(string)
res, err := idx.Query(q)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
var contexts []string
for _, r := range res {
for k, v := range r.Metadata {
delete(r.Metadata, k)
r.Metadata[strings.ToLower(k)] = v
}
b, _ := json.Marshal(r)
// TODO: maybe just provide text
contexts = append(contexts, string(b))
}
answer := askLLM(r.Context(), contexts, q)
output, _ := json.Marshal(map[string]interface{}{
"q": q,
"answer": string(html.Render([]byte(answer))),
"references": res,
})
w.Write(output)
return
}
})
if *ServerFlag {
fmt.Println("Starting server :8080")
http.ListenAndServe(":8080", nil)
}
}