The AI Tutor Chatbot


AI-Powered Tutor: Detailed Answers in Text & Audio.

This AI Tutor Chatbot acts as your personal learning assistant, instantly generating detailed answers to your questions. It then uses lifelike text-to-speech technology to read those explanations aloud. This multi-sensory approach caters to different learning styles and enhances understanding.

Project Source Code

Tools & Tech Stack

Folder Structure

1. Create a Virtual Environment

2. Install Required Libraries

Run the following command to install the required dependencies:

pip install flask
pip install gtts
pip install google.generativeai
pip install IPython

3. Core AI and Text-to-Speech Utilities (helper.py)

This file contains two core utility functions that provide essential services for an AI chatbot application:

1. gtts_tts()

This function is the "voice" of the application. It takes any text (ideally the AI's response) and converts it into an audio file (MP3) using Google's Text-to-Speech (gTTS) service. Its purpose is to create a spoken version of the text, enabling the audio playback feature in the application.

def gtts_tts(text, lang='en', output_file="gTTS_output.mp3"):
 if not text  raise ValueError("Text for gTTS cannot be empty.") tts = gTTS(text=text, lang=lang, slow=False) tts.save(output_file) print(f"Audio saved as {output_file}") return output_path

Arguments:

Returns: The path to the generated audio file.

2. ask_gemini()

This function is the "brain" of the operation. It takes a user's question, sends it to Google's Gemini AI model with a specific prompt requesting a concise answer, and returns the generated text response. Its purpose is to intelligently process queries and provide informative, brief answers.

def ask_gemini(user_question):  import google.generativeai as genai   # Configure the Gemini API  genai.configure(api_key="Paste your key here")   # Initialize the Gemini model  model = genai.GenerativeModel("gemini-1.5-flash")  if not user_question:     print("No question entered. Please try again.")     return None   prompt = f"Give me a quick, clear answer (3-4 lines) to this question: {user_question}. Be informative yet brief."  try:    response = model.generate_content(prompt)    return response.text.strip()  except Exception as e:    print(f"Error generating response with Gemini: {e}")    return None

Note: You must obtain your own Gemini API key from Google AI Studio. The key in the code ("Paste your key here") is a placeholder and will not work. You should replace it with your valid key and never expose it publicly.

Arguments:

Process: The function first constructs a detailed prompt from the user's question and then passes it to the model.generate_content() method to retrieve the response from the Gemini model.

Step 4: Frontend User Interface (index.html)

The index.html file serves as the front-end user interface for this web-based "Study Assistant Chatbot." It provides an interactive chat environment where users can ask questions and receive AI-generated answers in both text and audio form.

Key Components and Their Purpose:

1. Structure & Styling (HTML & CSS): 2. Core Functionality (JavaScript):

Step 5: Server-Side Application Logic (app.py)

The app.py file is the backend server for the "Study Assistant Chatbot," built using the Flask framework. It acts as the brain of the operation, handling all the logic that the front-end interface (index.html) cannot.

Key Components and Their Purpose: