Siyuan0730 commited on
Commit
5b1d121
·
1 Parent(s): 06ad84f
Files changed (1) hide show
  1. app.py +62 -36
app.py CHANGED
@@ -11,9 +11,12 @@ from nltk.tokenize import word_tokenize
11
  from nltk.corpus import stopwords
12
  from nltk.stem import WordNetLemmatizer
13
  from collections import Counter
 
 
 
14
  import nltk
15
 
16
- openai.api_key = st.secrets["OPENAI_API_KEY"]
17
 
18
  @st.cache_data
19
  def download_nltk():
@@ -24,7 +27,6 @@ def download_nltk():
24
  def chunkstring(string, length):
25
  return (string[0+i:length+i] for i in range(0, len(string), length))
26
 
27
-
28
  def pdf_parser(input_pdf):
29
  pdf = PdfReader(input_pdf)
30
  pdf_content = ""
@@ -62,7 +64,8 @@ def get_keywords(file_paths): #这里的重点是,对每一个file做尽可能
62
 
63
  return keywords_list
64
 
65
- def get_completion_from_messages(messages, model="gpt-4-1106-preview", temperature=0):
 
66
  response = openai.ChatCompletion.create(
67
  model=model,
68
  messages=messages,
@@ -302,6 +305,12 @@ def add_prompt_course_style(selected_style_list):
302
 
303
  def app():
304
  st.title("OmniTutor v0.1.0")
 
 
 
 
 
 
305
  st.markdown("""
306
  <style>
307
  .footer {
@@ -318,6 +327,7 @@ def app():
318
  <div class="footer">Made with 🧡 by Siyuan</div>
319
  """, unsafe_allow_html=True)
320
  with st.sidebar:
 
321
  st.image("https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20231021212525.png")
322
  added_files = st.file_uploader('Upload .md or .pdf files, simultaneous mixed upload these types is supported.', type=['.md','.pdf'], accept_multiple_files=True)
323
  with st.expander('Customize my course'):
@@ -353,22 +363,21 @@ def app():
353
  if "course_content_list" not in st.session_state:
354
  st.session_state.course_content_list = ''
355
 
 
 
356
  if "openai_model" not in st.session_state:
357
  st.session_state["openai_model"] = "gpt-3.5-turbo"
358
  # Initialize chat history
359
  if "messages" not in st.session_state:
360
  st.session_state.messages = []
361
-
362
 
363
  st.session_state.start_col1, st.session_state.start_col2 = st.columns(2)
364
 
365
  with st.session_state.start_col1:
366
  st.session_state.description = st.markdown('''
367
- > <font color = 'grey'> An all-round teacher. A teaching assistant who really knows the subject </font>
368
- >
369
- > <font color = 'grey'> Anything. Anywhere. All at once. </font> :100:
370
- >
371
- > Github Repo: https://github.com/Siyuan-Harry/OmniTutor
372
 
373
  ### ✨ Key features
374
 
@@ -379,12 +388,14 @@ def app():
379
 
380
  ### 🏃‍♂️ Get started!
381
 
382
- 1. **Upload learning materials**: <font color = 'grey'>The upload widget in the sidebar supports PDF and .md files simutaenously.</font>
383
- 2. **Customize your course**: <font color = 'grey'>By few clicks and swipes, adjusting teaching style, lesson count and language for your course.</font>
384
- 3. **Start course generating**: <font color = 'grey'>Touch "Generate my course!" button in the sidebar, then watch how OmniTutor creates personal-customized course for you.</font>
385
- 4. **Interactive learning**: <font color = 'grey'>Learn the course, and ask OmniTutor any questions related to this course whenever you encountered them.</font>
 
386
 
387
  🎉 Have fun playing with Omnitutor!
 
388
  ''', unsafe_allow_html=True
389
  )
390
  with st.session_state.start_col2:
@@ -393,21 +404,21 @@ def app():
393
 
394
  > <font color = 'grey'> 用OmniTutor阅读专业的论文! </font>
395
 
396
- <img src="https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/WX20231104-122256@2x%2016.03.49.png" height = "300" />
397
 
398
- <img src="https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/WX20231104-113553@2x%202.png" height = "300" />
399
 
400
  > <font color = 'grey'> 用OmniTutor学习Python! </font>
401
 
402
- <img src="https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/WX20231103-215001@2x.png" height = "300" />
403
 
404
- <img src="https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/WX20231103-214916@2x.png" height = "300" />
405
 
406
  ---
407
 
408
  ### 💰 Support by donation 捐款支持
409
 
410
- - 请捐款支持。因为每位用户的每次课程生成,都会产生一定的费用。个人开发者维护不易
411
  - Please donate to support. Because each course generated by each user will incur a certain cost for me.
412
 
413
  <img src="https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20231110145229.png" height = "260" />
@@ -426,24 +437,37 @@ def app():
426
 
427
 
428
  if btn:
429
- st.session_state.start_col1.empty()
430
- st.session_state.start_col2.empty()
431
- st.session_state.description.empty()
432
- st.session_state.case_pay.empty()
433
-
434
- #initialize app
435
- temp_file_paths = initialize_file(added_files)
436
- st.session_state.embeddings_df, st.session_state.faiss_index = initialize_vdb(temp_file_paths)
437
- st.session_state.course_outline_list = initialize_outline(temp_file_paths, num_lessons, language)
438
- st.session_state.course_content_list = initialize_content(st.session_state.course_outline_list, st.session_state.embeddings_df, st.session_state.faiss_index, language, style_options)
439
-
440
- st.markdown('''
441
- > 🤔 <font color = 'grey'> **Not satisfied with this course?** Simply click "Generate my course!" button to regenerate a new one! </font>
442
- >
443
- > 😁 <font color = 'grey'> If the course is good enough for you, learn and enter questions related in the input box below 👇... </font>
444
-
445
- :blue[Wish you all the best in your learning journey :)]
446
- ''', unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
447
 
448
 
449
  col1, col2 = st.columns([0.6,0.4])
@@ -454,6 +478,8 @@ def app():
454
  st.session_state.start_col2.empty()
455
  st.session_state.description.empty()
456
  st.session_state.case_pay.empty()
 
 
457
 
458
  with col1:
459
  #把课程大纲打印出来
 
11
  from nltk.corpus import stopwords
12
  from nltk.stem import WordNetLemmatizer
13
  from collections import Counter
14
+ #from langdetect import detect
15
+ #import jieba
16
+ #import jieba.analyse
17
  import nltk
18
 
19
+
20
 
21
  @st.cache_data
22
  def download_nltk():
 
27
  def chunkstring(string, length):
28
  return (string[0+i:length+i] for i in range(0, len(string), length))
29
 
 
30
  def pdf_parser(input_pdf):
31
  pdf = PdfReader(input_pdf)
32
  pdf_content = ""
 
64
 
65
  return keywords_list
66
 
67
+
68
+ def get_completion_from_messages(messages, model="gpt-4", temperature=0):
69
  response = openai.ChatCompletion.create(
70
  model=model,
71
  messages=messages,
 
305
 
306
  def app():
307
  st.title("OmniTutor v0.1.0")
308
+ announce = st.caption('''
309
+ :blue[⚠️Key Announcement:] The free service has been disrupted due to high costs. In order to use OmniTutor you need to type your own OPENAI API key into the sidebar.
310
+
311
+ :blue[⚠️关键公告:] 免费服务现在已经因为过高的支出而中断。为了使用OmniTutor,你需要在边栏顶部输入自己的Openai API key。
312
+ ''')
313
+ divider = st.divider()
314
  st.markdown("""
315
  <style>
316
  .footer {
 
327
  <div class="footer">Made with 🧡 by Siyuan</div>
328
  """, unsafe_allow_html=True)
329
  with st.sidebar:
330
+ api_key = st.text_input('Your OpenAI API key:', 'sk-...')
331
  st.image("https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20231021212525.png")
332
  added_files = st.file_uploader('Upload .md or .pdf files, simultaneous mixed upload these types is supported.', type=['.md','.pdf'], accept_multiple_files=True)
333
  with st.expander('Customize my course'):
 
363
  if "course_content_list" not in st.session_state:
364
  st.session_state.course_content_list = ''
365
 
366
+ if "OPENAI_API_KEY" not in st.session_state:
367
+ st.session_state["OPENAI_API_KEY"] = ''
368
  if "openai_model" not in st.session_state:
369
  st.session_state["openai_model"] = "gpt-3.5-turbo"
370
  # Initialize chat history
371
  if "messages" not in st.session_state:
372
  st.session_state.messages = []
 
373
 
374
  st.session_state.start_col1, st.session_state.start_col2 = st.columns(2)
375
 
376
  with st.session_state.start_col1:
377
  st.session_state.description = st.markdown('''
378
+ <font color = 'grey'> An all-round teacher. A teaching assistant who really knows the subject. **Anything. Anywhere. All at once.** </font> :100:
379
+
380
+ Github Repo: https://github.com/Siyuan-Harry/OmniTutor
 
 
381
 
382
  ### ✨ Key features
383
 
 
388
 
389
  ### 🏃‍♂️ Get started!
390
 
391
+ 1. **Input Your OpenAI API Key**: <font color = 'grey'>Give OmniTutor your own OpenAI API key (On top of the **sidebar**) to get started.</font>
392
+ 2. **Upload learning materials**: <font color = 'grey'>The upload widget in the sidebar supports PDF and .md files simutaenously.</font>
393
+ 3. **Customize your course**: <font color = 'grey'>By few clicks and swipes, adjusting teaching style, lesson count and language for your course.</font>
394
+ 4. **Start course generating**: <font color = 'grey'>Touch "Generate my course!" button in the sidebar, then watch how OmniTutor creates personal-customized course for you.</font>
395
+ 5. **Interactive learning**: <font color = 'grey'>Learn the course, and ask OmniTutor any questions related to this course whenever you encountered them.</font>
396
 
397
  🎉 Have fun playing with Omnitutor!
398
+
399
  ''', unsafe_allow_html=True
400
  )
401
  with st.session_state.start_col2:
 
404
 
405
  > <font color = 'grey'> 用OmniTutor阅读专业的论文! </font>
406
 
407
+ <img src="https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/WX20231104-122256@2x%2016.03.49.png" height = "200" />
408
 
409
+ <img src="https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/WX20231104-113553@2x%202.png" height = "200" />
410
 
411
  > <font color = 'grey'> 用OmniTutor学习Python! </font>
412
 
413
+ <img src="https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/WX20231103-215001@2x.png" height = "200" />
414
 
415
+ <img src="https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/WX20231103-214916@2x.png" height = "200" />
416
 
417
  ---
418
 
419
  ### 💰 Support by donation 捐款支持
420
 
421
+ - 如果OmniTutor对你有所帮助,请捐款赞助。因为每位用户的每次课程生成,都会产生一定的费用。个人开发者维护不易
422
  - Please donate to support. Because each course generated by each user will incur a certain cost for me.
423
 
424
  <img src="https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20231110145229.png" height = "260" />
 
437
 
438
 
439
  if btn:
440
+ if api_key != "sk-..." and api_key !="" and api_key.startswith("sk-"):
441
+ st.session_state.start_col1.empty()
442
+ st.session_state.start_col2.empty()
443
+ st.session_state.description.empty()
444
+ st.session_state.case_pay.empty()
445
+ announce.empty()
446
+ divider.empty()
447
+
448
+ #initialize app
449
+ temp_file_paths = initialize_file(added_files)
450
+ st.session_state["OPENAI_API_KEY"] = api_key
451
+ openai.api_key = st.session_state["OPENAI_API_KEY"]
452
+ st.session_state.embeddings_df, st.session_state.faiss_index = initialize_vdb(temp_file_paths)
453
+ st.session_state.course_outline_list = initialize_outline(temp_file_paths, num_lessons, language)
454
+ st.session_state.course_content_list = initialize_content(st.session_state.course_outline_list, st.session_state.embeddings_df, st.session_state.faiss_index, language, style_options)
455
+
456
+ st.markdown('''
457
+ > 🤔 <font color = 'grey'> **Not satisfied with this course?** Simply click "Generate my course!" button to regenerate a new one! </font>
458
+ >
459
+ > 😁 <font color = 'grey'> If the course is good enough for you, learn and enter questions related in the input box below 👇... </font>
460
+
461
+ :blue[Wish you all the best in your learning journey :)]
462
+ ''', unsafe_allow_html=True)
463
+ else:
464
+ st.session_state.start_col1.empty()
465
+ st.session_state.start_col2.empty()
466
+ st.session_state.description.empty()
467
+ st.session_state.case_pay.empty()
468
+ announce.empty()
469
+ divider.empty()
470
+ warning = st.write("请输入正确的API Key令牌")
471
 
472
 
473
  col1, col2 = st.columns([0.6,0.4])
 
478
  st.session_state.start_col2.empty()
479
  st.session_state.description.empty()
480
  st.session_state.case_pay.empty()
481
+ announce.empty()
482
+ divider.empty()
483
 
484
  with col1:
485
  #把课程大纲打印出来