-6.4 C
United States of America
Tuesday, January 7, 2025

Medical Chatbot with Gemini 2.0, Flask and Vector Embedding


Within the period of AI, chatbots have revolutionized how we work together with expertise. Maybe one of the vital impactful makes use of is within the healthcare trade. Chatbots are in a position to ship quick, correct data, and assist people extra successfully handle their well being. On this article, we’ll learn to develop a medical chatbot utilizing Gemini 2.0, Flask, HTML, and Bootstrap. The undertaking is about creating a customized, user-friendly customized platform to reply health-related queries with accuracy and pace.

Studying Goals

  • Perceive the important thing elements and structure of a Medical Chatbot with Gemini 2.0, and the way it enhances healthcare interactions.
  • Discover ways to combine Gemini 2.0 right into a medical chatbot to supply correct, context-aware responses tailor-made to affected person wants.
  • Discover the usage of HTML and Bootstrap to design a responsive and user-friendly chatbot interface.
  • Achieve hands-on expertise in organising and deploying a customized medical chatbot.
  • Uncover the position of FAISS in enhancing search effectivity for chatbot functionalities.

What’s Gemini 2.0?

Introduced in December 2024, Gemini 2.0 is the newest iteration of Google’s giant language mannequin (LLM) collection, developed by Google DeepMind. It introduces a number of key enhancements, together with multimodal output, native software use, and agentic skills, positioning it as a flexible AI mannequin for various purposes.

Constructing on its predecessor, Gemini 1.5, Gemini 2.0 extends the aptitude to course of and generate textual content, photos, video, and audio. It provides native picture creation and multilingual text-to-speech outputs for extra pure, interactive person experiences.

One of the crucial excellent options of Gemini 2.0 is its agentic AI, which permits the system to plan and execute duties independently. Experimental tasks like Mission Astra display this functionality by integrating with Google companies equivalent to Search and Maps to supply real-time, contextual help. One other instance is Mission Mariner, a Chrome extension that navigates the net autonomously to carry out duties equivalent to on-line procuring.

Key Options of Gemini 2.0

  • Multimodal Output: Gemini 2.0 processes and generates a number of knowledge varieties, together with textual content, photos, audio, and video, enabling extra pure and context-rich interactions.
  • Native Device Use: The mannequin seamlessly integrates with numerous instruments and platforms, enhancing its utility throughout totally different purposes.
  • Agentic Talents: Gemini 2.0 introduces AI brokers able to executing advanced duties with minimal human intervention, marking a step in the direction of extra autonomous AI methods.

Variations of Gemini 2.0

Gemini 2.0 is obtainable in a number of variations, every tailor-made for particular use circumstances:

  • Gemini 2.0 Flash Experimental: An experimental mannequin specializing in pace and effectivity, appropriate for fast activity execution.
  • Gemini 2.0 Professional: Designed for a variety of duties, providing a steadiness between efficiency and value.
  • Gemini 2.0 Extremely: Optimized for extremely advanced duties, offering superior efficiency for demanding purposes.

Flask

  • Flask is a light-weight internet framework in Python, perfect for constructing scalable and environment friendly internet purposes.
  • Within the chatbot, Flask handles backend operations, together with API integration with Gemini 2.0, routing, and managing person interactions.
  • Its simplicity and adaptability make it excellent for fast growth and integration duties.

HTML and Bootstrap

  • HTML types the structural basis of the chatbot’s interface, guaranteeing semantic and accessible internet design.
  • Bootstrap, a CSS framework, enhances the interface by offering responsive and aesthetically pleasing design elements. It ensures the chatbot works seamlessly throughout gadgets, from desktops to smartphones.

Key Options of the Medical Chatbot

  • Conversational Interface: The chatbot engages customers with pure, human-like interactions powered by Gemini 2.0.
  • Consumer-friendly Design: Bootstrap-enabled responsive design ensures ease of use on any system.
  • Well being Question Assist: Able to addressing quite a lot of medical questions, from signs to basic well being recommendation.
  • Accessibility: Designed to be straightforward to navigate for customers of all technical proficiencies.

Meta (previously Fb) developed FAISS as an open-source library for environment friendly similarity search and clustering of dense vectors. Machine studying generally makes use of FAISS, particularly for duties involving large-scale vector search and nearest neighbor retrieval. FAISS optimizes dealing with high-dimensional knowledge, making it perfect for purposes equivalent to suggestion methods, pure language processing, and picture retrieval.

In a nutshell, FAISS permits indexing dense vectors and helps quick approximate or precise search over them. It makes use of product quantization, HNSW (Hierarchical Navigable Small World graphs), and IVF (Inverted File Index) methods to steadiness the trade-off between pace and accuracy. These methods dramatically scale back the computational complexity and reminiscence utilization with excessive precision within the search outcomes. Nevertheless, FAISS additional helps each CPU and GPU acceleration, making it appropriate for hundreds of thousands and even billions of vectors for dealing with datasets.

Considered one of FAISS’s key strengths is its versatility. It gives a number of indexing methods, enabling customers to decide on essentially the most acceptable strategy for his or her particular use circumstances. For instance, flat indexes supply precise search capabilities, whereas quantization-based indexes prioritize effectivity. Its Python and C++ APIs make it accessible to a variety of builders, and its modular design permits for simple integration into present machine studying pipelines.

Be taught extra about Vector Database right here.

Flowchart Description: Medical Chatbot Workflow

Beneath is the move diagram:

Flowchart Description: Medical Chatbot Workflow
  • Begin: The person begins on the Dwelling Web page (index.html) of the medical chatbot.
  • Add PDFs to Construct Information Base:
    • Customers add PDF information by way of the File Add performance.
    • Backend processes the PDFs and builds the Information Base, storing it in a Vector Retailer (vector_store.pkl) for future queries.
    • If invalid information are uploaded, the system handles the error and prompts the person for legitimate information.
  • Ask Medical Questions:
    • Customers submit medical questions by way of the Ask Medical Questions (/ask) characteristic.
    • If a Information Base exists, the system retrieves related paperwork.
    • If no Information Base exists, an error message is displayed or a redirect is initiated.
  • Generate Response:
    • Related paperwork are retrieved and handed to the Gemini Mannequin for producing responses.
    • The mannequin processes the enter and gives an correct medical response.
  • Show or Redirect:
    • The generated response is exhibited to the person or redirected to an acceptable web page for additional interplay.
  • Finish: The person receives the response and should select to work together additional or finish the session.

This workflow ensures clean person interplay, environment friendly error dealing with, and correct response technology utilizing the Gemini Mannequin for a seamless medical chatbot expertise.

Setting Up the Surroundings

Start by putting in the required dependencies, configuring the API key, and organising the frontend to organize your atmosphere for the medical chatbot.

Set up the necessities.txt

pip set up -r https://uncooked.githubusercontent.com/Gouravlohar/Medical-Chatbot/refs/heads/grasp/necessities.txt

API Key 

Get your Gemini 2.0 API key from right here.

google AI studio

HTML Entrance-Finish Code for a Medical Chatbot

This HTML code types the front-end person interface of a medical chatbot software. It creates an interactive internet web page the place customers can:

  • Add PDF information to supply further context for the chatbot.
  • Ship chat messages to work together with the AI-based medical chatbot.

The interface makes use of Bootstrap for styling and jQuery for dealing with person interactions dynamically. It consists of options like a typing indicator for the chatbot and seamless message show. The code integrates with a Flask back-end to course of person inputs and return AI-generated responses.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content material="ie=edge">
  <meta title="viewport" content material="width=device-width, initial-scale=1">
  <meta title="description" content material="Demonstration of Gemini API in a Python Flask Utility.">

  <title>Medical Chatbot</title>
  <hyperlink rel="shortcut icon" kind="picture/x-icon" href="https://www.analyticsvidhya.com/weblog/2025/01/medical-chatbot-with-gemini-2-0-flask-and-vector-embedding/{{ url_for("static', filename="photos/iba_logo.png") }}">
  <hyperlink href="https://cdn.jsdelivr.web/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
  <hyperlink href="https://fonts.googleapis.com/css2?household=Inter:wght@300;400;500;600&show=swap" rel="stylesheet">
  <type>
    :root {
      --primary-gradient: linear-gradient(135deg, #6e8efb, #4834d4);
      --chat-bg: #111827;
      --message-bg: #1f2937;
      --user-message-bg: #3730a3;
      --text-primary: #fff;
      --text-secondary: #9ca3af;
    }

    physique {
      font-family: 'Inter', sans-serif;
      background-color: var(--chat-bg);
      coloration: var(--text-primary);
      min-height: 100vh;
      show: flex;
      flex-direction: column;
    }

    .chat-container {
      max-width: 1200px;
      margin: 0 auto;
      padding: 2rem;
      flex: 1;
      show: flex;
      flex-direction: column;
    }

    .title {
      text-align: middle;
      margin-bottom: 1rem;
      font-size: 2rem;
      font-weight: 600;
      coloration: var(--text-primary);
    }

    .warning {
      text-align: middle;
      margin-bottom: 2rem;
      font-size: 1rem;
      coloration: var(--text-secondary);
    }

    .messages-container {
      flex: 1;
      overflow-y: auto;
      padding: 1rem;
      scroll-behavior: clean;
    }

    .message {
      margin-bottom: 1rem;
      opacity: 0;
      rework: translateY(20px);
      animation: fadeIn 0.3s ease forwards;
    }

    .message-content {
      padding: 1rem;
      border-radius: 1rem;
      max-width: 80%;
    }

    .user-message .message-content {
      background: var(--user-message-bg);
      margin-left: auto;
    }

    .bot-message .message-content {
      background: var(--message-bg);
    }

    .input-container {
      padding: 1rem;
      background: var(--chat-bg);
      border-top: 1px strong rgba(255, 255, 255, 0.1);
    }

    .chat-input {
      background: var(--message-bg);
      border: none;
      border-radius: 1.5rem;
      padding: 1rem 1.5rem;
      coloration: var(--text-primary);
      width: calc(100% - 120px);
    }

    .send-button {
      background: var(--primary-gradient);
      border: none;
      border-radius: 1.5rem;
      padding: 1rem 2rem;
      coloration: white;
      font-weight: 600;
      transition: all 0.3s ease;
    }

    .send-button:hover {
      rework: translateY(-2px);
      box-shadow: 0 5px 15px rgba(110, 142, 251, 0.4);
    }

    .typing-indicator {
      show: flex;
      hole: 0.5rem;
      padding: 1rem;
      background: var(--message-bg);
      border-radius: 1rem;
      width: fit-content;
    }

    .typing-dot {
      width: 8px;
      peak: 8px;
      background: var(--text-secondary);
      border-radius: 50%;
      animation: typing 1.4s infinite ease-in-out;
    }

    .typing-dot:nth-child(2) {
      animation-delay: 0.2s;
    }

    .typing-dot:nth-child(3) {
      animation-delay: 0.4s;
    }

    @keyframes typing {
      0%,
      100% {
        rework: translateY(0);
      }

      50% {
        rework: translateY(-10px);
      }
    }

    @keyframes fadeIn {
      to {
        opacity: 1;
        rework: translateY(0);
      }
    }

    /* Message Formatting */
    .bot-message robust {
      coloration: #818cf8;
      font-weight: 600;
    }

    .bot-message ul {
      padding-left: 1.5rem;
      margin: 0.5rem 0;
    }
  </type>
</head>

<physique>
  <div class="chat-container">
    <div class="title">Welcome to Medical Chatbot</div>
    <div class="warning">Observe: That is an AI chatbot and should make errors. Please confirm the knowledge supplied.</div>
    {% with messages = get_flashed_messages() %}
      {% if messages %}
        <div class="alert alert-info" position="alert">
          {{ messages[0] }}
        </div>
      {% endif %}
    {% endwith %}
    <type id="upload-form" methodology="publish" enctype="multipart/form-data" motion="/add">
      <div class="mb-3">
        <label for="pdf_files" class="form-label">Add PDF information</label>
        <enter class="form-control" kind="file" id="pdf_files" title="pdf_files" a number of>
      </div>
      <button kind="submit" class="btn btn-primary">Add PDFs</button>
    </type>
    <div class="messages-container" id="messages-container">
      <!-- Messages can be appended right here -->
    </div>
    <type id="chat-form" methodology="publish">
      <div class="input-container">
        <enter kind="textual content" class="chat-input" id="chat-input" title="immediate" placeholder="Sort your message...">
        <button kind="submit" class="send-button" id="send-button">Ship</button>
      </div>
    </type>
  </div>

  <script src="https://code.jquery.com/jquery-3.6.3.min.js"></script>
  <script>
    $(doc).prepared(operate () {
      $("#chat-form").submit(operate (occasion) {
        occasion.preventDefault();
        var query = $("#chat-input").val();
        if (query.trim() === "") return;

        let userMessage = `
          <div class="message user-message">
            <div class="message-content">
              ${query}
            </div>
          </div>`;
        $("#messages-container").append(userMessage);
        $("#chat-input").val("");

        let typingIndicator = `
          <div class="message bot-message typing-indicator">
            <div class="typing-dot"></div>
            <div class="typing-dot"></div>
            <div class="typing-dot"></div>
          </div>`;
        $("#messages-container").append(typingIndicator);

        $.ajax({
          kind: "POST",
          url: "/ask",
          knowledge: {
            'immediate': query
          },
          success: operate (knowledge) {
            $(".typing-indicator").take away();
            let cleanedData = knowledge
              .change(/**(.*?)**/g, "<robust>$1</robust>")
              .change(/n/g, "<br>")
              .change(/- (.*?)(?=n|$)/g, "<li>$1</li>");
            let botMessage = `
              <div class="message bot-message">
                <div class="message-content">
                  ${cleanedData}
                </div>
              </div>`;
            $("#messages-container").append(botMessage);
          }
        });
      });
    });
  </script>
</physique>

</html>

UI Output

medical chatbot

Constructing a Doc-Powered AI Query Answering System

Think about with the ability to add a couple of PDFs and immediately ask questions on their content material, receiving exact, AI-generated solutions in seconds. That is the promise of a document-powered AI question-answering system. By combining the ability of AI fashions like Gemini, doc embedding methods, and a Flask-based internet interface, you possibly can create an clever software able to understanding, processing, and responding to person queries primarily based on uploaded paperwork. Beneath we’ll stroll you thru the steps to construct such a system, from organising the atmosphere to implementing superior options like similarity search and real-time responses.

Step 1: Imports and Setup

Start by importing essential libraries and modules, equivalent to Flask for the net software, Google Generative AI for mannequin integration, and LangChain for doc dealing with and vector retailer administration.

from flask import Flask, render_template, request, redirect, url_for, flash
import google.generativeai as genai
from langchain_core.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
import os
import logging
import pickle

Step 2: Flask App Initialization and Configuration

Arrange the Flask app, configure key settings just like the add folder for PDFs, and outline a secret key for session administration.

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.secret_key = 'supersecretkey'
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=T

Step 3: Logging Setup

Configure logging to seize vital data and errors, guaranteeing clean debugging and monitoring through the app’s operation.

logging.basicConfig(stage=logging.INFO)
logger = logging.getLogger(__name__)

Step 4: Gemini Mannequin

Initialize the Gemini AI mannequin and configure it together with your API key to allow interplay with the mannequin for producing content material.

mannequin = genai.GenerativeModel('gemini-2.0-flash-exp')
my_api_key_gemini = os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=my_api_key_gemini)

Step 5: Vector Retailer Setup

Arrange a vector retailer to retailer doc embeddings, loading it if already exists, to carry out environment friendly doc similarity searches later.

vector_store = None

# Load present vector retailer if accessible
if os.path.exists('vector_store.pkl'):
    with open('vector_store.pkl', 'rb') as f:
        vector_store = pickle.load(f)
  • vector_store = None: Initializes a variable to retailer the vector retailer.
  • if os.path.exists(‘vector_store.pkl’): Checks if a beforehand saved vector retailer file (vector_store.pkl) exists.

If it exists, the system masses the vector retailer from the file utilizing pickle.load(f).

This shops doc embeddings (numerical representations of the paperwork) for environment friendly similarity search.

Step 6: Error Dealing with (404)

Outline a handler for 404 errors to redirect customers to the house web page once they entry a non-existent route.

@app.errorhandler(404)
def page_not_found(e):
    return redirect(url_for('index'))

If a person accesses a non-existent web page (i.e., a 404 error happens), the system redirects them to the house web page (index).

Step 7: Most important Web page Route (/)

Create the route for the principle web page of the net software, rendering the preliminary HTML template to the person.

@app.route("https://www.analyticsvidhya.com/")
def index():
    return render_template('index.html')

Step 8: File Add Route (/add)

Implement the file add route, permitting customers to add PDF information, course of them, and convert them into embeddings for the vector retailer.

@app.route('/add', strategies=['POST'])
def add():
    world vector_store
    strive:
        if 'pdf_files' not in request.information:
            flash("No file half")
            return redirect(url_for('index'))
        
        information = request.information.getlist('pdf_files')
        paperwork = []
        
        for file in information:
            if file.filename == '':
                flash("No chosen file")
                return redirect(url_for('index'))
            file_path = os.path.be part of(app.config['UPLOAD_FOLDER'], file.filename)
            file.save(file_path)
            
            pdf_loader = PyPDFLoader(file_path)
            paperwork.lengthen(pdf_loader.load())
        
        # Create embeddings utilizing HuggingFaceEmbeddings
        embeddings = HuggingFaceEmbeddings()
        
        if vector_store is None:
            # Create a brand new vector retailer if it does not exist
            vector_store = FAISS.from_documents(paperwork, embeddings)
        else:
            # Add new paperwork to the present vector retailer
            vector_store.add_documents(paperwork)
        
        # Save the up to date vector retailer
        with open('vector_store.pkl', 'wb') as f:
            pickle.dump(vector_store, f)
        
        flash("PDFs uploaded and processed efficiently. The data base is prepared.")
        return redirect(url_for('index'))
    besides Exception as e:
        logger.error("An error occurred whereas processing the PDFs: %s", e)
        flash("An error occurred whereas processing the PDFs.")
        return redirect(url_for('index'))
  • This route permits customers to add PDF information.
  • The request.information object is checked for the presence of the pdf_files key.
  • Information are saved to the uploads listing.
  • PyPDFLoader is used to extract textual content from every uploaded PDF.
  • The extracted textual content is then transformed into embeddings utilizing HuggingFaceEmbeddings.
  • The embeddings are saved within the vector_store (created utilizing FAISS).
  • The up to date vector_store is saved to vector_store.pkl for future use.
  • If the add is profitable, successful message is proven. If there may be an error, an error message is proven.

Step 9: Query Answering Route (/ask)

Implement the question-answering route, the place customers can enter questions, and the app retrieves related paperwork and generates AI-powered responses primarily based on the content material.

@app.route('/ask', strategies=['POST'])
def ask():
    world vector_store
    if vector_store is None:
        return "Information base shouldn't be prepared. Please add PDFs first."

    query = request.type['prompt']
    # Retrieve related paperwork primarily based on the query
    relevant_docs = vector_store.similarity_search(query)
    context = " ".be part of([doc.page_content for doc in relevant_docs])
    custom_prompt = f"You're the finest physician. Solely present medical-related solutions. Context: {context} Query: {query}"

    response = mannequin.generate_content(custom_prompt)

    if response.textual content:
        return response.textual content
    else:
        return "Sorry, however I believe Gemini did not need to reply that!"
  • This route permits customers to ask questions.
  • If the vector retailer shouldn’t be prepared (i.e., no PDFs uploaded but), the app prompts the person to add PDFs first.
  • The person’s query is extracted from the shape.
  • The app performs a similarity search (vector_store.similarity_search) to seek out related paperwork primarily based on the query.
  • The app creates a customized immediate utilizing the context from the retrieved paperwork, instructing the AI mannequin (Gemini) to supply medical-related solutions.
  • The mannequin generates a response utilizing mannequin.generate_content(custom_prompt).
  • If the mannequin gives a response, the app returns it to the person. In any other case, the app returns a default message.

Step 10: Operating the Flask Utility

Lastly, run the Flask app in debug mode to begin the net software and make it accessible for customers to work together with.

if __name__ == '__main__':
    app.run(debug=True)

Get Code on GitHub right here

Output

PDF I used for testing hyperlink

American Journal of Medicine
American Journal of Medication
Medical Chatbot with Gemini 2.0

Immediate

What number of forms of Headache?
Output Medical Chatbot with Gemini 2.0

After importing the PDF, the system gives response straight from its content material.

Conclusion

On this weblog, we now have mentioned learn how to create a Flask-based internet software that makes use of AI instruments and methods to construct a data base from uploaded PDFs. The appliance permits customers to ask medical-related questions and get contextually related solutions primarily based on the content material of the uploaded paperwork by integrating generative fashions like Google Gemini and vector search mechanisms with LangChain. In such a system, AI, allied with fashionable instruments for internet growth, can fully automate data retrieval in an clever interactive expertise.

By understanding the fundamental construction of this code, from file importing to query answering, we see how a fundamental Flask app could be prolonged with highly effective AI capabilities. Whether or not growing a data administration system or just designing a chatbot, the identical mentioned applied sciences can come in useful and get you underway.

Key Takeaways

  • By utilizing instruments like LangChain and FAISS, we are able to convert unstructured PDF paperwork into a robust, searchable data base that responds intelligently to person queries.
  • Medical Chatbot with Gemini 2.0 permits customized, real-time medical help by leveraging superior AI fashions for correct responses.
  • You should use Google’s Gemini mannequin to generate contextually related solutions by interacting with the vectorized paperwork, providing a dynamic and interactive person expertise.
  • Flask serves because the light-weight framework that brings the whole lot collectively, dealing with file uploads, processing, and person interactions seamlessly.
  • FAISS, a robust vector search engine, helps discover essentially the most related paperwork for person queries, enhancing the standard and accuracy of responses.

Ceaselessly Requested Questions

Q1. What’s the function of the /ask route within the Flask software?

A. The /ask route permits customers to submit questions. The app then makes use of the uploaded PDFs to seek out related data and generates a response utilizing Google’s Gemini AI mannequin.

Q2. How does the appliance course of PDFs?

A. The appliance makes use of PyPDFLoader to extract textual content from uploaded PDFs. This textual content is then embedded into vectors utilizing HuggingFaceEmbeddings, and saved in a FAISS vector retailer for quick similarity searches.

Q3. Can I take advantage of this app for paperwork apart from medical?

A. Sure, you possibly can adapt the app to varied domains. By altering the immediate, you possibly can customise the question-answering performance to match totally different fields, equivalent to authorized, academic, or technical.

This autumn. How is the vector retailer saved and loaded?

A. The vector retailer is saved as a .pkl file utilizing Python’s pickle module. The app checks for the file’s existence on startup and masses it if accessible, guaranteeing that beforehand uploaded paperwork persist throughout classes.

Q5. What do I must run this app on my native machine?

A. You want Python and Flask put in, together with dependencies like google.generativeai, langchain, FAISS, and HuggingFaceEmbeddings. You additionally want an API key for Google’s Gemini mannequin. Make certain to arrange a digital atmosphere to handle the dependencies.

Hello I am Gourav, a Information Science Fanatic with a medium basis in statistical evaluation, machine studying, and knowledge visualization. My journey into the world of knowledge started with a curiosity to unravel insights from datasets.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles