Spaces:
Running
Running
Upload 25 files
Browse files- data/image/freepik-export-20240425023906eVmL.jpeg +0 -0
- data/output.md +0 -0
- data/parsed_data.pkl +3 -0
- media/ouput_file.mp3 +1 -0
- media/recorded.mp3 +0 -0
- src/OCR.py +85 -0
- src/__pycache__/OCR.cpython-310.pyc +0 -0
- src/__pycache__/about.cpython-310.pyc +0 -0
- src/__pycache__/audio.cpython-310.pyc +0 -0
- src/__pycache__/chat.cpython-310.pyc +0 -0
- src/__pycache__/pdf.cpython-310.pyc +0 -0
- src/__pycache__/pdf_up.cpython-310.pyc +0 -0
- src/about.py +29 -0
- src/audio.py +72 -0
- src/chainlit.md +27 -0
- src/chat.py +76 -0
- src/pdf_up.py +41 -0
- utils/__pycache__/ingest1.cpython-310.pyc +0 -0
- utils/__pycache__/qa.cpython-310.pyc +0 -0
- utils/__pycache__/stt.cpython-310.pyc +0 -0
- utils/__pycache__/tts.cpython-310.pyc +0 -0
- utils/ingest1.py +107 -0
- utils/qa.py +87 -0
- utils/stt.py +45 -0
- utils/tts.py +39 -0
data/image/freepik-export-20240425023906eVmL.jpeg
ADDED
![]() |
data/output.md
ADDED
The diff for this file is too large to render.
See raw diff
|
|
data/parsed_data.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f0a86749f9c9e30411fdd0846439e91c7e23eb925cedd20c715f2200a7f4184d
|
3 |
+
size 850770
|
media/ouput_file.mp3
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
|
media/recorded.mp3
ADDED
Binary file (246 kB). View file
|
|
src/OCR.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import easyocr
|
4 |
+
import streamlit as st
|
5 |
+
from PIL import Image
|
6 |
+
import cv2
|
7 |
+
from utils.qa import chain
|
8 |
+
from langchain.memory import ConversationBufferWindowMemory
|
9 |
+
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
|
10 |
+
|
11 |
+
def I_OCR():
|
12 |
+
# Function to display the OCR image with bounding boxes and text
|
13 |
+
def display_ocr_image(img, results):
|
14 |
+
img_np = np.array(img)
|
15 |
+
for detection in results:
|
16 |
+
top_left = tuple([int(val) for val in detection[0][0]])
|
17 |
+
bottom_right = tuple([int(val) for val in detection[0][2]])
|
18 |
+
text = detection[1]
|
19 |
+
font = cv2.FONT_HERSHEY_COMPLEX
|
20 |
+
cv2.rectangle(img_np, top_left, bottom_right, (0, 255, 0), 5)
|
21 |
+
cv2.putText(img_np, text, top_left, font, 1, (125, 29, 241), 2, cv2.LINE_AA)
|
22 |
+
st.image(img_np, channels="BGR", use_column_width=True)
|
23 |
+
|
24 |
+
# Function to extract text from DataFrame column
|
25 |
+
def extracted_text(col):
|
26 |
+
return " , ".join(img_df[col])
|
27 |
+
|
28 |
+
# Function to initialize session state
|
29 |
+
def initialize_session_state():
|
30 |
+
if "messages" not in st.session_state:
|
31 |
+
st.session_state.messages = [
|
32 |
+
{"role": "assistant", "content": "Hi! How may I assist you today?"}
|
33 |
+
]
|
34 |
+
|
35 |
+
# Function to get answer from QA model
|
36 |
+
def get_answer(query):
|
37 |
+
response = chain.invoke(query)
|
38 |
+
return response["result"]
|
39 |
+
|
40 |
+
# Streamlit app
|
41 |
+
st.title("Question in image")
|
42 |
+
|
43 |
+
file = st.file_uploader(label= "Upload Image Here (png/jpg/jpeg) : ", type=['png', 'jpg', 'jpeg'])
|
44 |
+
|
45 |
+
if file is not None:
|
46 |
+
image = Image.open(file)
|
47 |
+
st.image(image)
|
48 |
+
|
49 |
+
reader = easyocr.Reader(['en', 'hi'], gpu=False)
|
50 |
+
results = reader.readtext(np.array(image))
|
51 |
+
|
52 |
+
img_df = pd.DataFrame(results, columns=['bbox', 'Predicted Text', 'Prediction Confidence'])
|
53 |
+
|
54 |
+
text_combined = extracted_text(col='Predicted Text')
|
55 |
+
st.write("Text Generated :- ", text_combined)
|
56 |
+
|
57 |
+
display_ocr_image(image, results)
|
58 |
+
|
59 |
+
else:
|
60 |
+
st.warning("!! Please Upload your image !!")
|
61 |
+
|
62 |
+
initialize_session_state()
|
63 |
+
|
64 |
+
memory_storage = StreamlitChatMessageHistory(key="chat_messages")
|
65 |
+
memory = ConversationBufferWindowMemory(memory_key="chat_history", human_prefix="User", chat_memory=memory_storage, k=3)
|
66 |
+
|
67 |
+
for i, msg in enumerate(memory_storage.messages):
|
68 |
+
name = "user" if i % 2 == 0 else "assistant"
|
69 |
+
st.chat_message(name).markdown(msg.content)
|
70 |
+
|
71 |
+
if user_input := st.chat_input("User Input"):
|
72 |
+
with st.chat_message("user"):
|
73 |
+
st.markdown(user_input)
|
74 |
+
|
75 |
+
with st.spinner("Generating Response..."):
|
76 |
+
with st.chat_message("assistant"):
|
77 |
+
response = get_answer(user_input)
|
78 |
+
answer = response
|
79 |
+
st.markdown(answer)
|
80 |
+
|
81 |
+
#if st.sidebar.button("Clear Chat History"):
|
82 |
+
# memory_storage.clear()
|
83 |
+
|
84 |
+
# Run the OCR function
|
85 |
+
#OCR()
|
src/__pycache__/OCR.cpython-310.pyc
ADDED
Binary file (3.16 kB). View file
|
|
src/__pycache__/about.cpython-310.pyc
ADDED
Binary file (2.48 kB). View file
|
|
src/__pycache__/audio.cpython-310.pyc
ADDED
Binary file (2.42 kB). View file
|
|
src/__pycache__/chat.cpython-310.pyc
ADDED
Binary file (2.81 kB). View file
|
|
src/__pycache__/pdf.cpython-310.pyc
ADDED
Binary file (1.5 kB). View file
|
|
src/__pycache__/pdf_up.cpython-310.pyc
ADDED
Binary file (1.1 kB). View file
|
|
src/about.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
virtual_tutor_markdown = """
|
2 |
+
# Virtual Tutor Project
|
3 |
+
|
4 |
+
Welcome to the Virtual Tutor Project! Our mission is to revolutionize education through personalized and interactive virtual tutoring experiences.
|
5 |
+
|
6 |
+
## Introduction
|
7 |
+
The Virtual Tutor Project utilizes cutting-edge technology, including Large Language Models (LLMs), to create a dynamic learning environment tailored to each student's needs.
|
8 |
+
|
9 |
+
## Key Features
|
10 |
+
- **Personalized Learning:** Our virtual tutor adapts to the individual learning pace and style of each student, providing customized learning experiences.
|
11 |
+
- **Interactive Sessions:** Engaging lessons with interactive elements such as quizzes, games, and simulations enhance learning retention.
|
12 |
+
- **Real-time Feedback:** Immediate feedback on assignments and assessments helps students track their progress and identify areas for improvement.
|
13 |
+
- **24/7 Availability:** Accessible anytime, anywhere, our virtual tutor ensures learning continuity and flexibility.
|
14 |
+
- **Comprehensive Subjects:** Covering a wide range of subjects and topics, from mathematics and science to languages and humanities, catering to diverse educational needs.
|
15 |
+
- **OCR for Questionnaires:** Utilize Optical Character Recognition (OCR) technology to facilitate the processing of questionnaires for improved assessment and feedback.
|
16 |
+
- **Audio-to-Audio Query Window:** Incorporate an audio-to-audio query window feature, enabling students to ask questions verbally and receive audio responses from the virtual tutor.
|
17 |
+
|
18 |
+
## Benefits
|
19 |
+
- **Enhanced Learning Outcomes:** Personalized learning experiences foster deeper understanding and improved academic performance.
|
20 |
+
- **Convenience and Flexibility:** Students can learn at their own pace and schedule, eliminating barriers to education.
|
21 |
+
- **Engagement and Motivation:** Interactive lessons and real-time feedback keep students engaged and motivated to learn.
|
22 |
+
- **Accessibility:** The virtual tutor provides access to quality education to students worldwide, regardless of geographical location or socioeconomic background.
|
23 |
+
|
24 |
+
## Get in Touch
|
25 |
+
Have questions or feedback? Feel free to contact us at [virtual-tutor@vt.com](mailto:naresh.is21@bmsce.ac.in).
|
26 |
+
|
27 |
+
## Join the Virtual Tutor Revolution!
|
28 |
+
Experience the future of education with our Virtual Tutor Project. Start your journey to academic success today!
|
29 |
+
"""
|
src/audio.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import base64
|
3 |
+
from audio_recorder_streamlit import audio_recorder
|
4 |
+
from streamlit_float import *
|
5 |
+
from utils.stt import speech_to_text
|
6 |
+
from utils.tts import text_to_speech
|
7 |
+
from utils.qa import chain
|
8 |
+
|
9 |
+
recorded_audio = r".\media\recorded.mp3"
|
10 |
+
output_audio = r".\media\ouput_file.mp3"
|
11 |
+
|
12 |
+
def autoplay_audio(file_path: str):
|
13 |
+
with open(file_path, "rb") as f:
|
14 |
+
data = f.read()
|
15 |
+
b64 = base64.b64encode(data).decode("utf-8")
|
16 |
+
md = f"""
|
17 |
+
<audio autoplay>
|
18 |
+
<source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
|
19 |
+
</audio>
|
20 |
+
"""
|
21 |
+
st.markdown(md, unsafe_allow_html=True)
|
22 |
+
|
23 |
+
def get_answer(query):
|
24 |
+
response = chain.invoke(query)
|
25 |
+
return response['result']
|
26 |
+
|
27 |
+
def audio_d():
|
28 |
+
float_init()
|
29 |
+
|
30 |
+
st.title("Ai Doubt resolver")
|
31 |
+
|
32 |
+
# Initialize session state
|
33 |
+
if "messages" not in st.session_state:
|
34 |
+
st.session_state.messages = [
|
35 |
+
{"role": "assistant", "content": "Hi! How may I assist you today?"}
|
36 |
+
]
|
37 |
+
|
38 |
+
footer_container = st.container()
|
39 |
+
|
40 |
+
with footer_container:
|
41 |
+
audio_bytes = audio_recorder()
|
42 |
+
|
43 |
+
for message in st.session_state.messages:
|
44 |
+
with st.chat_message(message["role"]):
|
45 |
+
st.write(message["content"])
|
46 |
+
|
47 |
+
if audio_bytes:
|
48 |
+
with st.spinner("Transcribing..."):
|
49 |
+
webm_file_path = recorded_audio
|
50 |
+
with open(webm_file_path, "wb") as f:
|
51 |
+
f.write(audio_bytes)
|
52 |
+
|
53 |
+
transcript = speech_to_text()
|
54 |
+
if transcript:
|
55 |
+
st.session_state.messages.append({"role": "user", "content": transcript})
|
56 |
+
with st.chat_message("user"):
|
57 |
+
st.write(transcript)
|
58 |
+
|
59 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
60 |
+
with st.chat_message("assistant"):
|
61 |
+
with st.spinner("Thinking🤔..."):
|
62 |
+
final_response = get_answer(str(st.session_state.messages))
|
63 |
+
with st.spinner("Generating audio response..."):
|
64 |
+
text_to_speech(final_response)
|
65 |
+
audio_file = output_audio
|
66 |
+
autoplay_audio(audio_file)
|
67 |
+
st.write(final_response)
|
68 |
+
st.session_state.messages.append({"role": "assistant", "content": final_response})
|
69 |
+
os.remove(audio_file)
|
70 |
+
|
71 |
+
# Float the footer container and provide CSS to target it with
|
72 |
+
footer_container.float("bottom: 0rem;")
|
src/chainlit.md
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Virtual Tutor Project
|
2 |
+
|
3 |
+
Welcome to the Virtual Tutor Project! Our mission is to revolutionize education through personalized and interactive virtual tutoring experiences.
|
4 |
+
|
5 |
+
## Introduction
|
6 |
+
The Virtual Tutor Project utilizes cutting-edge technology, including Large Language Models (LLMs), to create a dynamic learning environment tailored to each student's needs.
|
7 |
+
|
8 |
+
## Key Features
|
9 |
+
- **Personalized Learning:** Our virtual tutor adapts to the individual learning pace and style of each student, providing customized learning experiences.
|
10 |
+
- **Interactive Sessions:** Engaging lessons with interactive elements such as quizzes, games, and simulations enhance learning retention.
|
11 |
+
- **Real-time Feedback:** Immediate feedback on assignments and assessments helps students track their progress and identify areas for improvement.
|
12 |
+
- **24/7 Availability:** Accessible anytime, anywhere, our virtual tutor ensures learning continuity and flexibility.
|
13 |
+
- **Comprehensive Subjects:** Covering a wide range of subjects and topics, from mathematics and science to languages and humanities, catering to diverse educational needs.
|
14 |
+
- **OCR for Questionnaires:** Utilize Optical Character Recognition (OCR) technology to facilitate the processing of questionnaires for improved assessment and feedback.
|
15 |
+
- **Audio-to-Audio Query Window:** Incorporate an audio-to-audio query window feature, enabling students to ask questions verbally and receive audio responses from the virtual tutor.
|
16 |
+
|
17 |
+
## Benefits
|
18 |
+
- **Enhanced Learning Outcomes:** Personalized learning experiences foster deeper understanding and improved academic performance.
|
19 |
+
- **Convenience and Flexibility:** Students can learn at their own pace and schedule, eliminating barriers to education.
|
20 |
+
- **Engagement and Motivation:** Interactive lessons and real-time feedback keep students engaged and motivated to learn.
|
21 |
+
- **Accessibility:** The virtual tutor provides access to quality education to students worldwide, regardless of geographical location or socioeconomic background.
|
22 |
+
|
23 |
+
## Get in Touch
|
24 |
+
Have questions or feedback? Feel free to contact us at [naresh.is21@bmsce.ac.in](mailto:naresh.is21@bmsce.ac.in).
|
25 |
+
|
26 |
+
## Join the Virtual Tutor Revolution!
|
27 |
+
Experience the future of education with our Virtual Tutor Project. Start your journey to academic success today!
|
src/chat.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import base64
|
3 |
+
from utils.qa import chain
|
4 |
+
from langchain.memory import ConversationBufferWindowMemory
|
5 |
+
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
|
6 |
+
|
7 |
+
def virtual_tutor():
|
8 |
+
#st.set_page_config(layout='wide')
|
9 |
+
#st.set_page_config(page_title="Virtual Tutor")
|
10 |
+
|
11 |
+
st.markdown("""
|
12 |
+
<svg width="600" height="100">
|
13 |
+
<text x="50%" y="50%" font-family="San serif" font-size="42px" fill="Black" text-anchor="middle" stroke="white"
|
14 |
+
stroke-width="0.3" stroke-linejoin="round">Virtual Tutor - CHAT
|
15 |
+
</text>
|
16 |
+
</svg>
|
17 |
+
""", unsafe_allow_html=True)
|
18 |
+
|
19 |
+
def add_bg_from_local(image_file):
|
20 |
+
with open(image_file, "rb") as image_file:
|
21 |
+
encoded_string = base64.b64encode(image_file.read())
|
22 |
+
st.markdown(
|
23 |
+
f"""<style>.stApp {{background-image: url(data:image/{"png"};base64,{encoded_string.decode()});
|
24 |
+
background-size: cover}}</style>""",
|
25 |
+
unsafe_allow_html=True)
|
26 |
+
|
27 |
+
#add_bg_from_local(r'C:\Users\Naresh Kumar Lahajal\Desktop\Capstone-streamlit\STREAMCHAT\freepik-export-20240425023906eVmL.jpeg')
|
28 |
+
|
29 |
+
def initialize_session_state():
|
30 |
+
if "messages" not in st.session_state:
|
31 |
+
st.session_state.messages = [
|
32 |
+
{"role": "assistant", "content": "Hi! How may I assist you today?"}
|
33 |
+
]
|
34 |
+
|
35 |
+
initialize_session_state()
|
36 |
+
|
37 |
+
m = st.markdown("""
|
38 |
+
<style>
|
39 |
+
.stChatInputContainer > div {
|
40 |
+
background-color: #000000;
|
41 |
+
}
|
42 |
+
</style>
|
43 |
+
""", unsafe_allow_html=True)
|
44 |
+
|
45 |
+
def get_answer(query):
|
46 |
+
response = chain.invoke(query)
|
47 |
+
return response
|
48 |
+
|
49 |
+
memory_storage = StreamlitChatMessageHistory(key="chat_messages")
|
50 |
+
memory = ConversationBufferWindowMemory(memory_key="chat_history", human_prefix="User", chat_memory=memory_storage, k=3)
|
51 |
+
|
52 |
+
for message in st.session_state.messages: # Display the prior chat messages
|
53 |
+
with st.chat_message(message["role"]):
|
54 |
+
st.write(message["content"])
|
55 |
+
|
56 |
+
for i, msg in enumerate(memory_storage.messages):
|
57 |
+
name = "user" if i % 2 == 0 else "assistant"
|
58 |
+
st.chat_message(name).markdown(msg.content)
|
59 |
+
|
60 |
+
if user_input := st.chat_input("User Input"):
|
61 |
+
with st.chat_message("user"):
|
62 |
+
st.markdown(user_input)
|
63 |
+
|
64 |
+
with st.spinner("Generating Response..."):
|
65 |
+
|
66 |
+
with st.chat_message("assistant"):
|
67 |
+
response = get_answer(user_input)
|
68 |
+
answer = response['result']
|
69 |
+
st.markdown(answer)
|
70 |
+
message = {"role": "assistant", "content": answer}
|
71 |
+
message_u = {"role": "user", "content": user_input}
|
72 |
+
st.session_state.messages.append(message_u)
|
73 |
+
st.session_state.messages.append(message)
|
74 |
+
|
75 |
+
|
76 |
+
#virtual_tutor()
|
src/pdf_up.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tempfile
|
2 |
+
import streamlit as st
|
3 |
+
from PIL import Image
|
4 |
+
import os
|
5 |
+
from utils.ingest1 import create_vector_database
|
6 |
+
|
7 |
+
def process_uploaded_file():
|
8 |
+
st.title("Upload File to Chat")
|
9 |
+
uploaded_file = st.file_uploader("File upload", type="pdf")
|
10 |
+
if uploaded_file:
|
11 |
+
temp_dir = tempfile.mkdtemp()
|
12 |
+
path = os.path.join(temp_dir, uploaded_file.name)
|
13 |
+
#with open(path, "wb") as f:
|
14 |
+
# f.write(uploaded_file.getvalue())
|
15 |
+
print(path)
|
16 |
+
st.write("Document uploaded successfully!")
|
17 |
+
# Display the uploaded document
|
18 |
+
st.write("Preview of the document:")
|
19 |
+
st.write(uploaded_file)
|
20 |
+
|
21 |
+
# Button to start parsing and vector database creation
|
22 |
+
if st.button("Start Processing"):
|
23 |
+
# Placeholder for processing logic
|
24 |
+
st.write("Processing...")
|
25 |
+
|
26 |
+
# Placeholder for progress bar
|
27 |
+
with st.spinner('Processing...'):
|
28 |
+
# Call your function to parse data and create vector database
|
29 |
+
create_vector_database(path)
|
30 |
+
|
31 |
+
st.success("Processing completed!")
|
32 |
+
|
33 |
+
# Display success message
|
34 |
+
st.write("Vector database created successfully!")
|
35 |
+
|
36 |
+
# Show success image
|
37 |
+
success_image = Image.open("success_image.jpg")
|
38 |
+
st.image(success_image, caption="Success!", use_column_width=True)
|
39 |
+
|
40 |
+
# Add a footer
|
41 |
+
#st.text("Built with Streamlit")
|
utils/__pycache__/ingest1.cpython-310.pyc
ADDED
Binary file (2.71 kB). View file
|
|
utils/__pycache__/qa.cpython-310.pyc
ADDED
Binary file (2.2 kB). View file
|
|
utils/__pycache__/stt.cpython-310.pyc
ADDED
Binary file (1.01 kB). View file
|
|
utils/__pycache__/tts.cpython-310.pyc
ADDED
Binary file (944 Bytes). View file
|
|
utils/ingest1.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import nest_asyncio # noqa: E402
|
3 |
+
nest_asyncio.apply()
|
4 |
+
|
5 |
+
# bring in our LLAMA_CLOUD_API_KEY
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
##### LLAMAPARSE #####
|
10 |
+
from llama_parse import LlamaParse
|
11 |
+
|
12 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
13 |
+
from langchain_community.embeddings.fastembed import FastEmbedEmbeddings
|
14 |
+
from langchain_community.vectorstores import Qdrant
|
15 |
+
from langchain_community.document_loaders import DirectoryLoader
|
16 |
+
|
17 |
+
|
18 |
+
llamaparse_api_key = os.getenv("LLAMA_CLOUD_API_KEY")
|
19 |
+
qdrant_url = os.getenv("QDRANT_URL")
|
20 |
+
qdrant_api_key = os.getenv("QDRANT_API_KEY")
|
21 |
+
|
22 |
+
#to_parse_documents = ["./data/example.pdf", "./data/uber_10q_march_2022.pdf"]
|
23 |
+
|
24 |
+
parsed_data_file = r".\data\parsed_data.pkl"
|
25 |
+
output_md = r".\data\output.md"
|
26 |
+
loki = r".\data"
|
27 |
+
|
28 |
+
import pickle
|
29 |
+
# Define a function to load parsed data if available, or parse if not
|
30 |
+
def load_or_parse_data(loc):
|
31 |
+
data_file = parsed_data_file
|
32 |
+
|
33 |
+
if os.path.exists(data_file):
|
34 |
+
# Load the parsed data from the file
|
35 |
+
with open(data_file, "rb") as f:
|
36 |
+
parsed_data = pickle.load(f)
|
37 |
+
else:
|
38 |
+
# Perform the parsing step and store the result in llama_parse_documents
|
39 |
+
parsingInstructiontest10k = """The provided document is an entry level machine learning textbook with example code and outputs.
|
40 |
+
It contains many images and tables.
|
41 |
+
Try to be precise while answering the questions"""
|
42 |
+
parser = LlamaParse(api_key=llamaparse_api_key, result_type="markdown", parsing_instruction=parsingInstructiontest10k)
|
43 |
+
llama_parse_documents = parser.load_data(loc)
|
44 |
+
|
45 |
+
|
46 |
+
# Save the parsed data to a file
|
47 |
+
with open(data_file, "wb") as f:
|
48 |
+
pickle.dump(llama_parse_documents, f)
|
49 |
+
|
50 |
+
# Set the parsed data to the variable
|
51 |
+
parsed_data = llama_parse_documents
|
52 |
+
|
53 |
+
return parsed_data
|
54 |
+
|
55 |
+
|
56 |
+
# Create vector database
|
57 |
+
def create_vector_database(loc):
|
58 |
+
"""
|
59 |
+
Creates a vector database using document loaders and embeddings.
|
60 |
+
|
61 |
+
This function loads urls,
|
62 |
+
splits the loaded documents into chunks, transforms them into embeddings using OllamaEmbeddings,
|
63 |
+
and finally persists the embeddings into a Chroma vector database.
|
64 |
+
|
65 |
+
"""
|
66 |
+
# Call the function to either load or parse the data
|
67 |
+
llama_parse_documents = load_or_parse_data(loc)
|
68 |
+
#print(llama_parse_documents[1].text[:100])
|
69 |
+
|
70 |
+
#with open('data/output.md', 'a') as f: # Open the file in append mode ('a')
|
71 |
+
# for doc in llama_parse_documents:
|
72 |
+
# f.write(doc.text + '\n')
|
73 |
+
with open(output_md, 'a', encoding='utf-8') as f: # Open the file in append mode ('a')
|
74 |
+
for doc in llama_parse_documents:
|
75 |
+
f.write(doc.text + '\n')
|
76 |
+
|
77 |
+
loader = DirectoryLoader(loki, glob="**/*.md", show_progress=True)
|
78 |
+
documents = loader.load()
|
79 |
+
# Split loaded documents into chunks
|
80 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=100)
|
81 |
+
docs = text_splitter.split_documents(documents)
|
82 |
+
|
83 |
+
#len(docs)
|
84 |
+
#docs[0]
|
85 |
+
|
86 |
+
# Initialize Embeddings
|
87 |
+
embeddings = FastEmbedEmbeddings()
|
88 |
+
|
89 |
+
# Create and persist a Chroma vector database from the chunked documents
|
90 |
+
qdrant = Qdrant.from_documents(
|
91 |
+
documents=docs,
|
92 |
+
embedding=embeddings,
|
93 |
+
url=qdrant_url,
|
94 |
+
collection_name="rag",
|
95 |
+
api_key=qdrant_api_key
|
96 |
+
)
|
97 |
+
|
98 |
+
#query it
|
99 |
+
#query = "what is the agend of Financial Statements for 2022 ?"
|
100 |
+
#found_doc = qdrant.similarity_search(query, k=3)
|
101 |
+
#print(found_doc[0][:100])
|
102 |
+
|
103 |
+
print('Vector DB created successfully !')
|
104 |
+
|
105 |
+
|
106 |
+
if __name__ == "__main__":
|
107 |
+
create_vector_database()
|
utils/qa.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from typing import List
|
3 |
+
from langchain_groq import ChatGroq
|
4 |
+
from langchain.prompts import PromptTemplate
|
5 |
+
from langchain_community.vectorstores import Qdrant
|
6 |
+
from langchain_community.embeddings.fastembed import FastEmbedEmbeddings
|
7 |
+
from qdrant_client import QdrantClient
|
8 |
+
#from langchain_community.chat_models import ChatOllama
|
9 |
+
|
10 |
+
|
11 |
+
#import chainlit as cl
|
12 |
+
from langchain.chains import RetrievalQA
|
13 |
+
|
14 |
+
# bring in our GROQ_API_KEY
|
15 |
+
from dotenv import load_dotenv
|
16 |
+
load_dotenv()
|
17 |
+
|
18 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
19 |
+
qdrant_url = os.getenv("QDRANT_URL")
|
20 |
+
qdrant_api_key = os.getenv("QDRANT_API_KEY")
|
21 |
+
|
22 |
+
custom_prompt_template = """Use the following pieces of information to answer the user's question.
|
23 |
+
If you don't know the answer, just say that you don't know,if it is out of context say that it is out of context and also try to provide the answer and don't be rude.
|
24 |
+
|
25 |
+
Context: {context}
|
26 |
+
Question: {question}
|
27 |
+
|
28 |
+
Only return the helpful answer below and nothing else.
|
29 |
+
Helpful answer:
|
30 |
+
"""
|
31 |
+
|
32 |
+
def set_custom_prompt():
|
33 |
+
"""
|
34 |
+
Prompt template for QA retrieval for each vectorstore
|
35 |
+
"""
|
36 |
+
prompt = PromptTemplate(template=custom_prompt_template,
|
37 |
+
input_variables=['context', 'question'])
|
38 |
+
return prompt
|
39 |
+
|
40 |
+
|
41 |
+
chat_model = ChatGroq(temperature=0, model_name="mixtral-8x7b-32768")
|
42 |
+
#chat_model = ChatGroq(temperature=0, model_name="Llama2-70b-4096")
|
43 |
+
#chat_model = ChatOllama(model="llama2", request_timeout=30.0)
|
44 |
+
|
45 |
+
client = QdrantClient(api_key=qdrant_api_key, url=qdrant_url,)
|
46 |
+
|
47 |
+
|
48 |
+
def retrieval_qa_chain(llm, prompt, vectorstore):
|
49 |
+
qa_chain = RetrievalQA.from_chain_type(
|
50 |
+
llm=llm,
|
51 |
+
chain_type="stuff",
|
52 |
+
retriever=vectorstore.as_retriever(search_kwargs={'k': 2}),
|
53 |
+
return_source_documents=True,
|
54 |
+
chain_type_kwargs={'prompt': prompt}
|
55 |
+
)
|
56 |
+
return qa_chain
|
57 |
+
|
58 |
+
|
59 |
+
def qa_bot():
|
60 |
+
embeddings = FastEmbedEmbeddings()
|
61 |
+
vectorstore = Qdrant(client=client, embeddings=embeddings, collection_name="rag")
|
62 |
+
llm = chat_model
|
63 |
+
qa_prompt=set_custom_prompt()
|
64 |
+
qa = retrieval_qa_chain(llm, qa_prompt, vectorstore)
|
65 |
+
return qa
|
66 |
+
|
67 |
+
#---------------------------------------------------------------------#
|
68 |
+
|
69 |
+
#qdrant_cloud_api_key="your_qdrant_cloud_api_key"
|
70 |
+
#qdrant_url="your_qdrant_url"
|
71 |
+
|
72 |
+
#qdrant_cloud = Qdrant.from_documents(
|
73 |
+
# docs,
|
74 |
+
# embeddings,
|
75 |
+
# url=qdrant_url,
|
76 |
+
# prefer_grpc=True,
|
77 |
+
# api_key=qdrant_cloud_api_key,
|
78 |
+
# collection_name="qdrant_cloud_documents",
|
79 |
+
#)
|
80 |
+
|
81 |
+
#---------------------------------------------------------------------#
|
82 |
+
query="how to make coffee"
|
83 |
+
print(query)
|
84 |
+
|
85 |
+
chain = qa_bot()
|
86 |
+
|
87 |
+
|
utils/stt.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# main.py (python example)
|
2 |
+
|
3 |
+
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
from deepgram import (
|
7 |
+
DeepgramClient,
|
8 |
+
PrerecordedOptions,
|
9 |
+
FileSource,
|
10 |
+
)
|
11 |
+
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
# Path to the audio file
|
15 |
+
AUDIO_FILE = r".\media\recorded.mp3"
|
16 |
+
API_KEY = os.getenv("DG_API_KEY")
|
17 |
+
|
18 |
+
|
19 |
+
def speech_to_text():
|
20 |
+
try:
|
21 |
+
# STEP 1 Create a Deepgram client using the API key
|
22 |
+
deepgram = DeepgramClient(API_KEY)
|
23 |
+
|
24 |
+
with open(AUDIO_FILE, "rb") as file:
|
25 |
+
buffer_data = file.read()
|
26 |
+
|
27 |
+
payload: FileSource = {
|
28 |
+
"buffer": buffer_data,
|
29 |
+
}
|
30 |
+
|
31 |
+
#STEP 2: Configure Deepgram options for audio analysis
|
32 |
+
options = PrerecordedOptions(
|
33 |
+
model="nova-2",
|
34 |
+
smart_format=True,
|
35 |
+
)
|
36 |
+
|
37 |
+
# STEP 3: Call the transcribe_file method with the text payload and options
|
38 |
+
response = deepgram.listen.prerecorded.v("1").transcribe_file(payload, options)
|
39 |
+
# STEP 4: Print the response
|
40 |
+
#print(response.to_json(indent=4))
|
41 |
+
#print(response["results"]["channels"][0]["alternatives"][0]["transcript"])
|
42 |
+
return str(response["results"]["channels"][0]["alternatives"][0]["transcript"])
|
43 |
+
|
44 |
+
except Exception as e:
|
45 |
+
print(f"Exception: {e}")
|
utils/tts.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import os
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import json
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
API_KEY = os.getenv("DG_API_KEY")
|
8 |
+
AUDIO_FILE=r".\media\ouput_file.mp3"
|
9 |
+
|
10 |
+
def text_to_speech(llm_response):
|
11 |
+
# Define the API endpoint
|
12 |
+
url = "https://api.deepgram.com/v1/speak?model=aura-asteria-en"
|
13 |
+
|
14 |
+
# Define the headers
|
15 |
+
headers = {
|
16 |
+
"Authorization": f"Token {API_KEY}",
|
17 |
+
"Content-Type": "application/json"
|
18 |
+
}
|
19 |
+
|
20 |
+
# Define the payload
|
21 |
+
payload = {
|
22 |
+
"text": llm_response
|
23 |
+
}
|
24 |
+
|
25 |
+
# Make the POST request
|
26 |
+
response = requests.post(url, headers=headers, json=payload)
|
27 |
+
|
28 |
+
# Check if the request was successful
|
29 |
+
if response.status_code == 200:
|
30 |
+
# Save the response content to a file
|
31 |
+
with open(AUDIO_FILE, "wb") as f:
|
32 |
+
f.write(response.content)
|
33 |
+
print("File saved successfully.")
|
34 |
+
else:
|
35 |
+
print(f"Error: {response.status_code} - {response.text}")
|
36 |
+
|
37 |
+
# Example usage
|
38 |
+
#transcribed_text = "Hello, how can I help you today?"
|
39 |
+
#tts(transcribed_text)
|