new-AI-chatbot / app.py
sarmatkaa's picture
Create app.py
7e79264 verified
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from langchain_community.document_loaders import PyPDFLoader
import gradio as gr
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
warnings.filterwarnings('ignore')
## LLM
def get_llm():
model_name = "TheBloke/guanaco-7B-GPTQ"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", trust_remote_code=True)
text_generator = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_new_tokens=256,
temperature=0.5,
do_sample=True,
)
return text_generator
## Document loader
def document_loader(file):
loader = PyPDFLoader(file)
loaded_document = loader.load()
full_text = "\n".join([doc.page_content for doc in loaded_document])
return full_text
## QA function without RAG
def simple_qa(file, query):
llm = get_llm()
document_text = document_loader(file)
prompt = f"Document:\n{document_text}\n\nQuestion: {query}\nAnswer:"
response = llm(prompt)
return response[0]['generated_text']
# Create Gradio interface
simple_application = gr.Interface(
fn=simple_qa,
allow_flagging="never",
inputs=[
gr.File(label="Upload PDF File", file_count="single", file_types=['.pdf'], type="filepath"),
gr.Textbox(label="Input Query", lines=2, placeholder="Type your question here...")
],
outputs=gr.Textbox(label="Output"),
title="Basia's simple Q&A bot (no RAG)!",
description="Upload a PDF document and ask any question. The chatbot will try to answer using the full document without retrieval."
)
# Launch the app
simple_application.launch(server_name="127.0.0.1", server_port=7860)