-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel-run.py
190 lines (148 loc) · 6.37 KB
/
model-run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from gtts import gTTS
from io import BytesIO
import pygame
import tempfile
from pydub import AudioSegment
import streamlit as st
from langchain.vectorstores import FAISS
from langchain import PromptTemplate
from langchain.document_loaders import PyPDFLoader, DirectoryLoader
from langchain import PromptTemplate
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.llms import CTransformers
from langchain.chains import RetrievalQA
from output import text_to_asl_video
from textwrap import shorten
import subprocess
import time
DB_FAISS_PATH = 'vectorstore/db_faiss'
custom_prompt_template = """When asked about legal matters, use the following pieces of information to answer the user's question and quote the Indian Laws whenenver you can.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
Context: {context}
Question: {question}
Only return the helpful answer below and nothing else.
Helpful answer:
"""
# Loading the model
def load_llm():
# Load the locally downloaded model here
llm = CTransformers(
model="llama-2-7b-chat.ggmlv3.q4_0.bin",
model_type="llama",
max_new_tokens=512,
temperature=0.5
)
return llm
def set_custom_prompt():
prompt = PromptTemplate(template=custom_prompt_template, input_variables=['context', 'question'])
return prompt
#Retrieval QA Chain
def retrieval_qa_chain(llm, prompt, db):
qa_chain = RetrievalQA.from_chain_type(llm=llm,
chain_type='stuff',
retriever=db.as_retriever(search_kwargs={'k': 2}),
return_source_documents=True,
chain_type_kwargs={'prompt': prompt}
)
return qa_chain
# QA Model Function
def qa_bot():
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
model_kwargs={'device': 'cpu'})
db = FAISS.load_local(DB_FAISS_PATH, embeddings)
llm = load_llm()
qa_prompt = set_custom_prompt()
qa = retrieval_qa_chain(llm, qa_prompt, db)
return qa
def generate_hand_sign_videos(query_text):
# Convert the text to a list of video URLs
video_urls = text_to_asl_video(query_text)
# Generate HTML for the video playlist
video_playlist = '<div id="video-container"></div><script>'
video_playlist += '''
var videos = %s;
var videoContainer = document.getElementById('video-container');
var currentIndex = 0;
function playNextVideo() {
if (currentIndex < videos.length) {
videoContainer.innerHTML = '<video width="330" height="200" controls autoplay onended="playNextVideo()"><source src="' + videos[currentIndex] + '" type="video/mp4"></video>';
currentIndex++;
}
}
playNextVideo();
''' % video_urls
video_playlist += '</script>'
return video_playlist
# Output function
def final_result(query):
qa_result = qa_bot()
response = qa_result({'query': query})
return response
def generate_hand_sign_videos(query_text):
# Convert the text to a list of video URLs
video_urls = text_to_asl_video(query_text)
# Generate HTML for the video playlist
video_playlist = '<div id="video-container"></div><script>'
video_playlist += '''
var videos = %s;
var videoContainer = document.getElementById('video-container');
var currentIndex = 0;
function playNextVideo() {
if (currentIndex < videos.length) {
videoContainer.innerHTML = '<video width="330" height="200" controls autoplay onended="playNextVideo()"><source src="' + videos[currentIndex] + '" type="video/mp4"></video>';
currentIndex++;
}
}
playNextVideo();
''' % video_urls
video_playlist += '</script>'
return video_playlist
def text_to_speech(text, lang='en'):
tts = gTTS(text=text, lang=lang)
mp3 = BytesIO()
tts.save(mp3)
mp3.seek(0)
return mp3
def play_audio(audio_file_path):
pygame.mixer.init()
pygame.mixer.music.load(audio_file_path)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
# from textwrap import shorten
def main():
st.title("Ai For ALL")
st.write("A lawyer for all with AI on call...")
# New selection for input type (text or video)
input_type = st.selectbox("Select Input Type:", ["Text", "Video"], key="input_type")
if input_type == "Text":
user_input = st.text_input("You (Text):", "Enter your queries here...", key="text_input")
output_format = st.selectbox("Select Output Format:", ["Text", "Speech", "Video"], key="s1")
# ... rest of the code for handling text input ...
else:
st.write("Running hand gesture tracking...")
# Execute "run1.py" (can be done using subprocess)
import subprocess
process = subprocess.Popen(['python', 'hand-gesture-recognition-code/run-0.py'])
import time
time.sleep(20) # Run for 10 seconds
process.terminate() # Terminate the process
st.write("Hey, there! This feature is not ready yet and only tracks your hand gestures, kindly select text as input.")
user_input = st.text_input("You (Text):", "Enter your queries here...", key="text_input")
output_format = st.selectbox("Select Output Format:", ["Text", "Speech", "Video"], key="s1")
response = final_result(user_input)['result'] # Get the model's response and extract the 'result' key
# Display the full text response
st.write(f"Bot (Text): {response}")
# If video is selected, summarize the response and generate/display the hand-sign video
if output_format == "Video":
response_text = shorten(response, width=60, placeholder="...") # Summarize to 30 words
video_html = generate_hand_sign_videos(response_text) # Use summarized response
st.components.v1.html(video_html, height=200) # Adjust the height as needed
if output_format == "Speech":
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
tts = gTTS(text=response, lang='en')
tts.save(temp_audio_file.name)
play_audio(temp_audio_file.name)
if __name__ == "__main__":
main()