CoolFace
Apppublic

EdgarDataScientist/Diabetrek_AI

sourceHugging Facecreativeml-openrail-mupdated 2y agoView on Hugging Face
0likes
chatbot.py106 linesDownload Raw Back to root
1import torch2import fitz  # PyMuPDF for PDF text extraction3from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline4from sentence_transformers import SentenceTransformer5import faiss6import gradio as gr7import os8from huggingface_hub import login9 10# Authenticate with Hugging Face Hub11 12pdf_path1 ='/content/Chrono 1.pdf'13pdf_path2 ='/content/Chrono 2.pdf'14pdf_path3 ='/content/Chrono 3.pdf'15 16# Load the Mistral 7B model and tokenizer17model_name = 'mistralai/Mistral-7B-Instruct-v0.3'18tokenizer = AutoTokenizer.from_pretrained(model_name)19model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)20 21# Load sentence transformer for embedding and similarity search22embedder = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')23 24# Function to extract text from PDFs25def extract_text_from_pdf(pdf_file_path):26    doc = fitz.open(pdf_file_path)27    text = ""28    for page in doc:29        text += page.get_text("text")30    return text31 32# Placeholder PDF knowledge base (Extracted content from PDFs)33pdf_knowledge_base = []34pdf_files = [pdf_path1, pdf_path2,pdf_path3]  # Add the actual paths to your PDF files35 36for pdf_file in pdf_files:37    pdf_text = extract_text_from_pdf(pdf_file)38    pdf_knowledge_base.append({"document": pdf_file, "content": pdf_text})39 40# Combine extracted text with specific company information41knowledge_base = [42    {"question": "How does DiabeTrek ensure data privacy and security?",43     "answer": ("DiabeTrek ensures data privacy through multiple layers of protection, including data encryption during "44                "transit and at rest. We comply with regulations like HIPAA and GDPR to safeguard your personal data.")},45    {"question": "What are DiabeTrek's emergency guidelines?",46     "answer": "DiabeTrek advises users to seek immediate medical attention in case of diabetes-related emergencies. This chatbot is not for emergency use."},47    {"question": "What are DiabeTrek's mission, vision, and values?",48     "answer": "DiabeTrek's mission is to improve the lives of people with diabetes through innovative AI-driven solutions. Our vision is a world where diabetes care is seamless, proactive, and accessible."},49    # Additional items can be added here following the CEO's instructions50]51 52# Create a FAISS index for efficient retrieval53embedding_dim = 384  # Output dimension of the MiniLM model54index = faiss.IndexFlatL2(embedding_dim)55 56# Create a list of embeddings and index them57knowledge_embeddings = []58for entry in knowledge_base:59    embedding = embedder.encode(entry['question'], convert_to_tensor=False)60    knowledge_embeddings.append(embedding)61    index.add(embedding.reshape(1, -1))62 63# Create embeddings for PDF content and index them64for pdf_entry in pdf_knowledge_base:65    embedding = embedder.encode(pdf_entry['content'], convert_to_tensor=False)66    knowledge_embeddings.append(embedding)67    index.add(embedding.reshape(1, -1))68 69# RAG Retrieval function70def retrieve_knowledge(question, top_k=1):71    question_embedding = embedder.encode(question, convert_to_tensor=False)72    D, I = index.search(question_embedding.reshape(1, -1), top_k)73    results = [knowledge_base[idx] for idx in I[0]]74    return results75 76# Chatbot function combining retrieval and generation77def customer_support_chatbot(user_input):78    # Retrieve relevant knowledge79    retrieved_knowledge = retrieve_knowledge(user_input)80 81    # Prepare context for the generative model82    context = " ".join([f"Q: {entry['question']} A: {entry['answer']}" for entry in retrieved_knowledge])83 84    # Generate response using Mistral85    prompt = f"Customer Question: {user_input}\n\n{context}\n\nResponse:"86    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)87    outputs = model.generate(**inputs, max_length=150, do_sample=True, temperature=0.7)88    response = tokenizer.decode(outputs[0], skip_special_tokens=True)89 90    return response91 92# Gradio UI93def gradio_interface(user_input):94    response = customer_support_chatbot(user_input)95    return response96 97# Build Gradio interface98interface = gr.Interface(fn=gradio_interface,99                         inputs="text",100                         outputs="text",101                         title="DiabeTrek Customer Support Chatbot",102                         description="Ask any question about DiabeTrek, its services, and policies.")103 104# Launch the Gradio app105interface.launch()106