Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.document_loaders import PyPDFLoader, OnlinePDFLoader
|
2 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
3 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
4 |
+
from langchain.vectorstores import Pinecone
|
5 |
+
from sentence_transformers import SentenceTransformer
|
6 |
+
from langchain.chains.question_answering import load_qa_chain
|
7 |
+
import pinecone
|
8 |
+
import os
|
9 |
+
from langchain.llms import LlamaCpp
|
10 |
+
from langchain.callbacks.manager import CallbackManager
|
11 |
+
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
12 |
+
from huggingface_hub import hf_hub_download
|
13 |
+
from langchain.chains.question_answering import load_qa_chain
|
14 |
+
from langchain import PromptTemplate
|
15 |
+
from langchain.chains import RetrievalQA
|
16 |
+
from langchain.prompts import PromptTemplate
|
17 |
+
from langchain.llms import CTransformers
|
18 |
+
import torch
|
19 |
+
from langchain.chains import LLMChain
|
20 |
+
from peft import PeftModel
|
21 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, LlamaTokenizer, StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
|
22 |
+
import gradio as gr
|
23 |
+
import time
|
24 |
+
from transformers import pipeline
|
25 |
+
from gtts import gTTS
|
26 |
+
|
27 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = "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"
|
28 |
+
PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY', '55f52f29-11e3-4b87-a6ba-9a5494dfdb58')
|
29 |
+
PINECONE_API_ENV = os.environ.get('PINECONE_API_ENV', 'asia-southeast1-gcp-free')
|
30 |
+
embeddings=HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
31 |
+
# initialize pinecone
|
32 |
+
pinecone.init(
|
33 |
+
api_key=PINECONE_API_KEY, # find at app.pinecone.io
|
34 |
+
environment='gcp-starter' # next to api key in console
|
35 |
+
)
|
36 |
+
index_name = "rpl-llama" # put in the name of your pinecone index here
|
37 |
+
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
|
38 |
+
model_name_or_path = "TheBloke/Llama-2-7b-Chat-GGUF"
|
39 |
+
model_basename = "llama-2-7b-chat.Q4_0.gguf"
|
40 |
+
model_path = hf_hub_download(repo_id=model_name_or_path, filename=model_basename)
|
41 |
+
n_gpu_layers = 40 # Change this value based on your model and your GPU VRAM pool.
|
42 |
+
n_batch = 512 # Should be between 1 and n_ctx, consider the amount of VRAM in your GPU.
|
43 |
+
|
44 |
+
# Make sure the model path is correct for your system!
|
45 |
+
llm = LlamaCpp(
|
46 |
+
model_path=model_path,
|
47 |
+
n_gpu_layers=n_gpu_layers,
|
48 |
+
n_batch=n_batch,
|
49 |
+
callback_manager=callback_manager,
|
50 |
+
verbose=True, # Verbose is required to pass to the callback manager
|
51 |
+
)
|
52 |
+
prompt_template="""
|
53 |
+
Use the embeddings, summarize and generate the answers to user's questions. Don't repeat sentences.
|
54 |
+
|
55 |
+
|
56 |
+
Context: {docs}
|
57 |
+
Question: {query}
|
58 |
+
|
59 |
+
Only return the helpful answer below and nothing else.
|
60 |
+
Helpful answer:
|
61 |
+
"""
|
62 |
+
|
63 |
+
PROMPT=PromptTemplate(template=prompt_template, input_variables=["docs", "query"])
|
64 |
+
llm_chain = LLMChain(prompt=PROMPT, llm=llm)
|
65 |
+
|
66 |
+
# Initialize the chatbot model
|
67 |
+
asr = pipeline("automatic-speech-recognition", model="openai/whisper-small")
|
68 |
+
global bot_message
|
69 |
+
# Create a Gradio interface
|
70 |
+
with gr.Blocks() as demo:
|
71 |
+
|
72 |
+
chatbot = gr.Chatbot(avatar_images=("human.png", "bot.png"), value=[[None, "Welcome to the Indore-Ekk Number Superstore family! We're thrilled to have you on board. \n How can I assist you today?"]])
|
73 |
+
with gr.Row(label="Voice Input and Output"):
|
74 |
+
with gr.Column(variant="panel"):
|
75 |
+
audio_file = gr.Audio(label='Voice based Input',source="microphone",type="filepath",optional=True)
|
76 |
+
with gr.Column(variant="panel"):
|
77 |
+
play_audio = gr.Audio(label='Output Audio', autoplay=True)
|
78 |
+
audio_out = gr.Textbox(visible=False)
|
79 |
+
|
80 |
+
with gr.Row(label="Voice Input and Output"):
|
81 |
+
with gr.Column(label='Text Based Input', variant="panel"):
|
82 |
+
msg = gr.Textbox(placeholder="Ask me your doubts")
|
83 |
+
with gr.Column(variant="panel"):
|
84 |
+
with gr.Row():
|
85 |
+
clear = gr.Button("Clear the Chatbot Conversation")
|
86 |
+
|
87 |
+
def text_to_speech(text):
|
88 |
+
var = gTTS(text = text,lang = 'en')
|
89 |
+
var.save('eng.mp3')
|
90 |
+
return gr.Audio.update(value='eng.mp3')
|
91 |
+
|
92 |
+
def user(user_message, history):
|
93 |
+
global query
|
94 |
+
global fck
|
95 |
+
query = user_message
|
96 |
+
fck = model_response(query)
|
97 |
+
print(user_message,fck)
|
98 |
+
return '', history + [[user_message, None]],gr.Textbox.update(value=fck)
|
99 |
+
|
100 |
+
def model_response(query):
|
101 |
+
global a
|
102 |
+
#query = "What is the leave policy?"
|
103 |
+
docs=docsearch.similarity_search(query)
|
104 |
+
docs = docs[0].page_content+docs[1].page_content+docs[2].page_content
|
105 |
+
a = llm_chain.run({'docs':docs,'query':query})
|
106 |
+
return a
|
107 |
+
|
108 |
+
def bot(history):
|
109 |
+
global bot_message
|
110 |
+
bot_message = model_response(query)
|
111 |
+
history[-1][1] = ""
|
112 |
+
for character in fck:
|
113 |
+
history[-1][1] += character
|
114 |
+
time.sleep(0.05)
|
115 |
+
yield history
|
116 |
+
|
117 |
+
|
118 |
+
def speech_to_text(audio_file,history):
|
119 |
+
if audio_file == None:
|
120 |
+
return "", history + [[None, None]]
|
121 |
+
else:
|
122 |
+
global query
|
123 |
+
global fck
|
124 |
+
text = asr(audio_file)["text"]
|
125 |
+
query = text
|
126 |
+
fck = model_response(query)
|
127 |
+
print(text)
|
128 |
+
return None, history + [[text, None]],gr.Textbox.update(value=fck)
|
129 |
+
#return text
|
130 |
+
|
131 |
+
audio_file.stop_recording(speech_to_text, [audio_file,chatbot], [audio_file,chatbot,audio_out], queue=False, show_progress=False).then(bot, chatbot, chatbot)
|
132 |
+
|
133 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot,audio_out], queue=False).then(
|
134 |
+
bot, chatbot, chatbot
|
135 |
+
)
|
136 |
+
|
137 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
138 |
+
audio_out.change(text_to_speech,inputs=[audio_out], outputs=play_audio)
|
139 |
+
|
140 |
+
demo.queue()
|
141 |
+
demo.launch(debug=True)
|