model"
else:
prompt = ""
return prompt
def bot(history, model=None):
print("===> model: ", model)
local_llm = llm
if model:
local_llm = CustomLLM(repo_id=model, model_type=model_type, api_token=API_TOKEN)
if len(history) > 0 and len(history[-1]) > 0 and '===>' in history[-1][0]:
new_model = history[-1][0].replace("===>", "")
response = f"You have changed the model to {new_model}"
elif len(history) > 0 and len(history[-1]) > 0 and '.pdf!!!' in history[-1][0].lower():
response = f"Unable to add file. Maximum 3 files allowed."
elif len(history) > 0 and len(history[-1]) > 0 and '.pdf' in history[-1][0]:
response = "You uploaded a PDF file. You can ask questions from the file."
else:
prompt = history[-1][0]
if hasFile(history):
query_engine = getEngine(local_llm)
response = query_engine.query(prompt)
print("Response from file")
else:
response = local_llm.predict(format_prompt(prompt, history, model))
print("Response from Model")
# print(response)
# response = "Thats cool!"
history[-1][1] = ""
for character in str(response):
history[-1][1] += character
# time.sleep(0.05)
yield history
with gr.Blocks() as demo:
gr.Markdown(
"""
Basic RAG with Huggingface Inference API
For best performance start with small PDF files (less than 20 pages).
"""
)
chatbot = gr.Chatbot(
[],
elem_id="chatbot",
bubble_full_width=False,
# avatar_images=(None, (os.path.join(os.path.dirname(__file__), "avatar.png"))),
)
with gr.Row():
drop = gr.Dropdown(
[
("Mixtral-8x7B-Instruct-v0.1", "mistralai/Mixtral-8x7B-Instruct-v0.1"),
("Mistral-7B-Instruct-v0.2", "mistralai/Mistral-7B-Instruct-v0.2"),
("gemma-7b-it", "google/gemma-7b-it"),
("gemma-2b-it", "google/gemma-2b-it")
],
value="mistralai/Mixtral-8x7B-Instruct-v0.1",
label="Model",
info=""
)
with gr.Row():
txt = gr.Textbox(
scale=4,
show_label=False,
placeholder="Type your question and press enter",
container=False,
)
btn = gr.UploadButton("📁", file_types=[".pdf"])
clear_btn = gr.ClearButton([chatbot, txt])
drop.change(modelChanged, [chatbot, drop], [chatbot, drop], queue=False).then(
bot, [chatbot, drop], chatbot
)
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
bot, [chatbot, drop], chatbot, api_name="bot_response"
)
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(
bot, [chatbot, drop], chatbot
)
clear_btn.click(clearClick)
demo.queue()
demo.launch()