Build Your Own AI Tutor with Streamlit and OpenAI
This article demonstrates how to create a versatile AI tutoring application using Streamlit and OpenAI's powerful language models. This application offers a range of functionalities, from answering course-related questions to generating code, fixing bugs, providing math solutions, and even offering subject-specific assistance in biology, chemistry, and physics. We'll also explore how to integrate external apps for voice chat, image chat, translation, image generation, graph tutorials, and text-to-diagram generation.
Demo: Click to View Demo
Key Features
- Course Query Assistant: Upload course materials (PDFs) and ask questions. The AI will provide answers based on the context of the uploaded documents.
- Code Generator: Describe the code you need, and the AI will generate it for you. Supports saving and downloading the generated code.
- AI Chatbot Tutor: Engage in a conversational chat with an AI tutor to get help with various subjects.
- AI Study Notes & Summaries: Upload course materials, and the AI will generate concise study notes and summaries.
- Code Bug Fixer: Paste buggy code, and the AI will attempt to identify and fix the issues.
- Mathematics Assistant: Ask math questions, and the AI will provide step-by-step solutions.
- Subject-Specific Assistants: Get targeted help in Biology, Chemistry, and Physics.
- External App Integrations: Seamlessly link to external apps for voice chat, image chat, English-to-Japanese translation, text-to-image generation, graph tutorials, and text-to-diagram generation.
Technologies Used
- Streamlit: A Python library for creating interactive web apps quickly and easily.
- OpenAI API: Access to OpenAI's powerful language models, including GPT-4 and GPT-3.5-turbo.
- PyPDF2: A library for working with PDF files in Python.
- FAISS: A library for efficient similarity search and clustering of dense vectors.
- NumPy: A library for numerical computing in Python.
- Scikit-learn: A library for machine learning in Python.
- PIL (Pillow): A library for image processing in Python.
Code Breakdown
The Python code for this application is structured around several key functions:
extract_text_from_pdf(pdf_file)
: Extracts text from a PDF file.get_embeddings(text)
: Generates text embeddings using OpenAI's embedding models.search_similar(query_embedding, index, stored_texts)
: Searches for similar text chunks using FAISS.generate_code_from_prompt(prompt)
: Generates code based on a prompt.save_code_to_file(code, filename)
: Saves code to a file.generate_summary(text)
: Generates a summary of a given text.fix_code_bugs(buggy_code)
: Attempts to fix bugs in code.generate_math_solution(query)
: Generates solutions to math problems.
The Streamlit app uses these functions to create the interactive user interface and handle user input. It also incorporates custom CSS and JavaScript for styling and animations.
Setting Up the Application
Install Libraries:
pip install streamlit PyPDF2 openai faiss-cpu numpy scikit-learn pillow
Obtain OpenAI API Key: Sign up for an OpenAI account and obtain your API key.
Clone the Repository (Optional): If the code is available on a repository (e.g., GitHub), clone it.
Run the Streamlit App:
streamlit run your_app_name.py
Using the Application
Enter OpenAI API Key: Enter your OpenAI API key in the designated field.
Select Mode: Choose the desired mode from the sidebar (e.g., "Course Query Assistant," "Code Generator," etc.).
Follow the Instructions: Each mode will have specific instructions for use. For example, in "Course Query Assistant," you'll upload PDF files and then ask questions.
Expanding the Application
This application can be further expanded by:
- Adding More Modes: Implement new functionalities, such as language translation, essay writing assistance, or interactive quizzes.
- Improving User Interface: Enhance the UI with more interactive elements and visualizations.
- Integrating with Other APIs: Connect to other APIs for tasks like image recognition, voice synthesis, or data analysis.
- Deploying the Application: Deploy the Streamlit app to a cloud platform to make it accessible to others.
Conclusion
This article provides a comprehensive guide to building your own AI tutor using Streamlit and OpenAI. The modular design of the code makes it easy to customize and expand the application with new features and integrations. By leveraging the power of AI, this tool can be a valuable asset for students and learners of all ages.