codingSarvesh/study-ai-tool
0
1import os2import gradio as gr3import fitz # PyMuPDF4import pytesseract5from PIL import Image6from transformers import pipeline7from moviepy.editor import VideoFileClip8import yt_dlp9import tempfile10 11# Load chatbot pipeline12chatbot = pipeline("text-generation", model="gpt2")13 14# Function to process various file types15def process_file(file, subject):16 if file is None:17 return "No file uploaded."18 file_path = file.name19 ext = os.path.splitext(file_path)[1].lower()20 21 text = ""22 23 if ext == ".pdf":24 with fitz.open(file_path) as doc:25 for page in doc:26 text += page.get_text()27 28 elif ext in [".png", ".jpg", ".jpeg"]:29 image = Image.open(file_path)30 text = pytesseract.image_to_string(image)31 32 elif ext == ".mp3":33 # Placeholder for audio transcription logic34 text = "Transcribed text from audio."35 36 elif ext == ".mp4":37 # Placeholder for video transcription logic38 text = "Transcribed text from video."39 40 else:41 return "Unsupported file type."42 43 # Save the extracted text under the specified subject44 subject_folder = os.path.join("subjects", subject)45 os.makedirs(subject_folder, exist_ok=True)46 with open(os.path.join(subject_folder, os.path.basename(file_path) + ".txt"), "w") as f:47 f.write(text)48 49 return text50 51# Function to process YouTube links52def process_youtube_link(link, subject):53 ydl_opts = {54 'format': 'bestaudio/best',55 'outtmpl': 'downloaded_audio.%(ext)s',56 'quiet': True,57 }58 with yt_dlp.YoutubeDL(ydl_opts) as ydl:59 ydl.download([link])60 61 # Placeholder for audio transcription logic62 text = "Transcribed text from YouTube video."63 64 # Save the extracted text under the specified subject65 subject_folder = os.path.join("subjects", subject)66 os.makedirs(subject_folder, exist_ok=True)67 with open(os.path.join(subject_folder, "youtube_video.txt"), "w") as f:68 f.write(text)69 70 return text71 72# Function to generate notes73def generate_notes(text):74 if not text.strip():75 return "No content to generate notes from."76 return f"### Notes\n\n{text}"77 78# Function to generate practice tests79def generate_practice_test(text):80 if not text.strip():81 return "No material to generate test from."82 return (83 "### Practice Test\n\n"84 "1. What is the main idea of the text?\n"85 "2. List and explain key concepts or terms.\n"86 "3. Summarize the most important point.\n"87 "4. Create a diagram or outline to explain the topic.\n"88 )89 90# Function for chatbot Q&A91def chat_with_ai(prompt):92 if not prompt.strip():93 return "Please ask a question."94 result = chatbot(prompt, max_length=100, do_sample=True)95 return result[0]['generated_text']96 97# Gradio Interface98with gr.Blocks() as app:99 gr.Markdown("# ๐ Study AI Assistant")100 101 with gr.Tab("๐ Upload Materials"):102 subject_input = gr.Textbox(label="Subject Name")103 file_input = gr.File(file_types=[".pdf", ".png", ".jpg", ".jpeg", ".mp3", ".mp4"], label="Upload File")104 upload_button = gr.Button("Extract Text")105 extracted_text = gr.Textbox(label="Extracted Text", lines=10)106 upload_button.click(process_file, inputs=[file_input, subject_input], outputs=extracted_text)107 108 with gr.Tab("๐ YouTube Link"):109 subject_input_yt = gr.Textbox(label="Subject Name")110 link_input = gr.Textbox(label="YouTube Link")111 link_button = gr.Button("Process Link")112 link_text = gr.Textbox(label="Extracted Text", lines=10)113 link_button.click(process_youtube_link, inputs=[link_input, subject_input_yt], outputs=link_text)114 115 with gr.Tab("๐ง Notes"):116 generate_notes_button = gr.Button("Generate Notes")117 notes_output = gr.Textbox(label="Notes", lines=10)118 generate_notes_button.click(generate_notes, inputs=extracted_text, outputs=notes_output)119 120 with gr.Tab("๐ Practice Test"):121 generate_test_button = gr.Button("Generate Practice Test")122 test_output = gr.Textbox(label="Practice Test", lines=10)123 generate_test_button.click(generate_practice_test, inputs=extracted_text, outputs=test_output)124 125 with gr.Tab("๐ฌ Chat"):126 chat_input = gr.Textbox(label="Ask a question about your material")127 chat_button = gr.Button("Ask AI")128 chat_output = gr.Textbox(label="AI Answer")129 chat_button.click(chat_with_ai, inputs=chat_input, outputs=chat_output)130 131app.launch()132 