intellectronica commited on
Commit
8cab861
·
1 Parent(s): 52f2071

More fh progress

Browse files
Files changed (3) hide show
  1. .gitignore +2 -0
  2. app.js +9 -16
  3. fhapp.py +25 -11
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ __pycache__/
2
+ .sesskey
app.js CHANGED
@@ -1,28 +1,21 @@
1
- const API_KEY_FIELDS = ['OPENAI_API_KEY', 'JINA_API_KEY'];
 
2
 
3
- function getAPIKeyTextbox(apiKeyField) {
4
- return document.getElementById(apiKeyField).getElementsByTagName('input')[0];
5
- }
6
-
7
- function initializeTextboxListeners() {
8
  for (let apiKeyField of API_KEY_FIELDS) {
9
- getAPIKeyTextbox(apiKeyField).addEventListener('input', () => {
10
- localStorage.setItem(apiKeyField, getAPIKeyTextbox(apiKeyField).value);
11
  });
12
  }
13
- }
14
 
15
- function populateTextboxesFromLocalStorsge() {
16
  for (let apiKeyField of API_KEY_FIELDS) {
17
- const textbox = getAPIKeyTextbox(apiKeyField);
18
  const apiKeyValue = localStorage.getItem(apiKeyField);
19
  if (apiKeyValue && textbox.value === '') {
20
  textbox.value = apiKeyValue;
21
  }
22
  }
23
- }
24
 
25
- window.setTimeout(() => {
26
- initializeTextboxListeners();
27
- populateTextboxesFromLocalStorsge();
28
- }, 1234);
 
1
+ window.onload = () => {
2
+ const API_KEY_FIELDS = ['openai_api_key', 'jina_api_key'];
3
 
 
 
 
 
 
4
  for (let apiKeyField of API_KEY_FIELDS) {
5
+ document.getElementById(apiKeyField).addEventListener('input', () => {
6
+ localStorage.setItem(apiKeyField, document.getElementById(apiKeyField).value);
7
  });
8
  }
 
9
 
 
10
  for (let apiKeyField of API_KEY_FIELDS) {
11
+ const textbox = document.getElementById(apiKeyField);
12
  const apiKeyValue = localStorage.getItem(apiKeyField);
13
  if (apiKeyValue && textbox.value === '') {
14
  textbox.value = apiKeyValue;
15
  }
16
  }
 
17
 
18
+ document.getElementById('text').addEventListener('change', () => {
19
+ document.getElementById('num_flashcards').value = Math.round(document.getElementById('text').value.length / 234);
20
+ });
21
+ }
fhapp.py CHANGED
@@ -5,46 +5,60 @@ from markdown import markdown
5
 
6
  from mkflashcards import *
7
 
8
- app, rt = fh.fast_app(live=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  @app.get("/")
11
  def home():
12
- return (
13
  fh.Container(
14
  fh.Card(fh.NotStr(markdown(ABOUT))),
15
  fh.Group(
16
  fh.B('OPENAI_API_KEY'),
17
- fh.Input(type='password', value=os.getenv('OPENAI_API_KEY', ''), id='OPENAI_API_KEY'),
18
  ),
19
  fh.Grid(
20
  fh.Group(
21
  fh.B('JINA_API_KEY'),
22
- fh.Input(type='password', value=os.getenv('JINA_API_KEY', ''), id='JINA_API_KEY'),
23
  ),
24
  fh.Group(
25
  fh.B('URL'),
26
- fh.Input(type='text', id='URL'),
27
  ),
28
- fh.Button('Fetch Text'),
29
  ),
30
  fh.Group(
31
  fh.B('Text'),
32
- fh.Textarea(rows=7, id='TEXT'),
33
  ),
34
  fh.Grid(
35
  fh.Group(
36
  fh.B('Number of flashcards to generate'),
37
- fh.Input(type='number', value=23, id='NUM_FLASHCARDS'),
38
  ),
39
  fh.Group(
40
  fh.B('Tags'),
41
- fh.Input(type='text', id='TAGS'),
42
  ),
43
- fh.Button('Generate Flashcards'),
44
  ),
45
  fh.Group(
46
  fh.B('Flashcards'),
47
- fh.Textarea(rows=23, id='FLASHCARDS'),
48
  ),
49
  ),
50
  )
 
5
 
6
  from mkflashcards import *
7
 
8
+ def app_js():
9
+ with open('app.js') as f:
10
+ return f.read()
11
+
12
+ app, rt = fh.fast_app(
13
+ hdrs=[fh.Script(app_js())],
14
+ )
15
+
16
+ @app.post('/-/fetch-text')
17
+ async def do_fetch_text(jina_api_key: str, url: str, *args, **kwargs):
18
+ return fetch_text(url, jina_api_key)
19
+
20
+ @app.post('/-/generate-flashcards')
21
+ async def do_fetch_text(openai_api_key: str, num_flashcards: int, tags: str, text: str, *args, **kwargs):
22
+ return generate_flashcards(openai_api_key, text, num_flashcards, tags)
23
 
24
  @app.get("/")
25
  def home():
26
+ return fh.Form(
27
  fh.Container(
28
  fh.Card(fh.NotStr(markdown(ABOUT))),
29
  fh.Group(
30
  fh.B('OPENAI_API_KEY'),
31
+ fh.Input(name='openai_api_key', type='password', value=os.getenv('OPENAI_API_KEY', ''), id='openai_api_key'),
32
  ),
33
  fh.Grid(
34
  fh.Group(
35
  fh.B('JINA_API_KEY'),
36
+ fh.Input(name='jina_api_key', type='password', value=os.getenv('JINA_API_KEY', ''), id='jina_api_key'),
37
  ),
38
  fh.Group(
39
  fh.B('URL'),
40
+ fh.Input(name='url', type='text', id='url'),
41
  ),
42
+ fh.Button('Fetch Text', hx_post='/-/fetch-text', hx_target='#text', hx_swap='innerHTML'),
43
  ),
44
  fh.Group(
45
  fh.B('Text'),
46
+ fh.Textarea(name='text', rows=7, id='text'),
47
  ),
48
  fh.Grid(
49
  fh.Group(
50
  fh.B('Number of flashcards to generate'),
51
+ fh.Input(name='num_flashcards', type='number', value=23, id='num_flashcards'),
52
  ),
53
  fh.Group(
54
  fh.B('Tags'),
55
+ fh.Input(name='tags', type='text', id='tags'),
56
  ),
57
+ fh.Button('Generate Flashcards', hx_post='/-/generate-flashcards', hx_target='#flashcards', hx_swap='innerHTML'),
58
  ),
59
  fh.Group(
60
  fh.B('Flashcards'),
61
+ fh.Textarea(rows=23, id='flashcards'),
62
  ),
63
  ),
64
  )