|
| 1 | +# =============================== |
| 2 | +# 1. 📘 Project Title & Purpose |
| 3 | +# =============================== |
| 4 | +# """ |
| 5 | +# AI‑Powered Personalized Learning Assistant with Smart Content Chatbot |
| 6 | + |
| 7 | +# This assistant helps users learn any topic by chatting with an AI tutor. |
| 8 | +# It uses PraisonAI Agents, OpenAI API, and Transformers to deliver answers clearly. |
| 9 | +# """ |
| 10 | + |
| 11 | +# ============================ |
| 12 | +# 2. 📦 Dependencies Required |
| 13 | +# ============================ |
| 14 | +# """ |
| 15 | +# Required Libraries: |
| 16 | +# - praisonaiagents |
| 17 | +# - openai |
| 18 | +# - transformers |
| 19 | + |
| 20 | +# Make sure to install these in Colab or terminal: |
| 21 | +# !pip install praisonaiagents openai transformers |
| 22 | +# """ |
| 23 | + |
| 24 | +# ===================== |
| 25 | +# 3. 🧰 Tools Used |
| 26 | +# ===================== |
| 27 | +# """ |
| 28 | +# ✅ praisonaiagents – Builds the smart tutoring agent |
| 29 | +# ✅ openai – Uses OpenAI GPT for accurate answers |
| 30 | +# ✅ transformers – Fallback model using GPT-2 |
| 31 | +# ✅ Python I/O – Simple interactive CLI using input/print |
| 32 | +# """ |
| 33 | + |
| 34 | +# ============================ |
| 35 | +# 4. 🔐 Set OpenAI API Key |
| 36 | +# ============================ |
| 37 | +import os |
| 38 | +os.environ['OPENAI_API_KEY'] = input('Enter your OpenAI API key: ') |
| 39 | + |
| 40 | +# ================================ |
| 41 | +# 5. 🧠 Initialize AI Agent + Fallback |
| 42 | +# ================================ |
| 43 | +from praisonaiagents import Agent |
| 44 | +from transformers import pipeline as hf_pipeline |
| 45 | +import openai |
| 46 | + |
| 47 | +openai.api_key = os.getenv('OPENAI_API_KEY') |
| 48 | +hf_chatbot = hf_pipeline('text-generation', model='gpt2') |
| 49 | + |
| 50 | +agent = Agent(instructions='You are a friendly tutor. Answer learner questions clearly and helpfully.') |
| 51 | + |
| 52 | +def chat_with_ai(user_input): |
| 53 | + try: |
| 54 | + return agent.start(user_input) |
| 55 | + except Exception as e: |
| 56 | + print('[Fallback GPT-2]', e) |
| 57 | + return hf_chatbot(user_input, max_length=100)[0]['generated_text'] |
| 58 | + |
| 59 | +# ============================ |
| 60 | +# 6. 💬 Interactive Chat Loop |
| 61 | +# ============================ |
| 62 | +print("Start chatting with your AI tutor! Type 'exit' to quit.") |
| 63 | +while True: |
| 64 | + user_input = input('You: ') |
| 65 | + if user_input.lower() in ['exit', 'quit']: |
| 66 | + print('Goodbye! Happy learning!') |
| 67 | + break |
| 68 | + response = chat_with_ai(user_input) |
| 69 | + print('🤖 Tutor:', response) |
0 commit comments