Medical Chatbot with Gemini 2.0, Flask and Vector Embedding

Within the period of AI, chatbots have revolutionized how we work together with know-how. Maybe one of the impactful makes use of is within the healthcare business. Chatbots are capable of 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 venture is about creating a personalised, user-friendly customized platform to reply health-related queries with accuracy and velocity.

Studying Targets

  • Perceive the important thing parts 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 using HTML and Bootstrap to design a responsive and user-friendly chatbot interface.
  • Acquire hands-on expertise in establishing and deploying a personalised medical chatbot.
  • Uncover the function of FAISS in enhancing search effectivity for chatbot functionalities.

What’s Gemini 2.0?

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

Constructing on its predecessor, Gemini 1.5, Gemini 2.0 extends the potential to course of and generate textual content, pictures, 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 initiatives like Undertaking Astra reveal this functionality by integrating with Google providers comparable to Search and Maps to supply real-time, contextual help. One other instance is Undertaking Mariner, a Chrome extension that navigates the online autonomously to carry out duties comparable to on-line buying.

Key Options of Gemini 2.0

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

Variations of Gemini 2.0

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

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

Flask

  • Flask is a light-weight internet framework in Python, ultimate for constructing scalable and environment friendly internet functions.
  • 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 varieties the structural basis of the chatbot’s interface, making certain semantic and accessible internet design.
  • Bootstrap, a CSS framework, enhances the interface by offering responsive and aesthetically pleasing design parts. It ensures the chatbot works seamlessly throughout units, 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 gadget.
  • Well being Question Help: Able to addressing a wide range 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 information, making it ultimate for functions comparable to advice programs, 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 velocity and accuracy. These methods dramatically cut 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 thousands and thousands and even billions of vectors for dealing with datasets.

Certainly one of FAISS’s key strengths is its versatility. It offers 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 circulate diagram:

Flowchart Description: Medical Chatbot Workflow
  • Begin: The person begins on the House Web page (index.html) of the medical chatbot.
  • Add PDFs to Construct Information Base:
    • Customers add PDF information through 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 through 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 offers 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 era utilizing the Gemini Mannequin for a seamless medical chatbot expertise.

Setting Up the Atmosphere

Start by putting in the required dependencies, configuring the API key, and establishing the frontend to arrange your surroundings 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 varieties 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 extra 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 contains 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="pictures/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">
  <model>
    :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);
      colour: 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: heart;
      margin-bottom: 1rem;
      font-size: 2rem;
      font-weight: 600;
      colour: var(--text-primary);
    }

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

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

    .message {
      margin-bottom: 1rem;
      opacity: 0;
      remodel: 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;
      colour: var(--text-primary);
      width: calc(100% - 120px);
    }

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

    .send-button:hover {
      remodel: 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% {
        remodel: translateY(0);
      }

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

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

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

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

<physique>
  <div class="chat-container">
    <div class="title">Welcome to Medical Chatbot</div>
    <div class="warning">Notice: That is an AI chatbot and should make errors. Please confirm the data supplied.</div>
    {% with messages = get_flashed_messages() %}
      {% if messages %}
        <div class="alert alert-info" function="alert">
          {{ messages[0] }}
        </div>
      {% endif %}
    {% endwith %}
    <type id="upload-form" methodology="put up" 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="put up">
      <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(perform () {
      $("#chat-form").submit(perform (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",
          information: {
            'immediate': query
          },
          success: perform (information) {
            $(".typing-indicator").take away();
            let cleanedData = information
              .exchange(/**(.*?)**/g, "<robust>$1</robust>")
              .exchange(/n/g, "<br>")
              .exchange(/- (.*?)(?=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 having the ability to add just a few 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 facility of AI fashions like Gemini, doc embedding methods, and a Flask-based internet interface, you possibly can create an clever device 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 establishing the surroundings to implementing superior options like similarity search and real-time responses.

Step 1: Imports and Setup

Start by importing needed libraries and modules, comparable to Flask for the online 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 necessary data and errors, making certain clean debugging and monitoring through the app’s operation.

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

Step 4: Gemini Mannequin

Initialize the Gemini AI mannequin and configure it along 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 out there
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 hundreds 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: Important Web page Route (/)

Create the route for the principle web page of the online 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
    attempt:
        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.prolong(pdf_loader.load())
        
        # Create embeddings utilizing HuggingFaceEmbeddings
        embeddings = HuggingFaceEmbeddings()
        
        if vector_store is None:
            # Create a brand new vector retailer if it would not exist
            vector_store = FAISS.from_documents(paperwork, embeddings)
        else:
            # Add new paperwork to the prevailing 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 information 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.
  • Recordsdata 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 will not 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 will not 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 offers 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 start out the online 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 offers response straight from its content material.

Conclusion

On this weblog, we’ve got mentioned the way to create a Flask-based internet software that makes use of AI instruments and methods to construct a information 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 trendy instruments for internet growth, can utterly 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 primary Flask app will be prolonged with highly effective AI capabilities. Whether or not creating a information 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 strong, searchable information base that responds intelligently to person queries.
  • Medical Chatbot with Gemini 2.0 permits personalised, real-time medical help by leveraging superior AI fashions for correct responses.
  • You need to 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 all the things collectively, dealing with file uploads, processing, and person interactions seamlessly.
  • FAISS, a strong vector search engine, helps discover essentially the most related paperwork for person queries, enhancing the standard and accuracy of responses.

Continuously Requested Questions

Q1. What’s the goal 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 applying 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, comparable to authorized, instructional, or technical.

This fall. 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 hundreds it if out there, making certain that beforehand uploaded paperwork persist throughout classes.

Q5. What do I have to 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. Be sure to arrange a digital surroundings to handle the dependencies.

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