File size: 13,663 Bytes
e3d7863
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import gradio as gr
import PyPDF2
from io import BytesIO
import tempfile
import os
from pptx import Presentation
import requests
import json
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
import torch
from gtts import gTTS
import numpy as np
import re

# Global variables to store models and data
tokenizer = None
model = None
tts_pipeline = None
uploaded_docs = {}
current_doc = None

def load_models():
    """Load free models from Hugging Face"""
    global tokenizer, model, tts_pipeline
    
    try:
        # Text generation model (free and good for conversations)
        model_name = "microsoft/DialoGPT-medium"
        tokenizer = AutoTokenizer.from_pretrained(model_name)
        model = AutoModelForCausalLM.from_pretrained(model_name)
        
        # Add padding token if it doesn't exist
        if tokenizer.pad_token is None:
            tokenizer.pad_token = tokenizer.eos_token
        
        print("βœ… Models loaded successfully!")
        return "Models loaded successfully!"
        
    except Exception as e:
        print(f"❌ Error loading models: {e}")
        return f"Error loading models: {e}"

def extract_text_from_pdf(pdf_file):
    """Extract text from PDF file"""
    try:
        if pdf_file is None:
            return ""
        
        pdf_reader = PyPDF2.PdfReader(pdf_file.name)
        text = ""
        for page_num, page in enumerate(pdf_reader.pages, 1):
            page_text = page.extract_text()
            text += f"\n--- Page {page_num} ---\n{page_text}\n"
        
        return text.strip()
    except Exception as e:
        return f"Error reading PDF: {str(e)}"

def extract_text_from_ppt(ppt_file):
    """Extract text from PowerPoint file"""
    try:
        if ppt_file is None:
            return ""
        
        presentation = Presentation(ppt_file.name)
        text = ""
        
        for slide_num, slide in enumerate(presentation.slides, 1):
            text += f"\n--- Slide {slide_num} ---\n"
            for shape in slide.shapes:
                if hasattr(shape, "text") and shape.text.strip():
                    text += shape.text + "\n"
        
        return text.strip()
    except Exception as e:
        return f"Error reading PowerPoint: {str(e)}"

def process_document(file, doc_type):
    """Process uploaded document and extract text"""
    global uploaded_docs, current_doc
    
    if file is None:
        return "❌ No file uploaded", "Please upload a document first."
    
    try:
        # Extract text based on file type
        if doc_type == "PDF":
            text_content = extract_text_from_pdf(file)
        elif doc_type == "PowerPoint":
            text_content = extract_text_from_ppt(file)
        else:
            return "❌ Unsupported file type", "Please upload a PDF or PowerPoint file."
        
        if not text_content or text_content.startswith("Error"):
            return f"❌ Failed to process {file.name}", text_content
        
        # Store document
        doc_name = file.name
        uploaded_docs[doc_name] = {
            'content': text_content,
            'type': doc_type,
            'file_path': file.name
        }
        current_doc = doc_name
        
        # Create preview (first 500 characters)
        preview = text_content[:500] + "..." if len(text_content) > 500 else text_content
        
        return f"βœ… Successfully processed: {doc_name}", f"Document Preview:\n\n{preview}"
        
    except Exception as e:
        return f"❌ Error processing {file.name}", f"Error: {str(e)}"

def generate_response(user_input, history, tutor_mode):
    """Generate AI response based on user input and document context"""
    global model, tokenizer, current_doc, uploaded_docs
    
    if not user_input.strip():
        return history, ""
    
    if current_doc is None or current_doc not in uploaded_docs:
        response = "Please upload and process a document first before asking questions."
        history.append([user_input, response])
        return history, ""
    
    try:
        # Get document context
        doc_content = uploaded_docs[current_doc]['content']
        
        # Create context-aware prompt based on tutor mode
        mode_contexts = {
            "Explain Concepts": "As an AI tutor, explain the following concept clearly and simply based on the document content:",
            "Quiz Mode": "Create a quiz question or test the user's understanding of:",
            "Practice Problems": "Provide practice exercises or real-world applications for:"
        }
        
        context_prompt = mode_contexts.get(tutor_mode, "Help me understand:")
        
        # Limit document content to avoid token limits
        limited_content = doc_content[:1000] + "..." if len(doc_content) > 1000 else doc_content
        
        # Create conversation prompt
        prompt = f"{context_prompt} {user_input}\n\nDocument context: {limited_content}\n\nResponse:"
        
        # Generate response using the model
        inputs = tokenizer.encode(prompt, return_tensors="pt", max_length=512, truncation=True)
        
        with torch.no_grad():
            outputs = model.generate(
                inputs,
                max_new_tokens=150,
                num_return_sequences=1,
                temperature=0.7,
                pad_token_id=tokenizer.eos_token_id,
                do_sample=True,
                top_p=0.9
            )
        
        # Decode response
        response = tokenizer.decode(outputs[0], skip_special_tokens=True)
        
        # Extract only the new generated part
        response = response[len(prompt):].strip()
        
        # Fallback if response is empty or too short
        if len(response) < 10:
            fallback_responses = {
                "Explain Concepts": f"Based on your document, {user_input} is an important concept. From what I can see in your materials, this topic involves several key aspects that are worth understanding in detail.",
                "Quiz Mode": f"Here's a question about {user_input}: Based on your document, what are the main points or key takeaways regarding this topic?",
                "Practice Problems": f"Let's practice with {user_input}. Try to apply the concepts from your document to solve a real-world scenario involving this topic."
            }
            response = fallback_responses.get(tutor_mode, f"Great question about {user_input}! From your document, I can help you understand this concept better.")
        
        # Clean up response
        response = response.replace(prompt, "").strip()
        if not response:
            response = f"I understand you're asking about {user_input}. Based on your document, this is an important topic that deserves careful explanation."
        
        # Add to history
        history.append([user_input, response])
        
        return history, ""
        
    except Exception as e:
        error_response = f"I apologize, but I'm having trouble processing your question right now. However, I can tell you that {user_input} is mentioned in your document and is worth exploring further."
        history.append([user_input, error_response])
        return history, ""

def text_to_speech(text):
    """Convert text to speech using gTTS"""
    try:
        if not text or len(text.strip()) == 0:
            return None
        
        # Clean text for TTS
        clean_text = re.sub(r'[^\w\s.,!?]', '', text)
        clean_text = clean_text[:500]  # Limit length for TTS
        
        if len(clean_text.strip()) == 0:
            return None
        
        # Generate speech
        tts = gTTS(text=clean_text, lang='en', slow=False)
        
        # Save to temporary file
        with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
            tts.save(tmp_file.name)
            return tmp_file.name
            
    except Exception as e:
        print(f"TTS Error: {e}")
        return None

def chat_with_speech(user_input, history, tutor_mode):
    """Chat function that includes speech output"""
    # Generate text response
    updated_history, _ = generate_response(user_input, history, tutor_mode)
    
    # Get the last AI response for TTS
    if updated_history and len(updated_history) > 0:
        last_response = updated_history[-1][1]
        audio_file = text_to_speech(last_response)
        return updated_history, "", audio_file
    
    return updated_history, "", None

def get_document_info():
    """Get information about currently loaded document"""
    global current_doc, uploaded_docs
    
    if current_doc and current_doc in uploaded_docs:
        doc = uploaded_docs[current_doc]
        word_count = len(doc['content'].split())
        return f"πŸ“„ Current Document: {current_doc}\nπŸ“Š Type: {doc['type']}\nπŸ“ Word Count: ~{word_count} words\nβœ… Ready for tutoring!"
    else:
        return "❌ No document loaded. Please upload a PDF or PowerPoint file."

# Load models on startup
print("Loading AI models...")
load_models()

# Create Gradio interface
with gr.Blocks(title="🧠 AI Tutor - Free Models", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # 🧠 AI Tutor - Free Models
    
    Upload your PDF or PowerPoint files and start learning with AI-powered tutoring!
    
    **Features:**
    - πŸ“„ Support for PDF and PowerPoint files
    - πŸ€– AI-powered tutoring with free Hugging Face models
    - πŸ”Š Text-to-speech for AI responses
    - πŸ“š Multiple learning modes (Explain, Quiz, Practice)
    """)
    
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("## πŸ“ Document Upload")
            
            # File upload
            file_input = gr.File(
                label="Upload Document",
                file_types=[".pdf", ".pptx", ".ppt"],
                type="filepath"
            )
            
            doc_type = gr.Radio(
                choices=["PDF", "PowerPoint"],
                label="Document Type",
                value="PDF"
            )
            
            process_btn = gr.Button("πŸ“Š Process Document", variant="primary")
            
            # Document status
            doc_status = gr.Textbox(
                label="Status",
                interactive=False,
                placeholder="Upload a document to get started..."
            )
            
            # Document preview
            doc_preview = gr.Textbox(
                label="Document Preview",
                interactive=False,
                lines=8,
                placeholder="Document content will appear here..."
            )
            
            # Current document info
            gr.Markdown("## πŸ“‹ Document Info")
            doc_info = gr.Textbox(
                label="Current Document",
                interactive=False,
                lines=4
            )
            
            # Update document info periodically
            doc_info_btn = gr.Button("πŸ”„ Refresh Info")
        
        with gr.Column(scale=2):
            gr.Markdown("## πŸ’¬ AI Tutor Chat")
            
            # Tutor mode selection
            tutor_mode = gr.Radio(
                choices=["Explain Concepts", "Quiz Mode", "Practice Problems"],
                label="🎯 Learning Mode",
                value="Explain Concepts"
            )
            
            # Chat interface
            chatbot = gr.Chatbot(
                label="Chat with AI Tutor",
                height=400,
                bubble_full_width=False
            )
            
            with gr.Row():
                msg_input = gr.Textbox(
                    label="Your Question",
                    placeholder="Ask me anything about your document...",
                    scale=4
                )
                send_btn = gr.Button("πŸ“€ Send", scale=1, variant="primary")
            
            # Audio output
            audio_output = gr.Audio(
                label="πŸ”Š AI Response (Audio)",
                type="filepath",
                autoplay=True
            )
            
            # Clear chat button
            clear_btn = gr.Button("πŸ—‘οΈ Clear Chat")
    
    # Event handlers
    process_btn.click(
        fn=process_document,
        inputs=[file_input, doc_type],
        outputs=[doc_status, doc_preview]
    )
    
    send_btn.click(
        fn=chat_with_speech,
        inputs=[msg_input, chatbot, tutor_mode],
        outputs=[chatbot, msg_input, audio_output]
    )
    
    msg_input.submit(
        fn=chat_with_speech,
        inputs=[msg_input, chatbot, tutor_mode],
        outputs=[chatbot, msg_input, audio_output]
    )
    
    clear_btn.click(
        fn=lambda: ([], None),
        outputs=[chatbot, audio_output]
    )
    
    doc_info_btn.click(
        fn=get_document_info,
        outputs=[doc_info]
    )
    
    # Load document info on startup
    demo.load(
        fn=get_document_info,
        outputs=[doc_info]
    )

    gr.Markdown("""
    ## πŸš€ How to Use:
    1. **Upload** your PDF or PowerPoint file
    2. **Process** the document to extract text
    3. **Choose** your learning mode (Explain, Quiz, or Practice)
    4. **Start chatting** with the AI tutor about your document
    5. **Listen** to AI responses with text-to-speech
    
    ## πŸ”§ Models Used:
    - **Text Generation**: Microsoft DialoGPT-medium (Free)
    - **Text-to-Speech**: Google TTS (gTTS) - Free
    - **Document Processing**: PyPDF2 & python-pptx (Free)
    """)

if __name__ == "__main__":
    demo.launch(
        share=True,
        server_name="0.0.0.0",
        server_port=7860
    )