├── pdf
├── test.md
├── Lec 1.pdf
├── Lec 7.pdf
├── Lec 8.pdf
├── Lec 2-3.pdf
├── Lec 4-6.pdf
├── courseOutline.pdf
└── sample_midterm.pdf
├── packages.txt
├── logo.png
├── method.png
├── sb_logo.png
├── web_app_view.png
├── requirements.txt
├── LICENSE
├── api_handler.py
├── file_upload.py
├── chat_gen.py
├── README.md
└── app.py
/pdf/test.md:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/packages.txt:
--------------------------------------------------------------------------------
1 | wkhtmltopdf
2 |
--------------------------------------------------------------------------------
/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/098765d/AI_Tutor/HEAD/logo.png
--------------------------------------------------------------------------------
/method.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/098765d/AI_Tutor/HEAD/method.png
--------------------------------------------------------------------------------
/pdf/Lec 1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/098765d/AI_Tutor/HEAD/pdf/Lec 1.pdf
--------------------------------------------------------------------------------
/pdf/Lec 7.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/098765d/AI_Tutor/HEAD/pdf/Lec 7.pdf
--------------------------------------------------------------------------------
/pdf/Lec 8.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/098765d/AI_Tutor/HEAD/pdf/Lec 8.pdf
--------------------------------------------------------------------------------
/sb_logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/098765d/AI_Tutor/HEAD/sb_logo.png
--------------------------------------------------------------------------------
/pdf/Lec 2-3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/098765d/AI_Tutor/HEAD/pdf/Lec 2-3.pdf
--------------------------------------------------------------------------------
/pdf/Lec 4-6.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/098765d/AI_Tutor/HEAD/pdf/Lec 4-6.pdf
--------------------------------------------------------------------------------
/web_app_view.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/098765d/AI_Tutor/HEAD/web_app_view.png
--------------------------------------------------------------------------------
/pdf/courseOutline.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/098765d/AI_Tutor/HEAD/pdf/courseOutline.pdf
--------------------------------------------------------------------------------
/pdf/sample_midterm.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/098765d/AI_Tutor/HEAD/pdf/sample_midterm.pdf
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | streamlit
2 | openai
3 | fpdf2
4 | jinja2
5 | markdown2
6 | pdfkit
7 | wkhtmltopdf
8 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 dd
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/api_handler.py:
--------------------------------------------------------------------------------
1 | from openai import OpenAI
2 | import time
3 |
4 |
5 | def send_query_get_response(client,user_question,assistant_id):
6 | # Create a new thread for the query
7 | thread = client.beta.threads.create()
8 | thread_id = thread.id
9 |
10 | user_question=user_question+ ' and tell me which file are the top results based on your similarity search (an example of can be you can refer to the course material titled "Present Value Relations" in the file "Lec 2-3.pdf" under slides 20-34.)'
11 |
12 | # Send the user's question to the thread
13 | client.beta.threads.messages.create(
14 | thread_id=thread_id,
15 | role="user",
16 | content=user_question,
17 | )
18 |
19 | # Create and start a run for the thread
20 | run = client.beta.threads.runs.create(
21 | thread_id=thread_id,
22 | assistant_id=assistant_id,
23 | )
24 |
25 | # Initialize a timer to limit the response wait time
26 | start_time = time.time()
27 |
28 | # Continuously check the run status
29 | run_status = None
30 | while run_status != 'completed':
31 | run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)
32 | run_status = run.status
33 |
34 | # Time check to break the loop if it runs more than 60 seconds
35 | if time.time() - start_time > 60:
36 | print("Final run status:", run_status)
37 | print("Took too long time")
38 | break
39 |
40 | # Fetch and print the entire conversation history
41 | messages = client.beta.threads.messages.list(
42 | thread_id=thread_id,
43 | order='asc'
44 | )
45 |
46 | # Return the last response from the thread
47 | message=messages.data[-1]
48 | message_content=message.content[0].text
49 | response=message_content.value
50 |
51 | return response if messages.data else "Server issue, try again"
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/file_upload.py:
--------------------------------------------------------------------------------
1 | import openai
2 | import streamlit as st
3 | import os
4 |
5 | def upload_files_to_assistant(client, uploaded_files):
6 | file_ids = []
7 | file_paths=[]
8 | for uploaded_file in uploaded_files:
9 | if uploaded_file is not None:
10 | file_path=os.path.join(os.getcwd(),uploaded_file.name)
11 | response = client.files.create(
12 | file=uploaded_file,
13 | purpose='assistants'
14 | )
15 | file_ids.append(response.id)
16 | file_paths.append(file_path)
17 | print(file_paths)
18 | return file_ids,file_paths
19 |
20 | def attach_files_to_assistant(client, file_ids, assistant_id):
21 | attached_files = []
22 | for file_id in file_ids:
23 | assistant_file = client.beta.assistants.files.create(
24 | assistant_id=assistant_id,
25 | file_id=file_id
26 | )
27 | attached_files.append(assistant_file)
28 | return attached_files
29 |
30 |
31 | def check_and_upload_files(client, assistant_id):
32 |
33 | assistant_files = client.beta.assistants.files.list(assistant_id=assistant_id)
34 | files_info = [file.id for file in assistant_files.data]
35 | if not files_info:
36 | st.warning("No Files Included, Upload Educational Material")
37 | uploaded_files = st.file_uploader("Choose PDF files", type="pdf", accept_multiple_files=True)
38 |
39 | if st.button("Upload and Attach Files"):
40 | if uploaded_files:
41 | try:
42 | file_ids ,file_paths= upload_files_to_assistant(client, uploaded_files)
43 |
44 | attached_files_info = attach_files_to_assistant(client, file_ids, assistant_id)
45 |
46 | if not attached_files_info:
47 | st.warning("No files were attached. Loading default data...")
48 | else:
49 | st.success(f"{len(attached_files_info)} files successfully attached.")
50 | except Exception as e:
51 | st.error(f"An error occurred while attaching files: {e}")
52 | else:
53 | st.warning("Please select at least one file to upload.")
54 |
55 | return files_info
--------------------------------------------------------------------------------
/chat_gen.py:
--------------------------------------------------------------------------------
1 |
2 | import io
3 | from datetime import datetime
4 | import markdown2 # assuming markdown2 is installed
5 |
6 | def generate_html(messages):
7 | timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
8 | chat_content = ""
9 | interaction_count = 0
10 | user_emoji = "User" # Emoji for user
11 | assistant_emoji = "AI-Tutor" # Emoji for assistant
12 |
13 | for message in messages:
14 | role_class = 'user' if message['role'] == 'user' else 'assistant'
15 | emoji = user_emoji if role_class == 'user' else assistant_emoji
16 | if role_class == 'user':
17 | interaction_count += 1
18 |
19 | formatted_content = markdown2.markdown(message['content']).replace('\\n', '
')
20 | chat_content += f"
5 |
39 |