Download file_utils.py from ankban/CommTutor: direct link, hf CLI and curl.
- Browser
- Download file 3.18 kB
-
https://huggingface.co/spaces/ankban/CommTutor/resolve/8a9bac85be2eafdff407dc57b7d0ff8fd9cf19b2/file_utils.py
- Command line
-
hf download hf://spaces/ankban/CommTutor@8a9bac85be2eafdff407dc57b7d0ff8fd9cf19b2/file_utils.py
-
curl -L -o file_utils.py https://huggingface.co/spaces/ankban/CommTutor/resolve/8a9bac85be2eafdff407dc57b7d0ff8fd9cf19b2/file_utils.py
3.18 kB
| import fitz # PyMuPDF | |
| from pptx import Presentation | |
| from openai import OpenAI | |
| import os | |
| # === GPT-4 Client Setup === | |
| client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | |
| MAX_TOKENS = 7000 # Safety buffer below GPT-4's 8192 limit | |
| # === Centralized LLM Caller === | |
| def call_llm(prompt, system_message="You are a helpful assistant.", temperature=0.7): | |
| # Rough token approximation: 1 token ≈ 4 characters (English average) | |
| approx_tokens = len(prompt) // 4 | |
| if approx_tokens > MAX_TOKENS: | |
| prompt = prompt[:MAX_TOKENS * 4] # truncate by character length | |
| prompt += "\n[NOTE: Truncated due to length limit.]" | |
| response = client.chat.completions.create( | |
| model="gpt-4", | |
| messages=[ | |
| {"role": "system", "content": system_message}, | |
| {"role": "user", "content": prompt} | |
| ], | |
| temperature=temperature | |
| ) | |
| return response.choices[0].message.content | |
| # === File Parsers === | |
| def extract_text_from_pdf(pdf_path): | |
| text = "" | |
| with fitz.open(pdf_path) as doc: | |
| for page in doc: | |
| text += page.get_text() | |
| return text | |
| def extract_text_from_txt(file_path): | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| return f.read() | |
| def extract_text_from_md(file_path): | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| return f.read() | |
| def extract_text_from_pptx(file_path): | |
| text = "" | |
| prs = Presentation(file_path) | |
| for slide in prs.slides: | |
| for shape in slide.shapes: | |
| if hasattr(shape, "text"): | |
| text += shape.text + "\n" | |
| return text | |
| def extract_text_from_file(file): | |
| if file is None: | |
| return "" | |
| name = file.name.lower() | |
| if name.endswith(".pdf"): | |
| return extract_text_from_pdf(file.name) | |
| elif name.endswith(".txt"): | |
| return extract_text_from_txt(file.name) | |
| elif name.endswith(".md"): | |
| return extract_text_from_md(file.name) | |
| elif name.endswith(".pptx"): | |
| return extract_text_from_pptx(file.name) | |
| else: | |
| return "Unsupported file type." | |
| # === GPT-4 Powered Content Generators === | |
| def generate_summary(text): | |
| prompt = f"""Please summarize the following study material in a concise and organized manner: | |
| {text} | |
| """ | |
| return call_llm(prompt, system_message="You are a helpful study assistant.") | |
| def generate_flashcards(text): | |
| prompt = f"""Based on the content below, create a set of helpful flashcards. | |
| Each flashcard should be formatted as: | |
| Q: Question? | |
| A: Answer. | |
| {text} | |
| """ | |
| return call_llm(prompt, system_message="You are a flashcard generator assistant.") | |
| def generate_quiz(text): | |
| prompt = f"""Create a short quiz (3-5 questions) based on the content below. | |
| Include a mix of multiple choice and short answer questions. | |
| {text} | |
| """ | |
| return call_llm(prompt, system_message="You are a quiz generator assistant.") | |
| def answer_question(text, question): | |
| prompt = f"""You are an AI tutor. Answer the following question based only on the content below. | |
| Content: | |
| {text} | |
| Question: | |
| {question} | |
| """ | |
| return call_llm(prompt, system_message="You are a helpful and accurate tutor that stays grounded in provided content.") | |