rohan13 commited on
Commit
3b0afd8
·
1 Parent(s): f834049

Saving chat_history in mongodb

Browse files
Files changed (6) hide show
  1. app.py +83 -71
  2. main.py +7 -4
  3. mapping.py +79 -0
  4. memory.py +63 -0
  5. requirements.txt +2 -1
  6. utils.py +40 -92
app.py CHANGED
@@ -1,102 +1,114 @@
 
 
 
1
  import gradio as gr
2
- from main import index, run
3
  from gtts import gTTS
4
- import os, time
5
-
6
  from transformers import pipeline
7
 
 
 
8
  p = pipeline("automatic-speech-recognition", model="openai/whisper-base")
9
 
10
  """Use text to call chat method from main.py"""
11
 
12
  models = ["GPT-3.5", "Flan UL2", "Flan T5"]
13
 
14
- def add_text(history, text, model):
15
- print("Question asked: " + text)
16
- response = run_model(text, model)
17
- history = history + [(text, response)]
18
- print(history)
19
- return history, ""
20
-
21
-
22
- def run_model(text, model):
23
- start_time = time.time()
24
- print("start time:" + str(start_time))
25
- response = run(text, model)
26
- end_time = time.time()
27
- # If response contains string `SOURCES:`, then add a \n before `SOURCES`
28
- if "SOURCES:" in response:
29
- response = response.replace("SOURCES:", "\nSOURCES:")
30
- # response = response + "\n\n" + "Time taken: " + str(end_time - start_time)
31
- print(response)
32
- print("Time taken: " + str(end_time - start_time))
33
- return response
34
-
35
-
36
-
37
- def get_output(history, audio, model):
38
-
39
- txt = p(audio)["text"]
40
- # history.append(( (audio, ) , txt))
41
- audio_path = 'response.wav'
42
- response = run_model(txt, model)
43
- # Remove all text from SOURCES: to the end of the string
44
- trimmed_response = response.split("SOURCES:")[0]
45
- myobj = gTTS(text=trimmed_response, lang='en', slow=False)
46
- myobj.save(audio_path)
47
- # split audio by / and keep the last element
48
- # audio = audio.split("/")[-1]
49
- # audio = audio + ".wav"
50
- history.append(( (audio, ) , (audio_path, )))
51
- print(history)
52
- return history
53
-
54
- def set_model(history, model):
55
- print("Model selected: " + model)
56
- history = get_first_message(history)
57
- index(model)
58
- return history
59
-
60
-
61
- def get_first_message(history):
62
- history = [(None,
63
- 'Learn about <a href="https://www.coursera.org/learn/3d-printing-applications">3D printing Applications</a> course with referred sources.')]
64
- return history
65
-
66
-
67
- def bot(history):
68
- return history
69
-
70
  with gr.Blocks(theme='snehilsanyal/scikit-learn') as demo:
 
71
 
72
- gr.HTML("<h1 style='text-align: center;'>Course Assistant - 3D Printing Applications</h1>")
73
 
74
- chatbot = gr.Chatbot(get_first_message([]), elem_id="chatbot", label='Coursera Chatbot').style(height=500, container=False)
 
75
 
76
- with gr.Row():
77
- # Create radio button to select model
78
- radio = gr.Radio(models, label="Choose a model", value="GPT-3.5", type="value", visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  with gr.Row():
80
  # with gr.Column(scale=0.75):
81
- txt = gr.Textbox(
82
- label="Ask your question here",
83
- placeholder="Enter text and press enter", lines=1
84
- ).style(container=False)
85
 
86
  # with gr.Column(scale=0.25):
87
- audio = gr.Audio(source="microphone", type="filepath", visible=False).style(container=False)
88
 
89
  txt.submit(add_text, [chatbot, txt, radio], [chatbot, txt], postprocess=False).then(
90
  bot, chatbot, chatbot
91
  )
92
 
93
- audio.change(fn=get_output, inputs=[chatbot, audio, radio], outputs=[chatbot]).then(
94
  bot, chatbot, chatbot
95
  )
96
 
97
  radio.change(fn=set_model, inputs=[chatbot, radio], outputs=[chatbot]).then(bot, chatbot, chatbot)
98
 
99
- audio.change(lambda:None, None, audio)
100
 
101
  set_model(chatbot, radio.value)
102
 
 
1
+ import time
2
+ import uuid
3
+
4
  import gradio as gr
 
5
  from gtts import gTTS
 
 
6
  from transformers import pipeline
7
 
8
+ from main import index, run
9
+
10
  p = pipeline("automatic-speech-recognition", model="openai/whisper-base")
11
 
12
  """Use text to call chat method from main.py"""
13
 
14
  models = ["GPT-3.5", "Flan UL2", "Flan T5"]
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  with gr.Blocks(theme='snehilsanyal/scikit-learn') as demo:
17
+ state = gr.State([])
18
 
 
19
 
20
+ def create_session_id():
21
+ return str(uuid.uuid4())
22
 
23
+
24
+ def add_text(history, text, model):
25
+ print("Question asked: " + text)
26
+ response = run_model(text, model)
27
+ history = history + [(text, response)]
28
+ print(history)
29
+ return history, ""
30
+
31
+
32
+ def run_model(text, model):
33
+ start_time = time.time()
34
+ print("start time:" + str(start_time))
35
+ response = run(text, model, state.session_id)
36
+ end_time = time.time()
37
+ # If response contains string `SOURCES:`, then add a \n before `SOURCES`
38
+ if "SOURCES:" in response:
39
+ response = response.replace("SOURCES:", "\nSOURCES:")
40
+ # response = response + "\n\n" + "Time taken: " + str(end_time - start_time)
41
+ print(response)
42
+ print("Time taken: " + str(end_time - start_time))
43
+ return response
44
+
45
+
46
+ def get_output(history, audio, model):
47
+ txt = p(audio)["text"]
48
+ # history.append(( (audio, ) , txt))
49
+ audio_path = 'response.wav'
50
+ response = run_model(txt, model)
51
+ # Remove all text from SOURCES: to the end of the string
52
+ trimmed_response = response.split("SOURCES:")[0]
53
+ myobj = gTTS(text=trimmed_response, lang='en', slow=False)
54
+ myobj.save(audio_path)
55
+ # split audio by / and keep the last element
56
+ # audio = audio.split("/")[-1]
57
+ # audio = audio + ".wav"
58
+ history.append(((audio,), (audio_path,)))
59
+ print(history)
60
+ return history
61
+
62
+
63
+ def set_model(history, model):
64
+ print("Model selected: " + model)
65
+ history = get_first_message(history)
66
+ index(model, state.session_id)
67
+ return history
68
+
69
+
70
+ def get_first_message(history):
71
+ history = [(None,
72
+ 'Learn about <a href="https://www.coursera.org/learn/3d-printing-applications/">3D printing Applications</a> course with referred sources.')]
73
+ return history
74
+
75
+
76
+ def bot(history):
77
+ return history
78
+
79
+
80
+ state.session_id = create_session_id()
81
+ print("Session ID: " + state.session_id)
82
+ # Title on top in middle of the page
83
+ # gr.HTML("<h1 style='text-align: center;'>Course Assistant - 3D Printing Revolution</h1>")
84
+
85
+ chatbot = gr.Chatbot(get_first_message([]), elem_id="chatbot", label='3D Printing Revolution').style(height=400,
86
+ container=False)
87
+
88
+ # with gr.Row():
89
+ # Create radio button to select model
90
+ radio = gr.Radio(models, label="Choose a model", value="GPT-3.5", type="value", visible=False)
91
  with gr.Row():
92
  # with gr.Column(scale=0.75):
93
+ txt = gr.Textbox(
94
+ label="Ask your question here",
95
+ placeholder="Enter text and press enter", lines=1
96
+ ).style(container=False)
97
 
98
  # with gr.Column(scale=0.25):
99
+ audio = gr.Audio(source="microphone", type="filepath", visible=False)
100
 
101
  txt.submit(add_text, [chatbot, txt, radio], [chatbot, txt], postprocess=False).then(
102
  bot, chatbot, chatbot
103
  )
104
 
105
+ audio.change(fn=get_output, inputs=[chatbot, audio, radio], outputs=[chatbot], show_progress=True).then(
106
  bot, chatbot, chatbot
107
  )
108
 
109
  radio.change(fn=set_model, inputs=[chatbot, radio], outputs=[chatbot]).then(bot, chatbot, chatbot)
110
 
111
+ audio.change(lambda: None, None, audio)
112
 
113
  set_model(chatbot, radio.value)
114
 
main.py CHANGED
@@ -1,10 +1,13 @@
1
- from utils import get_search_index, generate_answer, set_model_and_embeddings
2
 
3
- def index(model):
 
 
4
  set_model_and_embeddings(model)
5
  get_search_index(model)
6
  return True
7
 
8
- def run(question, model):
9
- index(model)
 
10
  return generate_answer(question)
 
1
+ from utils import get_search_index, generate_answer, set_model_and_embeddings, set_session_id
2
 
3
+
4
+ def index(model, session_id):
5
+ set_session_id(session_id)
6
  set_model_and_embeddings(model)
7
  get_search_index(model)
8
  return True
9
 
10
+
11
+ def run(question, model, session_id):
12
+ index(model, session_id)
13
  return generate_answer(question)
mapping.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FILE_URL_MAPPING = {
2
+ 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/03_design-thinking-in-action/02_multiply.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/1qm8i/multiply',
3
+ 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/03_design-thinking-in-action/01_utensil-grip-personalization.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/BYBmh/utensil-grip-personalization',
4
+ 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/03_design-thinking-in-action/03_city-x-project.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/0HDFr/city-x-project',
5
+ 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/01_module-4-information/02_an-intro-to-design-thinking.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/IdLQM/an-intro-to-design-thinking',
6
+ 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/02_an-introduction-to-design-thinking/01_what-is-design-thinking.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/npYkr/what-is-design-thinking',
7
+ 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/02_an-introduction-to-design-thinking/03_3d-printing-and-design-thinking.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/QJAGJ/3d-printing-and-design-thinking',
8
+ 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/02_an-introduction-to-design-thinking/02_understanding-user-needs.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/Jj03y/understanding-user-needs',
9
+ 'docs/3d-printing-applications/01_course-orientation/01_about-the-courses/01_welcome-to-3d-printing-applications.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/xYJax/welcome-to-3d-printing-applications',
10
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/02_whats-different-about-3d-printing/01_complexity-is-free.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/lA0z9/complexity-is-free',
11
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/02_whats-different-about-3d-printing/02_3d-printing-a-paradigm-shift.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/I3NUA/3d-printing-a-paradigm-shift',
12
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/06_optional-content-webinars-with-experts/01_webinar-3d-printing-with-soft-materials-bonus.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/PbM8Z/webinar-3d-printing-with-soft-materials-bonus',
13
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/03_3d-printing-industry-trends/02_3d-printing-use-cases.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/5dSO8/3d-printing-use-cases',
14
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/03_3d-printing-industry-trends/04_a-view-from-the-trenches.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/FAINi/a-view-from-the-trenches',
15
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/03_3d-printing-industry-trends/03_a-venture-capitalists-view-of-the-industry.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/kcSMh/a-venture-capitalists-view-of-the-industry',
16
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/03_3d-printing-industry-trends/01_a-look-into-the-past-and-the-future.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/IWgMe/a-look-into-the-past-and-the-future',
17
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/04_3d-printing-on-the-edge/01_cutting-edge-applications.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/Cy1Ef/cutting-edge-applications',
18
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/04_3d-printing-on-the-edge/02_bioprinting.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/V6WNO/bioprinting',
19
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/01_module-1-information/02_a-new-way-of-making.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/jPkoW/a-new-way-of-making',
20
+ 'docs/3d-printing-applications/06_course-wrap-up/01_course-wrap-up-whats-next/01_whats-next.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/ffuzj/whats-next',
21
+ 'docs/3d-printing-applications/06_course-wrap-up/01_course-wrap-up-whats-next/03_gies-online-programs.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/gPv5h/gies-online-programs',
22
+ 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/03_3d-printing-in-education/01_3d-printing-in-education.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/rrgbB/3d-printing-in-education',
23
+ 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/03_3d-printing-in-education/02_girls-in-stem-makergirls.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/8JKL4/girls-in-stem-makergirls',
24
+ 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/01_module-3-information/02_3d-printing-in-development-and-education.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/oncn3/3d-printing-in-development-and-education',
25
+ 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/02_3d-printing-for-development-3d4d/06_3d4d-with-techfortrade.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/aRmfA/3d4d-with-techfortrade',
26
+ 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/02_3d-printing-for-development-3d4d/03_from-trash-to-objects.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/C5Yaq/from-trash-to-objects',
27
+ 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/02_3d-printing-for-development-3d4d/04_enabling-the-future.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/PaC0r/enabling-the-future',
28
+ 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/02_3d-printing-for-development-3d4d/05_a-step-up-with-bionic-hands.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/94W3o/a-step-up-with-bionic-hands',
29
+ 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/02_3d-printing-for-development-3d4d/02_illinois-marketplace-and-maker-literacy-program.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/lE9Zj/illinois-marketplace-and-maker-literacy-program',
30
+ 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/02_3d-printing-for-development-3d4d/01_3d-printing-and-subsistence-marketplaces.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/MJPvM/3d-printing-and-subsistence-marketplaces',
31
+ 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/03_3d-printing-and-intellectual-property/01_3d-printing-and-the-future-or-demise-of-intellectual-property.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/6QRBO/3d-printing-and-the-future-or-demise-of-intellectual-property',
32
+ 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/01_module-2-information/02_on-demand-manufacturing.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/mwrCN/on-demand-manufacturing',
33
+ 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/02_everyone-can-be-a-maker/01_a-market-of-one-3d-hubs.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/SKKXa/a-market-of-one-3d-hubs',
34
+ 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/02_everyone-can-be-a-maker/06_careers-in-3d-printing.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/NBK4o/careers-in-3d-printing',
35
+ 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/02_everyone-can-be-a-maker/04_learning-by-making-and-making-for-fun-at-the-fab-lab.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/PuC0A/learning-by-making-and-making-for-fun-at-the-fab-lab',
36
+ 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/02_everyone-can-be-a-maker/05_for-businesses-and-entrepreneurs.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/I3WQW/for-businesses-and-entrepreneurs',
37
+ 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/02_everyone-can-be-a-maker/03_on-demand-and-local-techshop.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/EMRez/on-demand-and-local-techshop',
38
+ 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/02_everyone-can-be-a-maker/02_a-market-of-a-few.en.txt': 'https://www.coursera.org/learn/3d-printing-applications/lecture/4dB6p/a-market-of-a-few',
39
+ 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/01_module-4-information/03_module-4-readings_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/hnOry/module-4-readings',
40
+ 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/01_module-4-information/01_module-4-overview_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/zq4e8/module-4-overview',
41
+ 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/04_module-4-assignments/01_module-4-graded-quiz_exam.html': 'https://www.coursera.org/learn/3d-printing-applications/exam/nRxIo/module-4-graded-quiz',
42
+ 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/04_module-4-assignments/02_module-4-peer-review-assignment_peer_assignment_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/peer/EWHOn/module-4-peer-review-assignment',
43
+ 'docs/3d-printing-applications/07_Resources/04_3d-printing-news-sources/01__resources.html': 'https://www.coursera.org/learn/3d-printing-applications/resources/LcUHI',
44
+ 'docs/3d-printing-applications/07_Resources/03_3d-printing-applications/01__resources.html': 'https://www.coursera.org/learn/3d-printing-applications/resources/rLuyV',
45
+ 'docs/3d-printing-applications/07_Resources/03_3d-printing-applications/01__scientists-create-new-bio-ink-for-3d-printing-with-stem-cells-322296.html': 'http://tech.firstpost.com/news-analysis/scientists-create-new-bio-ink-for-3d-printing-with-stem-cells-322296.html',
46
+ 'docs/3d-printing-applications/07_Resources/05_recommended-books/01__resources.html': 'https://www.coursera.org/learn/3d-printing-applications/resources/8hxKz',
47
+ 'docs/3d-printing-applications/07_Resources/06_explore-the-imba/01__resources.html': 'https://www.coursera.org/learn/3d-printing-applications/resources/49U0k',
48
+ 'docs/3d-printing-applications/07_Resources/01_about-our-team/01__resources.html': 'https://www.coursera.org/learn/3d-printing-applications/resources/62fHX',
49
+ 'docs/3d-printing-applications/07_Resources/02_glossary/01__resources.html': 'https://www.coursera.org/learn/3d-printing-applications/resources/dG0g4',
50
+ 'docs/3d-printing-applications/01_course-orientation/01_about-the-courses/04_glossary_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/QQpUl/glossary',
51
+ 'docs/3d-printing-applications/01_course-orientation/01_about-the-courses/03_about-the-discussion-forums_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/MRtAV/about-the-discussion-forums',
52
+ 'docs/3d-printing-applications/01_course-orientation/01_about-the-courses/02_syllabus_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/9DenU/syllabus',
53
+ 'docs/3d-printing-applications/01_course-orientation/02_about-your-classmates/01_updating-your-profile_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/DfAqY/updating-your-profile',
54
+ 'docs/3d-printing-applications/01_course-orientation/02_about-your-classmates/02_social-media_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/Qf1Am/social-media',
55
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/06_optional-content-webinars-with-experts/04_webinar-3d-printing-comes-of-age_3d-printing-comes-of-age.html': 'http://www.3dprintingprofs.com/2016/07/3d-printing-comes-of-age-webinar/',
56
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/06_optional-content-webinars-with-experts/05_bonus-webinar-mymini-factory-ceo-interview_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/2XNdg/bonus-webinar-mymini-factory-ceo-interview',
57
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/06_optional-content-webinars-with-experts/03_bonus-webinar-whats-all-the-hype-around-3d-printing_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/jKR1S/bonus-webinar-whats-all-the-hype-around-3d-printing',
58
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/06_optional-content-webinars-with-experts/02_bonus-webinar-audio-whats-the-hype-around-3dprinting_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/Wh3Lh/bonus-webinar-audio-whats-the-hype-around-3dprinting',
59
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/06_optional-content-webinars-with-experts/06_bonus-webinar-the-maker-movement-in-education_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/hHU9F/bonus-webinar-the-maker-movement-in-education',
60
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/06_optional-content-webinars-with-experts/04_webinar-3d-printing-comes-of-age_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/zNdWR/webinar-3d-printing-comes-of-age',
61
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/05_module-1-assignments/02_module-1-peer-review-assignment_peer_assignment_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/peer/wW35G/module-1-peer-review-assignment',
62
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/05_module-1-assignments/01_module-1-graded-quiz_exam.html': 'https://www.coursera.org/learn/3d-printing-applications/exam/Mf63o/module-1-graded-quiz',
63
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/01_module-1-information/01_module-1-overview_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/wEptY/module-1-overview',
64
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/01_module-1-information/03_module-1-readings_3d-printing-complexity-is-free-may-be-costly-for-some.html': 'https://www2.deloitte.com/us/en/insights/focus/3d-opportunity/3d-printing-complexity-is-free-may-be-costly-for-some.html',
65
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/01_module-1-information/03_module-1-readings_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/SpZ2p/module-1-readings',
66
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/01_module-1-information/03_module-1-readings_3d-printing-comes-of-age.html': 'http://www.pwc.com/us/en/industrial-products/3d-printing-comes-of-age.html',
67
+ 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/01_module-1-information/03_module-1-readings_press71.html': 'http://wohlersassociates.com/press71.html',
68
+ 'docs/3d-printing-applications/06_course-wrap-up/01_course-wrap-up-whats-next/02_congratulations_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/oDj5t/congratulations',
69
+ 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/03_3d-printing-in-education/03_resources-for-educators_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/sdDp2/resources-for-educators',
70
+ 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/01_module-3-information/03_module-3-readings_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/5kR9o/module-3-readings',
71
+ 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/01_module-3-information/01_module-3-overview_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/OChGO/module-3-overview',
72
+ 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/01_module-3-information/03_module-3-readings_book.html': 'http://sdu.ictp.it/3D/book.html',
73
+ 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/04_module-3-assignments/01_module-3-graded-quiz_exam.html': 'https://www.coursera.org/learn/3d-printing-applications/exam/NMiTK/module-3-graded-quiz',
74
+ 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/04_module-3-assignments/02_module-3-peer-review-assignment_peer_assignment_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/peer/5xxgp/module-3-peer-review-assignment',
75
+ 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/04_module-2-assignment/01_module-2-graded-quiz_exam.html': 'https://www.coursera.org/learn/3d-printing-applications/exam/6Z8Ef/module-2-graded-quiz',
76
+ 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/04_module-2-assignment/02_module-2-peer-review-assignment_peer_assignment_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/peer/Pt8hO/module-2-peer-review-assignment',
77
+ 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/01_module-2-information/01_module-2-overview_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/94W7x/module-2-overview',
78
+ 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/01_module-2-information/03_module-2-readings_instructions.html': 'https://www.coursera.org/learn/3d-printing-applications/supplement/3uaqE/module-2-readings'
79
+ }
memory.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import logging
3
+ from datetime import datetime
4
+ from typing import List
5
+
6
+ from langchain.memory import MongoDBChatMessageHistory
7
+ from langchain.schema import AIMessage, BaseMessage, HumanMessage, messages_from_dict, _message_to_dict
8
+ from pymongo import errors
9
+
10
+ logger = logging.getLogger(__name__)
11
+
12
+
13
+ class CustomMongoDBChatMessageHistory(MongoDBChatMessageHistory):
14
+
15
+ @property
16
+ def messages(self) -> List[BaseMessage]: # type: ignore
17
+ """Retrieve the messages from MongoDB"""
18
+ from pymongo import errors
19
+ cursor = None
20
+ try:
21
+ cursor = self.collection.find({"SessionId": self.session_id})
22
+ except errors.OperationFailure as error:
23
+ logger.error(error)
24
+
25
+ document_count = self.collection.count_documents({"SessionId": self.session_id})
26
+
27
+ if cursor and document_count > 0:
28
+ document = cursor[0] # Get the first document with the matching session id
29
+ items = document["messages"] # Get the messages array from the document
30
+ else:
31
+ items = []
32
+
33
+ messages = messages_from_dict([json.loads(item) for item in items])
34
+ return messages
35
+
36
+ def add_user_message(self, message: str) -> None:
37
+ self.append(HumanMessage(content=message))
38
+
39
+ def add_ai_message(self, message: str) -> None:
40
+ self.append(AIMessage(content=message))
41
+
42
+ def append(self, message: BaseMessage) -> None:
43
+ """Append the message to the record in MongoDB with the desired format"""
44
+
45
+ # Determine the sender based on the message type
46
+ sender = "ai" if isinstance(message, AIMessage) else "human"
47
+
48
+ # Create the message object with the desired format
49
+ message_obj = {
50
+ "type": sender,
51
+ "content": message.content,
52
+ "timestamp": datetime.utcnow()
53
+ }
54
+
55
+ try:
56
+ # Update the messages array with the new message object
57
+ self.collection.update_one(
58
+ {"SessionId": self.session_id},
59
+ {"$push": {"messages": json.dumps(_message_to_dict(message))}},
60
+ upsert=True
61
+ )
62
+ except errors.WriteError as err:
63
+ logger.error(err)
requirements.txt CHANGED
@@ -8,4 +8,5 @@ gtts
8
  torch
9
  tiktoken
10
  huggingface-hub
11
- gradio
 
 
8
  torch
9
  tiktoken
10
  huggingface-hub
11
+ gradio
12
+ pymongo
utils.py CHANGED
@@ -1,9 +1,10 @@
1
  import os
2
  import pickle
3
- import langchain
4
 
5
  import faiss
 
6
  from langchain import HuggingFaceHub
 
7
  from langchain.chains import ConversationalRetrievalChain
8
  from langchain.chat_models import ChatOpenAI
9
  from langchain.document_loaders import DirectoryLoader, TextLoader, UnstructuredHTMLLoader
@@ -16,7 +17,9 @@ from langchain.prompts.chat import (
16
  )
17
  from langchain.text_splitter import CharacterTextSplitter
18
  from langchain.vectorstores.faiss import FAISS
19
- from langchain.cache import InMemoryCache
 
 
20
 
21
  langchain.llm_cache = InMemoryCache()
22
 
@@ -27,97 +30,21 @@ models = ["GPT-3.5", "Flan UL2", "GPT-4", "Flan T5"]
27
  pickle_file = "_vs.pkl"
28
  index_file = "_vs.index"
29
  models_folder = "models/"
 
30
 
31
  llm = ChatOpenAI(model_name="gpt-4", temperature=0.1)
32
 
33
  embeddings = OpenAIEmbeddings(model='text-embedding-ada-002')
34
 
35
- chat_history = []
 
 
 
36
 
37
  memory = ConversationBufferWindowMemory(memory_key="chat_history", k=10)
38
 
39
  vectorstore_index = None
40
 
41
- file_url_mapping = {
42
- 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/03_design-thinking-in-action/02_multiply.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/1qm8i/multiply',
43
- 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/03_design-thinking-in-action/01_utensil-grip-personalization.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/BYBmh/utensil-grip-personalization',
44
- 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/03_design-thinking-in-action/03_city-x-project.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/0HDFr/city-x-project',
45
- 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/01_module-4-information/02_an-intro-to-design-thinking.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/IdLQM/an-intro-to-design-thinking',
46
- 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/02_an-introduction-to-design-thinking/01_what-is-design-thinking.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/npYkr/what-is-design-thinking',
47
- 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/02_an-introduction-to-design-thinking/03_3d-printing-and-design-thinking.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/QJAGJ/3d-printing-and-design-thinking',
48
- 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/02_an-introduction-to-design-thinking/02_understanding-user-needs.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/Jj03y/understanding-user-needs',
49
- 'docs/3d-printing-applications/01_course-orientation/01_about-the-courses/01_welcome-to-3d-printing-applications.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/xYJax/welcome-to-3d-printing-applications',
50
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/02_whats-different-about-3d-printing/01_complexity-is-free.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/lA0z9/complexity-is-free',
51
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/02_whats-different-about-3d-printing/02_3d-printing-a-paradigm-shift.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/I3NUA/3d-printing-a-paradigm-shift',
52
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/06_optional-content-webinars-with-experts/01_webinar-3d-printing-with-soft-materials-bonus.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/PbM8Z/webinar-3d-printing-with-soft-materials-bonus',
53
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/03_3d-printing-industry-trends/02_3d-printing-use-cases.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/5dSO8/3d-printing-use-cases',
54
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/03_3d-printing-industry-trends/04_a-view-from-the-trenches.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/FAINi/a-view-from-the-trenches',
55
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/03_3d-printing-industry-trends/03_a-venture-capitalists-view-of-the-industry.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/kcSMh/a-venture-capitalists-view-of-the-industry',
56
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/03_3d-printing-industry-trends/01_a-look-into-the-past-and-the-future.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/IWgMe/a-look-into-the-past-and-the-future',
57
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/04_3d-printing-on-the-edge/01_cutting-edge-applications.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/Cy1Ef/cutting-edge-applications',
58
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/04_3d-printing-on-the-edge/02_bioprinting.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/V6WNO/bioprinting',
59
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/01_module-1-information/02_a-new-way-of-making.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/jPkoW/a-new-way-of-making',
60
- 'docs/3d-printing-applications/06_course-wrap-up/01_course-wrap-up-whats-next/01_whats-next.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/ffuzj/whats-next',
61
- 'docs/3d-printing-applications/06_course-wrap-up/01_course-wrap-up-whats-next/03_gies-online-programs.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/gPv5h/gies-online-programs',
62
- 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/03_3d-printing-in-education/01_3d-printing-in-education.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/rrgbB/3d-printing-in-education',
63
- 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/03_3d-printing-in-education/02_girls-in-stem-makergirls.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/8JKL4/girls-in-stem-makergirls',
64
- 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/01_module-3-information/02_3d-printing-in-development-and-education.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/oncn3/3d-printing-in-development-and-education',
65
- 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/02_3d-printing-for-development-3d4d/06_3d4d-with-techfortrade.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/aRmfA/3d4d-with-techfortrade',
66
- 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/02_3d-printing-for-development-3d4d/03_from-trash-to-objects.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/C5Yaq/from-trash-to-objects',
67
- 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/02_3d-printing-for-development-3d4d/04_enabling-the-future.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/PaC0r/enabling-the-future',
68
- 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/02_3d-printing-for-development-3d4d/05_a-step-up-with-bionic-hands.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/94W3o/a-step-up-with-bionic-hands',
69
- 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/02_3d-printing-for-development-3d4d/02_illinois-marketplace-and-maker-literacy-program.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/lE9Zj/illinois-marketplace-and-maker-literacy-program',
70
- 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/02_3d-printing-for-development-3d4d/01_3d-printing-and-subsistence-marketplaces.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/MJPvM/3d-printing-and-subsistence-marketplaces',
71
- 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/03_3d-printing-and-intellectual-property/01_3d-printing-and-the-future-or-demise-of-intellectual-property.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/6QRBO/3d-printing-and-the-future-or-demise-of-intellectual-property',
72
- 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/01_module-2-information/02_on-demand-manufacturing.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/mwrCN/on-demand-manufacturing',
73
- 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/02_everyone-can-be-a-maker/01_a-market-of-one-3d-hubs.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/SKKXa/a-market-of-one-3d-hubs',
74
- 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/02_everyone-can-be-a-maker/06_careers-in-3d-printing.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/NBK4o/careers-in-3d-printing',
75
- 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/02_everyone-can-be-a-maker/04_learning-by-making-and-making-for-fun-at-the-fab-lab.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/PuC0A/learning-by-making-and-making-for-fun-at-the-fab-lab',
76
- 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/02_everyone-can-be-a-maker/05_for-businesses-and-entrepreneurs.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/I3WQW/for-businesses-and-entrepreneurs',
77
- 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/02_everyone-can-be-a-maker/03_on-demand-and-local-techshop.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/EMRez/on-demand-and-local-techshop',
78
- 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/02_everyone-can-be-a-maker/02_a-market-of-a-few.en.txt':'https://www.coursera.org/learn/3d-printing-applications/lecture/4dB6p/a-market-of-a-few',
79
- 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/01_module-4-information/03_module-4-readings_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/hnOry/module-4-readings',
80
- 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/01_module-4-information/01_module-4-overview_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/zq4e8/module-4-overview',
81
- 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/04_module-4-assignments/01_module-4-graded-quiz_exam.html':'https://www.coursera.org/learn/3d-printing-applications/exam/nRxIo/module-4-graded-quiz',
82
- 'docs/3d-printing-applications/05_module-4-from-ideas-to-objects/04_module-4-assignments/02_module-4-peer-review-assignment_peer_assignment_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/peer/EWHOn/module-4-peer-review-assignment',
83
- 'docs/3d-printing-applications/07_Resources/04_3d-printing-news-sources/01__resources.html':'https://www.coursera.org/learn/3d-printing-applications/resources/LcUHI',
84
- 'docs/3d-printing-applications/07_Resources/03_3d-printing-applications/01__resources.html':'https://www.coursera.org/learn/3d-printing-applications/resources/rLuyV',
85
- 'docs/3d-printing-applications/07_Resources/03_3d-printing-applications/01__scientists-create-new-bio-ink-for-3d-printing-with-stem-cells-322296.html':'http://tech.firstpost.com/news-analysis/scientists-create-new-bio-ink-for-3d-printing-with-stem-cells-322296.html',
86
- 'docs/3d-printing-applications/07_Resources/05_recommended-books/01__resources.html':'https://www.coursera.org/learn/3d-printing-applications/resources/8hxKz',
87
- 'docs/3d-printing-applications/07_Resources/06_explore-the-imba/01__resources.html':'https://www.coursera.org/learn/3d-printing-applications/resources/49U0k',
88
- 'docs/3d-printing-applications/07_Resources/01_about-our-team/01__resources.html':'https://www.coursera.org/learn/3d-printing-applications/resources/62fHX',
89
- 'docs/3d-printing-applications/07_Resources/02_glossary/01__resources.html':'https://www.coursera.org/learn/3d-printing-applications/resources/dG0g4',
90
- 'docs/3d-printing-applications/01_course-orientation/01_about-the-courses/04_glossary_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/QQpUl/glossary',
91
- 'docs/3d-printing-applications/01_course-orientation/01_about-the-courses/03_about-the-discussion-forums_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/MRtAV/about-the-discussion-forums',
92
- 'docs/3d-printing-applications/01_course-orientation/01_about-the-courses/02_syllabus_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/9DenU/syllabus',
93
- 'docs/3d-printing-applications/01_course-orientation/02_about-your-classmates/01_updating-your-profile_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/DfAqY/updating-your-profile',
94
- 'docs/3d-printing-applications/01_course-orientation/02_about-your-classmates/02_social-media_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/Qf1Am/social-media',
95
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/06_optional-content-webinars-with-experts/04_webinar-3d-printing-comes-of-age_3d-printing-comes-of-age.html':'http://www.3dprintingprofs.com/2016/07/3d-printing-comes-of-age-webinar/',
96
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/06_optional-content-webinars-with-experts/05_bonus-webinar-mymini-factory-ceo-interview_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/2XNdg/bonus-webinar-mymini-factory-ceo-interview',
97
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/06_optional-content-webinars-with-experts/03_bonus-webinar-whats-all-the-hype-around-3d-printing_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/jKR1S/bonus-webinar-whats-all-the-hype-around-3d-printing',
98
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/06_optional-content-webinars-with-experts/02_bonus-webinar-audio-whats-the-hype-around-3dprinting_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/Wh3Lh/bonus-webinar-audio-whats-the-hype-around-3dprinting',
99
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/06_optional-content-webinars-with-experts/06_bonus-webinar-the-maker-movement-in-education_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/hHU9F/bonus-webinar-the-maker-movement-in-education',
100
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/06_optional-content-webinars-with-experts/04_webinar-3d-printing-comes-of-age_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/zNdWR/webinar-3d-printing-comes-of-age',
101
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/05_module-1-assignments/02_module-1-peer-review-assignment_peer_assignment_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/peer/wW35G/module-1-peer-review-assignment',
102
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/05_module-1-assignments/01_module-1-graded-quiz_exam.html':'https://www.coursera.org/learn/3d-printing-applications/exam/Mf63o/module-1-graded-quiz',
103
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/01_module-1-information/01_module-1-overview_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/wEptY/module-1-overview',
104
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/01_module-1-information/03_module-1-readings_3d-printing-complexity-is-free-may-be-costly-for-some.html':'https://www2.deloitte.com/us/en/insights/focus/3d-opportunity/3d-printing-complexity-is-free-may-be-costly-for-some.html',
105
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/01_module-1-information/03_module-1-readings_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/SpZ2p/module-1-readings',
106
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/01_module-1-information/03_module-1-readings_3d-printing-comes-of-age.html':'http://www.pwc.com/us/en/industrial-products/3d-printing-comes-of-age.html',
107
- 'docs/3d-printing-applications/02_module-1-3d-printing-a-new-way-of-making/01_module-1-information/03_module-1-readings_press71.html':'http://wohlersassociates.com/press71.html',
108
- 'docs/3d-printing-applications/06_course-wrap-up/01_course-wrap-up-whats-next/02_congratulations_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/oDj5t/congratulations',
109
- 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/03_3d-printing-in-education/03_resources-for-educators_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/sdDp2/resources-for-educators',
110
- 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/01_module-3-information/03_module-3-readings_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/5kR9o/module-3-readings',
111
- 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/01_module-3-information/01_module-3-overview_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/OChGO/module-3-overview',
112
- 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/01_module-3-information/03_module-3-readings_book.html':'http://sdu.ictp.it/3D/book.html',
113
- 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/04_module-3-assignments/01_module-3-graded-quiz_exam.html':'https://www.coursera.org/learn/3d-printing-applications/exam/NMiTK/module-3-graded-quiz',
114
- 'docs/3d-printing-applications/04_module-3-3d-printing-for-development-and-education/04_module-3-assignments/02_module-3-peer-review-assignment_peer_assignment_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/peer/5xxgp/module-3-peer-review-assignment',
115
- 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/04_module-2-assignment/01_module-2-graded-quiz_exam.html':'https://www.coursera.org/learn/3d-printing-applications/exam/6Z8Ef/module-2-graded-quiz',
116
- 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/04_module-2-assignment/02_module-2-peer-review-assignment_peer_assignment_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/peer/Pt8hO/module-2-peer-review-assignment',
117
- 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/01_module-2-information/01_module-2-overview_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/94W7x/module-2-overview',
118
- 'docs/3d-printing-applications/03_module-2-3d-printing-on-demand-manufacturing/01_module-2-information/03_module-2-readings_instructions.html':'https://www.coursera.org/learn/3d-printing-applications/supplement/3uaqE/module-2-readings'
119
- }
120
-
121
  system_template = """You are Coursera QA Bot. Have a conversation with a human, answering the following questions as best you can.
122
  You are a teaching assistant for a Coursera Course: 3D Printing Applications and can answer any question about that using vectorstore or context.
123
  Use the following pieces of context to answer the users question.
@@ -131,11 +58,25 @@ messages = [
131
  CHAT_PROMPT = ChatPromptTemplate.from_messages(messages)
132
 
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  def set_model_and_embeddings(model):
135
- global chat_history
136
  set_model(model)
137
  # set_embeddings(model)
138
- chat_history = []
139
 
140
 
141
  def set_model(model):
@@ -149,7 +90,7 @@ def set_model(model):
149
  llm = ChatOpenAI(model_name="gpt-4", temperature=0.1)
150
  elif model == "Flan UL2":
151
  print("Loading Flan-UL2")
152
- llm = HuggingFaceHub(repo_id="google/flan-ul2", model_kwargs={"temperature": 0.1, "max_new_tokens":500})
153
  elif model == "Flan T5":
154
  print("Loading Flan T5")
155
  llm = HuggingFaceHub(repo_id="google/flan-t5-base", model_kwargs={"temperature": 0.1})
@@ -222,7 +163,7 @@ def fetch_data_for_embeddings():
222
 
223
  # use file_url_mapping to set metadata of document to url which has been set as the source
224
  for document in document_list:
225
- document.metadata["url"] = file_url_mapping.get(document.metadata["source"])
226
  print("document list: " + str(len(document_list)))
227
  return document_list
228
 
@@ -246,7 +187,7 @@ def create_chunk_documents():
246
 
247
 
248
  def get_qa_chain(vectorstore_index):
249
- global llm, model_name
250
  print(llm)
251
 
252
  # embeddings_filter = EmbeddingsFilter(embeddings=embeddings, similarity_threshold=0.76)
@@ -268,12 +209,13 @@ def get_chat_history(inputs) -> str:
268
 
269
 
270
  def generate_answer(question) -> str:
271
- global chat_history, vectorstore_index
272
  chain = get_qa_chain(vectorstore_index)
273
-
274
  result = chain(
275
- {"question": question, "chat_history": chat_history, "vectordbkwargs": {"search_distance": 0.6}})
276
- chat_history = [(question, result["answer"])]
 
277
  sources = []
278
  print(result)
279
 
@@ -283,3 +225,9 @@ def generate_answer(question) -> str:
283
 
284
  source = ',\n'.join(set(sources))
285
  return result['answer'] + '\nSOURCES: ' + source
 
 
 
 
 
 
 
1
  import os
2
  import pickle
 
3
 
4
  import faiss
5
+ import langchain
6
  from langchain import HuggingFaceHub
7
+ from langchain.cache import InMemoryCache
8
  from langchain.chains import ConversationalRetrievalChain
9
  from langchain.chat_models import ChatOpenAI
10
  from langchain.document_loaders import DirectoryLoader, TextLoader, UnstructuredHTMLLoader
 
17
  )
18
  from langchain.text_splitter import CharacterTextSplitter
19
  from langchain.vectorstores.faiss import FAISS
20
+
21
+ from mapping import FILE_URL_MAPPING
22
+ from memory import CustomMongoDBChatMessageHistory
23
 
24
  langchain.llm_cache = InMemoryCache()
25
 
 
30
  pickle_file = "_vs.pkl"
31
  index_file = "_vs.index"
32
  models_folder = "models/"
33
+ MONGO_DB_URL = os.environ['MONGO_DB_URL']
34
 
35
  llm = ChatOpenAI(model_name="gpt-4", temperature=0.1)
36
 
37
  embeddings = OpenAIEmbeddings(model='text-embedding-ada-002')
38
 
39
+ message_history = CustomMongoDBChatMessageHistory(
40
+ connection_string=MONGO_DB_URL, session_id='session_id', database_name='coursera_bots',
41
+ collection_name='3d_printing_revolution'
42
+ )
43
 
44
  memory = ConversationBufferWindowMemory(memory_key="chat_history", k=10)
45
 
46
  vectorstore_index = None
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  system_template = """You are Coursera QA Bot. Have a conversation with a human, answering the following questions as best you can.
49
  You are a teaching assistant for a Coursera Course: 3D Printing Applications and can answer any question about that using vectorstore or context.
50
  Use the following pieces of context to answer the users question.
 
58
  CHAT_PROMPT = ChatPromptTemplate.from_messages(messages)
59
 
60
 
61
+ def set_session_id(session_id):
62
+ global message_history, memory
63
+ # check if message_history with same session id exists
64
+ if message_history.session_id == session_id:
65
+ print("Session id already set: " + str(message_history.session_id))
66
+ else:
67
+ # create new message history with session id
68
+ print("Setting session id to " + str(session_id))
69
+ message_history = CustomMongoDBChatMessageHistory(
70
+ connection_string=MONGO_DB_URL, session_id=session_id, database_name='coursera_bots',
71
+ collection_name='printing_3d_revolution'
72
+ )
73
+ memory = ConversationBufferWindowMemory(memory_key="chat_history", chat_memory=message_history, k=10,
74
+ return_messages=True)
75
+
76
+
77
  def set_model_and_embeddings(model):
 
78
  set_model(model)
79
  # set_embeddings(model)
 
80
 
81
 
82
  def set_model(model):
 
90
  llm = ChatOpenAI(model_name="gpt-4", temperature=0.1)
91
  elif model == "Flan UL2":
92
  print("Loading Flan-UL2")
93
+ llm = HuggingFaceHub(repo_id="google/flan-ul2", model_kwargs={"temperature": 0.1, "max_new_tokens": 500})
94
  elif model == "Flan T5":
95
  print("Loading Flan T5")
96
  llm = HuggingFaceHub(repo_id="google/flan-t5-base", model_kwargs={"temperature": 0.1})
 
163
 
164
  # use file_url_mapping to set metadata of document to url which has been set as the source
165
  for document in document_list:
166
+ document.metadata["url"] = FILE_URL_MAPPING.get(document.metadata["source"])
167
  print("document list: " + str(len(document_list)))
168
  return document_list
169
 
 
187
 
188
 
189
  def get_qa_chain(vectorstore_index):
190
+ global llm
191
  print(llm)
192
 
193
  # embeddings_filter = EmbeddingsFilter(embeddings=embeddings, similarity_threshold=0.76)
 
209
 
210
 
211
  def generate_answer(question) -> str:
212
+ global vectorstore_index
213
  chain = get_qa_chain(vectorstore_index)
214
+ history = memory.chat_memory.messages
215
  result = chain(
216
+ {"question": question, "chat_history": history})
217
+
218
+ save_chat_history(question, result)
219
  sources = []
220
  print(result)
221
 
 
225
 
226
  source = ',\n'.join(set(sources))
227
  return result['answer'] + '\nSOURCES: ' + source
228
+
229
+
230
+ def save_chat_history(question, result):
231
+ memory.chat_memory.add_user_message(question)
232
+ memory.chat_memory.add_ai_message(result["answer"])
233
+ print("chat history after saving: " + str(memory.chat_memory.messages))