|
|
|
"""app |
|
|
|
Automatically generated by Colab. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/1pwwcBb5Zlw1DA3u5K8W8mjrwBTBWXc1L |
|
""" |
|
|
|
import gradio as gr |
|
import numpy as np |
|
from transformers import pipeline |
|
import os |
|
import time |
|
import groq |
|
import uuid |
|
|
|
|
|
from langchain_groq import ChatGroq |
|
from langchain.schema import HumanMessage |
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
from langchain_community.vectorstores import Chroma |
|
from langchain_community.embeddings import HuggingFaceEmbeddings |
|
from langchain.docstore.document import Document |
|
|
|
|
|
import chardet |
|
|
|
import fitz |
|
import docx |
|
import gtts |
|
from pptx import Presentation |
|
import re |
|
|
|
|
|
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en") |
|
|
|
|
|
groq.api_key = os.getenv("GROQ_API_KEY") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chat_model = ChatGroq(model_name="llama-3.3-70b-versatile", api_key=groq.api_key) |
|
|
|
|
|
|
|
embedding_model = HuggingFaceEmbeddings() |
|
vectorstore = Chroma(embedding_function=embedding_model) |
|
|
|
|
|
|
|
"""app |
|
|
|
Automatically generated by Colab. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/1jdKA4WQJcLb0_aQ3vtGVM46B1wriSsDv |
|
""" |
|
|
|
import gradio as gr |
|
import numpy as np |
|
from transformers import pipeline |
|
import os |
|
import time |
|
import groq |
|
import uuid |
|
|
|
|
|
from langchain_groq import ChatGroq |
|
from langchain.schema import HumanMessage |
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
from langchain_community.vectorstores import Chroma |
|
from langchain_community.embeddings import HuggingFaceEmbeddings |
|
from langchain.docstore.document import Document |
|
|
|
|
|
import chardet |
|
|
|
import fitz |
|
import docx |
|
import gtts |
|
from pptx import Presentation |
|
import re |
|
|
|
|
|
|
|
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en") |
|
|
|
|
|
groq.api_key = os.getenv("GROQ_API_KEY", "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") |
|
|
|
|
|
chat_model = ChatGroq(model_name="llama-3.3-70b-versatile", api_key=groq.api_key) |
|
|
|
|
|
os.makedirs("chroma_db", exist_ok=True) |
|
embedding_model = HuggingFaceEmbeddings() |
|
|
|
vectorstore = Chroma( |
|
embedding_function=embedding_model, |
|
persist_directory="chroma_db" |
|
) |
|
vectorstore.persist() |
|
|
|
|
|
|
|
chat_memory = [] |
|
|
|
|
|
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. |
|
Generate 20 Questions. |
|
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. |
|
|
|
3. Given the following info, what is the value of P(jam|Rain)? |
|
P(no Rain) = 0.8; |
|
P(no Jam) = 0.2; |
|
P(Rain|Jam) = 0.1 |
|
|
|
a) 0.016 |
|
b) 0.025 |
|
c) 0.1 |
|
d) 0.4 |
|
|
|
Answer: d) 0.4 |
|
|
|
Feedback: This question tests understanding of Bayes’ Theorem by requiring the calculation of conditional probability using the given values. |
|
""" |
|
|
|
|
|
def clean_response(response): |
|
"""Removes <think> tags, asterisks, and markdown formatting.""" |
|
cleaned_text = re.sub(r"<think>.*?</think>", "", response, flags=re.DOTALL) |
|
cleaned_text = re.sub(r"(\*\*|\*|\[|\])", "", cleaned_text) |
|
cleaned_text = re.sub(r"^##+\s*", "", cleaned_text, flags=re.MULTILINE) |
|
cleaned_text = re.sub(r"\\", "", cleaned_text) |
|
cleaned_text = re.sub(r"---", "", cleaned_text) |
|
return cleaned_text.strip() |
|
|
|
|
|
def generate_quiz(content): |
|
prompt = f"{quiz_prompt}\n\nDocument content:\n{content}" |
|
response = chat_model([HumanMessage(content=prompt)]) |
|
cleaned_response = clean_response(response.content) |
|
return cleaned_response |
|
|
|
|
|
def retrieve_documents(query): |
|
results = vectorstore.similarity_search(query, k=3) |
|
return [doc.page_content for doc in results] |
|
|
|
|
|
def chat_with_groq(user_input): |
|
try: |
|
|
|
relevant_docs = retrieve_documents(user_input) |
|
context = "\n".join(relevant_docs) if relevant_docs else "No relevant documents found." |
|
|
|
|
|
system_prompt = "You are a helpful AI assistant. Answer questions accurately and concisely." |
|
conversation_history = "\n".join(chat_memory[-10:]) |
|
prompt = f"{system_prompt}\n\nConversation History:\n{conversation_history}\n\nUser Input: {user_input}\n\nContext:\n{context}" |
|
|
|
|
|
response = chat_model([HumanMessage(content=prompt)]) |
|
|
|
|
|
cleaned_response_text = clean_response(response.content) |
|
|
|
|
|
chat_memory.append(f"User: {user_input}") |
|
chat_memory.append(f"AI: {cleaned_response_text}") |
|
|
|
|
|
audio_file = speech_playback(cleaned_response_text) |
|
|
|
|
|
return [(user_input, cleaned_response_text)], audio_file |
|
except Exception as e: |
|
return [("Error", str(e))], None |
|
|
|
|
|
|
|
def speech_playback(text): |
|
try: |
|
|
|
unique_id = str(uuid.uuid4()) |
|
audio_file = f"output_audio_{unique_id}.mp3" |
|
|
|
|
|
tts = gtts.gTTS(text, lang='en') |
|
tts.save(audio_file) |
|
|
|
|
|
return audio_file |
|
except Exception as e: |
|
print(f"Error in speech_playback: {e}") |
|
return None |
|
|
|
|
|
def detect_encoding(file_path): |
|
try: |
|
with open(file_path, "rb") as f: |
|
raw_data = f.read(4096) |
|
detected = chardet.detect(raw_data) |
|
encoding = detected["encoding"] |
|
return encoding if encoding else "utf-8" |
|
except Exception: |
|
return "utf-8" |
|
|
|
|
|
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)}" |
|
|
|
|
|
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)}" |
|
|
|
|
|
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)}" |
|
|
|
|
|
def process_document(file): |
|
try: |
|
file_extension = os.path.splitext(file.name)[-1].lower() |
|
if file_extension in [".png", ".jpg", ".jpeg"]: |
|
return "Error: Images cannot be processed for text extraction." |
|
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() |
|
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) |
|
vectorstore.persist() |
|
|
|
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)}" |
|
|
|
|
|
def transcribe_audio(audio): |
|
sr, y = audio |
|
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"] |
|
|
|
|
|
def chat_with_groq(user_input): |
|
try: |
|
|
|
relevant_docs = retrieve_documents(user_input) |
|
context = "\n".join(relevant_docs) if relevant_docs else "No relevant documents found." |
|
|
|
|
|
system_prompt = "You are a helpful AI assistant. Answer questions accurately and concisely." |
|
conversation_history = "\n".join(chat_memory[-10:]) |
|
prompt = f"{system_prompt}\n\nConversation History:\n{conversation_history}\n\nUser Input: {user_input}\n\nContext:\n{context}" |
|
|
|
|
|
response = chat_model([HumanMessage(content=prompt)]) |
|
|
|
|
|
cleaned_response_text = clean_response(response.content) |
|
|
|
|
|
chat_memory.append(f"User: {user_input}") |
|
chat_memory.append(f"AI: {cleaned_response_text}") |
|
|
|
|
|
audio_file = speech_playback(cleaned_response_text) |
|
|
|
|
|
return [(user_input, cleaned_response_text)], audio_file |
|
except Exception as e: |
|
return [("Error", str(e))], None |
|
|
|
|
|
|
|
def tutor_ai_chatbot(): |
|
"""Main Gradio interface for the Tutor AI Chatbot.""" |
|
with gr.Blocks() as app: |
|
gr.Markdown("# 📚 AI Tutor - We.(POC)") |
|
gr.Markdown("An interactive Personal AI Tutor chatbot to help with your learning needs.") |
|
|
|
|
|
with gr.Tab("AI Chatbot"): |
|
with gr.Row(): |
|
with gr.Column(scale=3): |
|
chatbot = gr.Chatbot(height=500) |
|
with gr.Row(): |
|
msg = gr.Textbox(label="Ask a question", placeholder="Type your question here...") |
|
submit = gr.Button("Send") |
|
|
|
|
|
with gr.Column(scale=1): |
|
audio_input = gr.Audio(type="numpy", label="Record or Upload Audio") |
|
|
|
|
|
with gr.Column(scale=1): |
|
audio_playback = gr.Audio(label="Audio Response", type="filepath") |
|
|
|
|
|
clear_btn = gr.Button("Clear Chat") |
|
|
|
|
|
submit.click( |
|
chat_with_groq, |
|
inputs=[msg], |
|
outputs=[chatbot, audio_playback] |
|
) |
|
|
|
|
|
def clear_chat_history(): |
|
return None, None |
|
|
|
clear_btn.click(clear_chat_history, inputs=None, outputs=[chatbot, audio_playback]) |
|
|
|
|
|
msg.submit( |
|
chat_with_groq, |
|
inputs=[msg], |
|
outputs=[chatbot, audio_playback] |
|
) |
|
|
|
|
|
with gr.Accordion("Example Questions", open=False): |
|
gr.Examples( |
|
examples=[ |
|
"Can you explain the concept of RLHF AI?", |
|
"What are AI transformers?", |
|
"What is MoE AI?", |
|
"What's gate networks AI?", |
|
"I am making a switch, please generating baking recipe?" |
|
], |
|
inputs=msg |
|
) |
|
|
|
|
|
with gr.Tab("Upload Notes & Generate Quiz"): |
|
with gr.Row(): |
|
with gr.Column(scale=2): |
|
file_input = gr.File(label="Upload Lecture Notes (PDF, DOCX, PPTX)") |
|
|
|
with gr.Column(scale=3): |
|
quiz_output = gr.Textbox(label="Generated Quiz", lines=10) |
|
|
|
|
|
|
|
with gr.Tab("Introduction Video"): |
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
|
|
gr.Markdown("### Welcome to the Introduction Video") |
|
gr.Markdown("Music from Xu Mengyuan - China-O, musician Xu Mengyuan YUAN! | 徐梦圆 - China-O 音乐人徐梦圆YUAN! ") |
|
gr.Video("https://github.com/lesterchia1/AI_tutor/raw/main/We%20not%20me%20video.mp4", label="Introduction Video") |
|
|
|
|
|
|
|
audio_input.change(fn=transcribe_audio, inputs=audio_input, outputs=msg) |
|
file_input.change(process_document, inputs=file_input, outputs=quiz_output) |
|
|
|
|
|
|
|
app.launch(share=True) |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
tutor_ai_chatbot() |
|
|
|
|
|
|
|
|
|
|
|
|
|
chat_memory = [] |
|
|
|
|
|
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. |
|
Generate 20 Questions. |
|
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. |
|
""" |
|
|
|
|
|
def clean_response(response): |
|
"""Removes <think> tags, asterisks, and markdown formatting.""" |
|
cleaned_text = re.sub(r"<think>.*?</think>", "", response, flags=re.DOTALL) |
|
cleaned_text = re.sub(r"(\*\*|\*|\[|\])", "", cleaned_text) |
|
cleaned_text = re.sub(r"^##+\s*", "", cleaned_text, flags=re.MULTILINE) |
|
cleaned_text = re.sub(r"\\", "", cleaned_text) |
|
cleaned_text = re.sub(r"---", "", cleaned_text) |
|
return cleaned_text.strip() |
|
|
|
|
|
def generate_quiz(content): |
|
prompt = f"{quiz_prompt}\n\nDocument content:\n{content}" |
|
response = chat_model([HumanMessage(content=prompt)]) |
|
cleaned_response = clean_response(response.content) |
|
return cleaned_response |
|
|
|
|
|
def retrieve_documents(query): |
|
results = vectorstore.similarity_search(query, k=3) |
|
return [doc.page_content for doc in results] |
|
|
|
|
|
def chat_with_groq(user_input): |
|
try: |
|
|
|
relevant_docs = retrieve_documents(user_input) |
|
context = "\n".join(relevant_docs) if relevant_docs else "No relevant documents found." |
|
|
|
|
|
system_prompt = "You are a helpful AI assistant. Answer questions accurately and concisely." |
|
conversation_history = "\n".join(chat_memory[-10:]) |
|
prompt = f"{system_prompt}\n\nConversation History:\n{conversation_history}\n\nUser Input: {user_input}\n\nContext:\n{context}" |
|
|
|
|
|
response = chat_model([HumanMessage(content=prompt)]) |
|
|
|
|
|
cleaned_response_text = clean_response(response.content) |
|
|
|
|
|
chat_memory.append(f"User: {user_input}") |
|
chat_memory.append(f"AI: {cleaned_response_text}") |
|
|
|
|
|
audio_file = speech_playback(cleaned_response_text) |
|
|
|
|
|
return [(user_input, cleaned_response_text)], audio_file |
|
except Exception as e: |
|
return [("Error", str(e))], None |
|
|
|
|
|
|
|
def speech_playback(text): |
|
try: |
|
|
|
unique_id = str(uuid.uuid4()) |
|
audio_file = f"output_audio_{unique_id}.mp3" |
|
|
|
|
|
tts = gtts.gTTS(text, lang='en') |
|
tts.save(audio_file) |
|
|
|
|
|
return audio_file |
|
except Exception as e: |
|
print(f"Error in speech_playback: {e}") |
|
return None |
|
|
|
|
|
def detect_encoding(file_path): |
|
try: |
|
with open(file_path, "rb") as f: |
|
raw_data = f.read(4096) |
|
detected = chardet.detect(raw_data) |
|
encoding = detected["encoding"] |
|
return encoding if encoding else "utf-8" |
|
except Exception: |
|
return "utf-8" |
|
|
|
|
|
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)}" |
|
|
|
|
|
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)}" |
|
|
|
|
|
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)}" |
|
|
|
|
|
def process_document(file): |
|
try: |
|
file_extension = os.path.splitext(file.name)[-1].lower() |
|
if file_extension in [".png", ".jpg", ".jpeg"]: |
|
return "Error: Images cannot be processed for text extraction." |
|
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() |
|
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) |
|
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)}" |
|
|
|
|
|
def transcribe_audio(audio): |
|
sr, y = audio |
|
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"] |
|
|
|
|
|
def chat_with_groq(user_input): |
|
try: |
|
|
|
relevant_docs = retrieve_documents(user_input) |
|
context = "\n".join(relevant_docs) if relevant_docs else "No relevant documents found." |
|
|
|
|
|
system_prompt = "You are a helpful AI assistant. Answer questions accurately and concisely." |
|
conversation_history = "\n".join(chat_memory[-10:]) |
|
prompt = f"{system_prompt}\n\nConversation History:\n{conversation_history}\n\nUser Input: {user_input}\n\nContext:\n{context}" |
|
|
|
|
|
response = chat_model([HumanMessage(content=prompt)]) |
|
|
|
|
|
cleaned_response_text = clean_response(response.content) |
|
|
|
|
|
chat_memory.append(f"User: {user_input}") |
|
chat_memory.append(f"AI: {cleaned_response_text}") |
|
|
|
|
|
audio_file = speech_playback(cleaned_response_text) |
|
|
|
|
|
return [(user_input, cleaned_response_text)], audio_file |
|
except Exception as e: |
|
return [("Error", str(e))], None |
|
|
|
|
|
|
|
|
|
|
|
def tutor_ai_chatbot(): |
|
"""Main Gradio interface for the Tutor AI Chatbot.""" |
|
with gr.Blocks() as app: |
|
gr.Markdown("# 📚 AI Tutor - We.(POC)") |
|
gr.Markdown("An interactive Personal AI Tutor chatbot to help with your learning needs.") |
|
|
|
|
|
with gr.Tab("AI Chatbot"): |
|
with gr.Row(): |
|
with gr.Column(scale=3): |
|
chatbot = gr.Chatbot(height=500) |
|
with gr.Row(): |
|
msg = gr.Textbox(label="Ask a question", placeholder="Type your question here...") |
|
submit = gr.Button("Send") |
|
|
|
|
|
|
|
|
|
with gr.Column(scale=1): |
|
audio_input = gr.Audio(type="numpy", label="Record or Upload Audio") |
|
|
|
|
|
with gr.Column(scale=1): |
|
audio_playback = gr.Audio(label="Audio Response", type="filepath") |
|
|
|
|
|
clear_btn = gr.Button("Clear Chat") |
|
|
|
|
|
submit.click( |
|
chat_with_groq, |
|
inputs=[msg], |
|
outputs=[chatbot, audio_playback] |
|
) |
|
|
|
|
|
def clear_chat_history(): |
|
return None, None |
|
|
|
clear_btn.click(clear_chat_history, inputs=None, outputs=[chatbot, audio_playback]) |
|
|
|
|
|
msg.submit( |
|
chat_with_groq, |
|
inputs=[msg], |
|
outputs=[chatbot, audio_playback] |
|
) |
|
|
|
|
|
with gr.Accordion("Example Questions", open=False): |
|
gr.Examples( |
|
examples=[ |
|
"Can you explain the concept of RLHF AI?", |
|
"What are AI transformers?", |
|
"What is MoE AI?", |
|
"What's gate networks AI?", |
|
"I am making a switch, please generating baking recipe?" |
|
], |
|
inputs=msg |
|
) |
|
|
|
|
|
with gr.Tab("Upload Notes & Generate Quiz"): |
|
with gr.Row(): |
|
with gr.Column(scale=2): |
|
file_input = gr.File(label="Upload Lecture Notes (PDF, DOCX, PPTX) [Must be less than 6k of words]") |
|
|
|
with gr.Column(scale=3): |
|
quiz_output = gr.Textbox(label="Generated Quiz", lines=10) |
|
|
|
|
|
|
|
with gr.Tab("Introduction Video"): |
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
|
|
gr.Markdown("### Welcome to the Introduction Video") |
|
gr.Markdown("Music from Xu Mengyuan - China-O, musician Xu Mengyuan YUAN! | 徐梦圆 - China-O 音乐人徐梦圆YUAN! ") |
|
gr.Video("https://github.com/lesterchia1/AI_tutor/raw/main/We%20not%20me%20video.mp4", label="Introduction Video") |
|
|
|
|
|
|
|
|
|
audio_input.change(fn=transcribe_audio, inputs=audio_input, outputs=msg) |
|
file_input.change(process_document, inputs=file_input, outputs=quiz_output) |
|
|
|
|
|
|
|
app.launch(share=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
tutor_ai_chatbot() |