# -*- coding: utf-8 -*- """Untitled44.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1PxYl919o_aJGWdZoMRTVFD4XESysiWAH """ import gradio as gr import random import time from transformers import pipeline,AutoModelForQuestionAnswering,AutoTokenizer import pdfplumber def extract_text_from_pdf(pdf_path): text = "" with pdfplumber.open(pdf_path) as pdf: for page in pdf.pages: text += page.extract_text() return text pdf_path = "Ketan gandhi-chatbotdata.pdf" pdf_text = extract_text_from_pdf(pdf_path) model_name="deepset/roberta-base-squad2" qa_pipeline = pipeline("question-answering", model=AutoModelForQuestionAnswering.from_pretrained(model_name),tokenizer=AutoTokenizer.from_pretrained(model_name)) with gr.Blocks() as demo: chatbot = gr.Chatbot() msg = gr.Textbox(placeholder="Ask me anything related to Ketan Gandhi😎") clear = gr.ClearButton([msg, chatbot]) def respond(message, chat_history): question = message answer = qa_pipeline(question=question, context=pdf_text) confidence_threshold=0.3 if answer['score']>confidence_threshold: bot_message = answer['answer'] chat_history.append((message, bot_message)) else: chat_history.append((message,"I may not be the right bot to answer this......😅")) time.sleep(2) return "", chat_history def vote(data: gr.LikeData): if data.liked: print("You upvoted this response: " + data.value) else: print("You downvoted this response: " + data.value) msg.submit(respond, [msg, chatbot], [msg, chatbot]) chatbot.like(vote, None, None) if __name__ == "__main__": demo.launch()