File size: 10,156 Bytes
6877857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""App

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1TdjbTSA8V5GUProQ3Bd-uYmTLXSInoWf
"""

import subprocess

def install_espeak():
    try:
        #subprocess.run(["sudo", "apt-get", "install", "espeak", "-y"], check=True)
        subprocess.run(["apt-get", "install", "espeak", "-y"], check=True)  # Removed 'sudo'
        print("eSpeak installed successfully!")
    except subprocess.CalledProcessError as e:
        print(f"Error occurred while installing eSpeak: {e}")

# Call the function to install eSpeak
install_espeak()

import gradio as gr
import numpy as np
from transformers import pipeline
import os
import groq
from langchain_groq import ChatGroq
from langchain.schema import HumanMessage
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Chroma
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.docstore.document import Document
import chardet
import fitz  # PyMuPDF for PDFs
import docx  # python-docx for Word files
import gtts  # Google Text-to-Speech library
from pptx import Presentation  # python-pptx for PowerPoint files
import re

# Initialize Whisper model for speech-to-text
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")

# Set API Key (Ensure it's stored securely in an environment variable)
groq.api_key = os.getenv("GROQ_API_KEY", "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")  # Replace with a valid API key

# Initialize Chat Model
chat_model = ChatGroq(model_name="DeepSeek-R1-Distill-Llama-70b", api_key=groq.api_key)     # llama-3.3-70b-versatile

# Initialize Embeddings
embedding_model = HuggingFaceEmbeddings()

# Initialize ChromaDB
vectorstore = Chroma(embedding_function=embedding_model)

# Prompt for quiz generation with added remark
quiz_prompt = """
You are an AI assistant specialized in education and assessment creation. Given an uploaded document or text, generate a quiz with a mix of multiple-choice questions (MCQs) and fill-in-the-blank questions. The quiz should be directly based on the key concepts, facts, and details from the provided material.
Remove all unnecessary formatting generated by the LLM, including <think> tags, asterisks, markdown formatting, and any bold or italic text, as well as **, ###, ##, and # tags."

For each question:

- Provide 4 answer choices (for MCQs), with only one correct answer.
- Ensure fill-in-the-blank questions focus on key terms, phrases, or concepts from the document.
- Include an answer key for all questions.
- Ensure questions vary in difficulty and encourage comprehension rather than memorization.
- Additionally, implement an instant feedback mechanism:
    - When a user selects an answer, indicate whether it is correct or incorrect.
    - If incorrect, provide a brief explanation from the document to guide learning.
    - Ensure responses are concise and educational to enhance understanding.

Output Example:
1. Fill in the blank: The LLM Agent framework has a central decision-making unit called the _______________________.

Answer: Agent Core

Feedback: The Agent Core is the central component of the LLM Agent framework, responsible for managing goals, tool instructions, planning modules, memory integration, and agent persona.

2. What is the main limitation of LLM-based applications?
a) Limited token capacity
b) Lack of domain expertise
c) Prone to hallucination
d) All of the above

Answer: d) All of the above

Feedback: LLM-based applications have several limitations, including limited token capacity, lack of domain expertise, and being prone to hallucination, among others.


"""

# Function to clean AI response by removing unwanted formatting
def clean_response(response):
    """Removes <think> tags, asterisks, and markdown formatting."""
    cleaned_text = re.sub(r"<think>.*?</think>", "", response, flags=re.DOTALL)  # Remove <think> tags
    cleaned_text = re.sub(r"(\*\*|\*)", "", cleaned_text)  # Remove **bold** and *italics*
    cleaned_text = re.sub(r"^#+\s*", "", cleaned_text, flags=re.MULTILINE)  # Remove # and ### tags
    return cleaned_text.strip()

# Function to generate quiz based on content
def generate_quiz(content):
    prompt = f"{quiz_prompt}\n\nDocument content:\n{content}"
    response = chat_model([HumanMessage(content=prompt)])

    # Apply text cleaning before returning the response
    cleaned_response = clean_response(response.content)

    return cleaned_response


# Function to handle chatbot interactions
def chat_with_groq(user_input):
    try:
        response = chat_model([HumanMessage(content=user_input)])
        cleaned_response_text = clean_response(response.content)  # Clean the response here
        audio_file = speech_playback(cleaned_response_text)  # Play the speech after generating the response
        return cleaned_response_text, audio_file  # Return both response and audio file path
    except Exception as e:
        return f"Error: {str(e)}", None

# Function to play response as speech using gTTS
def speech_playback(text):
    tts = gtts.gTTS(text, lang='en')
    audio_file = "output_audio.mp3"
    tts.save(audio_file)
    return audio_file  # Return the path to the audio file

# Function to detect encoding safely
def detect_encoding(file_path):
    try:
        with open(file_path, "rb") as f:
            raw_data = f.read(4096)  # Read first 4KB for detection
            detected = chardet.detect(raw_data)
            encoding = detected["encoding"]
        return encoding if encoding else "utf-8"  # Default to UTF-8 if detection fails
    except Exception:
        return "utf-8"

# Function to extract text from PDF
def extract_text_from_pdf(pdf_path):
    try:
        doc = fitz.open(pdf_path)
        text = "\n".join([page.get_text("text") for page in doc])
        return text if text.strip() else "No extractable text found."
    except Exception as e:
        return f"Error extracting text from PDF: {str(e)}"

# Function to extract text from Word files (.docx)
def extract_text_from_docx(docx_path):
    try:
        doc = docx.Document(docx_path)
        text = "\n".join([para.text for para in doc.paragraphs])
        return text if text.strip() else "No extractable text found."
    except Exception as e:
        return f"Error extracting text from Word document: {str(e)}"

# Function to extract text from PowerPoint files (.pptx)
def extract_text_from_pptx(pptx_path):
    try:
        presentation = Presentation(pptx_path)
        text = ""
        for slide in presentation.slides:
            for shape in slide.shapes:
                if hasattr(shape, "text"):
                    text += shape.text + "\n"
        return text if text.strip() else "No extractable text found."
    except Exception as e:
        return f"Error extracting text from PowerPoint: {str(e)}"

# Function to process documents safely
def process_document(file):
    try:
        file_extension = os.path.splitext(file.name)[-1].lower()

        if file_extension in [".png", ".jpg", ".jpeg"]:
            return f"Error: Images cannot be processed for text extraction."

        # Extract text based on file type
        if file_extension == ".pdf":
            content = extract_text_from_pdf(file.name)
        elif file_extension == ".docx":
            content = extract_text_from_docx(file.name)
        elif file_extension == ".pptx":
            content = extract_text_from_pptx(file.name)
        else:
            encoding = detect_encoding(file.name)
            with open(file.name, "r", encoding=encoding, errors="replace") as f:
                content = f.read()

        # Process text into chunks
        text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
        documents = [Document(page_content=chunk) for chunk in text_splitter.split_text(content)]
        vectorstore.add_documents(documents)

        # Generate quiz based on document content
        quiz = generate_quiz(content)

        return f"Document processed successfully (File Type: {file_extension}). Quiz generated:\n{quiz}"

    except Exception as e:
        return f"Error processing document: {str(e)}"

# Function to handle speech-to-text conversion
def transcribe_audio(audio):
    sr, y = audio

    # Convert to mono if stereo
    if y.ndim > 1:
        y = y.mean(axis=1)

    y = y.astype(np.float32)
    y /= np.max(np.abs(y))

    return transcriber({"sampling_rate": sr, "raw": y})["text"]

# Gradio UI
with gr.Blocks() as demo:
    gr.HTML("<h2 style='text-align: center;'>AI Tutor</h2>")
    gr.HTML("""
        <div style="text-align: center; margin-bottom: 20px;">
            <img src="https://img.freepik.com/premium-photo/little-girl-is-seen-sitting-front-laptop-computer-engaged-with-nearby-robot-robot-assistant-helping-child-with-homework-ai-generated_585735-12266.jpg" style="max-width: 60%; height: auto; border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,0.2);" />
        </div>
    """)

    with gr.Row():
        with gr.Column():
            audio_input = gr.Audio(type="numpy", label="Record Audio")
            transcription_output = gr.Textbox(label="Transcription")
            user_input = gr.Textbox(label="Ask a question")
            chat_output = gr.Textbox(label="Response")
            audio_output = gr.Audio(label="Audio Playback")  # Add an audio output component
            submit_btn = gr.Button("Ask")
        with gr.Column():
            file_upload = gr.File(label="Upload a document")
            process_status = gr.Textbox(label="Processing Status", interactive=False)
            process_btn = gr.Button("Process Document")

    audio_input.change(fn=transcribe_audio, inputs=audio_input, outputs=transcription_output)
    transcription_output.change(fn=lambda x: x, inputs=transcription_output, outputs=user_input)
    submit_btn.click(chat_with_groq, inputs=user_input, outputs=[chat_output, audio_output])  # Fixed closing brackets
    process_btn.click(process_document, inputs=file_upload, outputs=process_status)

# Launch the Gradio app
demo.launch()