Spaces:
Sleeping
Sleeping
Mr-Vicky-01
commited on
Commit
•
53a56c8
1
Parent(s):
bd0d907
Update app.py
Browse files
app.py
CHANGED
@@ -4,23 +4,23 @@ from llama_index.llms.huggingface import HuggingFaceInferenceAPI
|
|
4 |
from dotenv import load_dotenv
|
5 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
6 |
from llama_index.core import Settings
|
7 |
-
import os
|
8 |
from youtube_transcript_api import YouTubeTranscriptApi
|
9 |
-
import time
|
10 |
import shutil
|
|
|
|
|
11 |
|
12 |
# Load environment variables
|
13 |
load_dotenv()
|
14 |
|
15 |
-
|
16 |
|
17 |
# Configure the Llama index settings
|
18 |
Settings.llm = HuggingFaceInferenceAPI(
|
19 |
model_name="meta-llama/Meta-Llama-3-8B-Instruct",
|
20 |
tokenizer_name="meta-llama/Meta-Llama-3-8B-Instruct",
|
21 |
-
context_window=
|
22 |
token=os.getenv("HF_TOKEN"),
|
23 |
-
max_new_tokens=
|
24 |
generate_kwargs={"temperature": 0.1},
|
25 |
)
|
26 |
Settings.embed_model = HuggingFaceEmbedding(
|
@@ -37,11 +37,20 @@ os.makedirs(PERSIST_DIR, exist_ok=True)
|
|
37 |
|
38 |
def data_ingestion():
|
39 |
documents = SimpleDirectoryReader(DATA_DIR).load_data()
|
40 |
-
print(documents)
|
41 |
storage_context = StorageContext.from_defaults()
|
42 |
-
index = VectorStoreIndex.from_documents(documents
|
43 |
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
def extract_transcript_details(youtube_video_url):
|
46 |
try:
|
47 |
video_id=youtube_video_url.split("=")[1]
|
@@ -57,24 +66,13 @@ def extract_transcript_details(youtube_video_url):
|
|
57 |
except Exception as e:
|
58 |
st.error(e)
|
59 |
|
60 |
-
def remove_old_files():
|
61 |
-
# Specify the directory path you want to clear
|
62 |
-
directory_path = "data"
|
63 |
-
|
64 |
-
# Remove all files and subdirectories in the specified directory
|
65 |
-
shutil.rmtree(directory_path)
|
66 |
-
|
67 |
-
# Recreate an empty directory if needed
|
68 |
-
os.makedirs(directory_path)
|
69 |
-
|
70 |
-
|
71 |
def handle_query(query):
|
72 |
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
73 |
index = load_index_from_storage(storage_context)
|
74 |
chat_text_qa_msgs = [
|
75 |
(
|
76 |
"user",
|
77 |
-
"""You are
|
78 |
Context:
|
79 |
{context_str}
|
80 |
Question:
|
@@ -83,37 +81,34 @@ def handle_query(query):
|
|
83 |
)
|
84 |
]
|
85 |
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
86 |
-
|
87 |
query_engine = index.as_query_engine(text_qa_template=text_qa_template)
|
88 |
answer = query_engine.query(query)
|
89 |
-
|
90 |
-
|
91 |
if hasattr(answer, 'response'):
|
92 |
-
|
93 |
elif isinstance(answer, dict) and 'response' in answer:
|
94 |
-
|
95 |
else:
|
96 |
-
|
97 |
|
98 |
-
|
99 |
-
for i in
|
100 |
-
yield
|
101 |
time.sleep(0.001)
|
102 |
|
103 |
|
104 |
# Streamlit app initialization
|
105 |
st.title("Chat with your PDF📄")
|
106 |
-
st.markdown("Built by [
|
107 |
-
st.markdown("chat here👇")
|
108 |
|
109 |
if 'messages' not in st.session_state:
|
110 |
-
st.session_state.messages = [{'role': 'assistant', "content": 'Hello! Upload a PDF and ask me anything about
|
111 |
-
|
112 |
-
# Display or clear chat messages
|
113 |
for message in st.session_state.messages:
|
114 |
-
with st.chat_message(message[
|
115 |
-
st.write(message[
|
116 |
-
|
117 |
with st.sidebar:
|
118 |
st.title("Menu:")
|
119 |
uploaded_file = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button")
|
@@ -137,15 +132,15 @@ with st.sidebar:
|
|
137 |
st.success("Done")
|
138 |
|
139 |
user_prompt = st.chat_input("Ask me anything about the content of the PDF:")
|
140 |
-
|
|
|
141 |
st.session_state.messages.append({'role': 'user', "content": user_prompt})
|
142 |
-
with st.chat_message("user"):
|
143 |
st.write(user_prompt)
|
144 |
|
145 |
-
#
|
146 |
-
|
147 |
-
with st.chat_message("assistant"):
|
148 |
response = handle_query(user_prompt)
|
149 |
-
|
150 |
-
|
151 |
-
st.session_state.messages.append(
|
|
|
4 |
from dotenv import load_dotenv
|
5 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
6 |
from llama_index.core import Settings
|
|
|
7 |
from youtube_transcript_api import YouTubeTranscriptApi
|
|
|
8 |
import shutil
|
9 |
+
import os
|
10 |
+
import time
|
11 |
|
12 |
# Load environment variables
|
13 |
load_dotenv()
|
14 |
|
15 |
+
icons = {"assistant": "robot.png", "user": "man-kddi.png"}
|
16 |
|
17 |
# Configure the Llama index settings
|
18 |
Settings.llm = HuggingFaceInferenceAPI(
|
19 |
model_name="meta-llama/Meta-Llama-3-8B-Instruct",
|
20 |
tokenizer_name="meta-llama/Meta-Llama-3-8B-Instruct",
|
21 |
+
context_window=3900,
|
22 |
token=os.getenv("HF_TOKEN"),
|
23 |
+
# max_new_tokens=1000,
|
24 |
generate_kwargs={"temperature": 0.1},
|
25 |
)
|
26 |
Settings.embed_model = HuggingFaceEmbedding(
|
|
|
37 |
|
38 |
def data_ingestion():
|
39 |
documents = SimpleDirectoryReader(DATA_DIR).load_data()
|
|
|
40 |
storage_context = StorageContext.from_defaults()
|
41 |
+
index = VectorStoreIndex.from_documents(documents)
|
42 |
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
43 |
|
44 |
+
def remove_old_files():
|
45 |
+
# Specify the directory path you want to clear
|
46 |
+
directory_path = "data"
|
47 |
+
|
48 |
+
# Remove all files and subdirectories in the specified directory
|
49 |
+
shutil.rmtree(directory_path)
|
50 |
+
|
51 |
+
# Recreate an empty directory if needed
|
52 |
+
os.makedirs(directory_path)
|
53 |
+
|
54 |
def extract_transcript_details(youtube_video_url):
|
55 |
try:
|
56 |
video_id=youtube_video_url.split("=")[1]
|
|
|
66 |
except Exception as e:
|
67 |
st.error(e)
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
def handle_query(query):
|
70 |
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
71 |
index = load_index_from_storage(storage_context)
|
72 |
chat_text_qa_msgs = [
|
73 |
(
|
74 |
"user",
|
75 |
+
"""You are Q&A assistant named CHATTO, created by Pachaiappan an AI Specialist. Your main goal is to provide answers as accurately as possible, based on the instructions and context you have been given. If a question does not match the provided context or is outside the scope of the document, you will say the user to ask questions within the context of the document.
|
76 |
Context:
|
77 |
{context_str}
|
78 |
Question:
|
|
|
81 |
)
|
82 |
]
|
83 |
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
|
|
84 |
query_engine = index.as_query_engine(text_qa_template=text_qa_template)
|
85 |
answer = query_engine.query(query)
|
86 |
+
|
87 |
+
|
88 |
if hasattr(answer, 'response'):
|
89 |
+
return answer.response
|
90 |
elif isinstance(answer, dict) and 'response' in answer:
|
91 |
+
return answer['response']
|
92 |
else:
|
93 |
+
return "Sorry, I couldn't find an answer."
|
94 |
|
95 |
+
def streamer(text):
|
96 |
+
for i in text:
|
97 |
+
yield i
|
98 |
time.sleep(0.001)
|
99 |
|
100 |
|
101 |
# Streamlit app initialization
|
102 |
st.title("Chat with your PDF📄")
|
103 |
+
st.markdown("**Built by [Pachaiappan❤️](https://github.com/Mr-Vicky-01)**")
|
|
|
104 |
|
105 |
if 'messages' not in st.session_state:
|
106 |
+
st.session_state.messages = [{'role': 'assistant', "content": 'Hello! Upload a PDF and ask me anything about the content.'}]
|
107 |
+
|
|
|
108 |
for message in st.session_state.messages:
|
109 |
+
with st.chat_message(message['role'], avatar=icons[message['role']]):
|
110 |
+
st.write(message['content'])
|
111 |
+
|
112 |
with st.sidebar:
|
113 |
st.title("Menu:")
|
114 |
uploaded_file = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button")
|
|
|
132 |
st.success("Done")
|
133 |
|
134 |
user_prompt = st.chat_input("Ask me anything about the content of the PDF:")
|
135 |
+
|
136 |
+
if user_prompt and uploaded_file:
|
137 |
st.session_state.messages.append({'role': 'user', "content": user_prompt})
|
138 |
+
with st.chat_message("user", avatar="man-kddi.png"):
|
139 |
st.write(user_prompt)
|
140 |
|
141 |
+
# Trigger assistant's response retrieval and update UI
|
142 |
+
with st.spinner("Thinking..."):
|
|
|
143 |
response = handle_query(user_prompt)
|
144 |
+
with st.chat_message("user", avatar="robot.png"):
|
145 |
+
st.write_stream(streamer(response))
|
146 |
+
st.session_state.messages.append({'role': 'assistant', "content": response})
|