Mr-Vicky-01 commited on
Commit
a5b1980
1 Parent(s): 030c076

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -13
app.py CHANGED
@@ -8,6 +8,7 @@ import shutil
8
  import os
9
  import time
10
 
 
11
  icons = {"assistant": "robot.png", "user": "man-kddi.png"}
12
 
13
  # Configure the Llama index settings
@@ -16,6 +17,7 @@ Settings.llm = HuggingFaceInferenceAPI(
16
  tokenizer_name="meta-llama/Meta-Llama-3-8B-Instruct",
17
  context_window=3900,
18
  token=os.getenv("HF_TOKEN"),
 
19
  generate_kwargs={"temperature": 0.1},
20
  )
21
  Settings.embed_model = HuggingFaceEmbedding(
@@ -37,20 +39,27 @@ def data_ingestion():
37
  index.storage_context.persist(persist_dir=PERSIST_DIR)
38
 
39
  def remove_old_files():
 
40
  directory_path = "data"
 
 
41
  shutil.rmtree(directory_path)
 
 
42
  os.makedirs(directory_path)
43
 
44
  def extract_transcript_details(youtube_video_url):
45
  try:
46
- video_id = youtube_video_url.split("=")[1]
47
- transcript_text = YouTubeTranscriptApi.get_transcript(video_id)
 
48
 
49
  transcript = ""
50
  for i in transcript_text:
51
  transcript += " " + i["text"]
52
 
53
  return transcript
 
54
  except Exception as e:
55
  st.error(e)
56
 
@@ -58,20 +67,21 @@ def handle_query(query):
58
  storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
59
  index = load_index_from_storage(storage_context)
60
  chat_text_qa_msgs = [
61
- (
62
- "user",
63
- """You are Q&A assistant named CHATTO, created by Pachaiappan [linkdin](https://www.linkedin.com/in/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 only say the user to 'Please ask a questions within the context of the document'.
64
- Context:
65
- {context_str}
66
- Question:
67
- {query_str}
68
- """
69
- )
70
  ]
71
  text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
72
  query_engine = index.as_query_engine(text_qa_template=text_qa_template)
73
  answer = query_engine.query(query)
74
 
 
75
  if hasattr(answer, 'response'):
76
  return answer.response
77
  elif isinstance(answer, dict) and 'response' in answer:
@@ -84,6 +94,7 @@ def streamer(text):
84
  yield i
85
  time.sleep(0.001)
86
 
 
87
  # Streamlit app initialization
88
  st.title("Chat with your PDF📄")
89
  st.markdown("**Built by [Pachaiappan❤️](https://mr-vicky-01.github.io/Portfolio/)**")
@@ -101,7 +112,7 @@ with st.sidebar:
101
  video_url = st.text_input("Enter Youtube Video Link: ")
102
  if st.button("Submit & Process"):
103
  with st.spinner("Processing..."):
104
- if len(os.listdir("data")) != 0:
105
  remove_old_files()
106
 
107
  if uploaded_file:
@@ -128,4 +139,4 @@ if user_prompt and (uploaded_file or video_url):
128
  with st.spinner("Thinking..."):
129
  response = handle_query(user_prompt)
130
  with st.chat_message("user", avatar="robot.png"):
131
- st.write_stream(streamer(response))
 
8
  import os
9
  import time
10
 
11
+
12
  icons = {"assistant": "robot.png", "user": "man-kddi.png"}
13
 
14
  # Configure the Llama index settings
 
17
  tokenizer_name="meta-llama/Meta-Llama-3-8B-Instruct",
18
  context_window=3900,
19
  token=os.getenv("HF_TOKEN"),
20
+ # max_new_tokens=1000,
21
  generate_kwargs={"temperature": 0.1},
22
  )
23
  Settings.embed_model = HuggingFaceEmbedding(
 
39
  index.storage_context.persist(persist_dir=PERSIST_DIR)
40
 
41
  def remove_old_files():
42
+ # Specify the directory path you want to clear
43
  directory_path = "data"
44
+
45
+ # Remove all files and subdirectories in the specified directory
46
  shutil.rmtree(directory_path)
47
+
48
+ # Recreate an empty directory if needed
49
  os.makedirs(directory_path)
50
 
51
  def extract_transcript_details(youtube_video_url):
52
  try:
53
+ video_id=youtube_video_url.split("=")[1]
54
+
55
+ transcript_text=YouTubeTranscriptApi.get_transcript(video_id)
56
 
57
  transcript = ""
58
  for i in transcript_text:
59
  transcript += " " + i["text"]
60
 
61
  return transcript
62
+
63
  except Exception as e:
64
  st.error(e)
65
 
 
67
  storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
68
  index = load_index_from_storage(storage_context)
69
  chat_text_qa_msgs = [
70
+ (
71
+ "user",
72
+ """You are Q&A assistant named CHATTO, created by Pachaiappan [linkdin](https://www.linkedin.com/in/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 only say the user to 'Please ask a questions within the context of the document'.
73
+ Context:
74
+ {context_str}
75
+ Question:
76
+ {query_str}
77
+ """
78
+ )
79
  ]
80
  text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
81
  query_engine = index.as_query_engine(text_qa_template=text_qa_template)
82
  answer = query_engine.query(query)
83
 
84
+
85
  if hasattr(answer, 'response'):
86
  return answer.response
87
  elif isinstance(answer, dict) and 'response' in answer:
 
94
  yield i
95
  time.sleep(0.001)
96
 
97
+
98
  # Streamlit app initialization
99
  st.title("Chat with your PDF📄")
100
  st.markdown("**Built by [Pachaiappan❤️](https://mr-vicky-01.github.io/Portfolio/)**")
 
112
  video_url = st.text_input("Enter Youtube Video Link: ")
113
  if st.button("Submit & Process"):
114
  with st.spinner("Processing..."):
115
+ if len(os.listdir("data")) !=0:
116
  remove_old_files()
117
 
118
  if uploaded_file:
 
139
  with st.spinner("Thinking..."):
140
  response = handle_query(user_prompt)
141
  with st.chat_message("user", avatar="robot.png"):
142
+ st.write_stream(streamer(response))