ankban commited on
Commit
36fe359
Β·
verified Β·
1 Parent(s): e79e5f0

Update file_utils.py

Browse files
Files changed (1) hide show
  1. file_utils.py +75 -104
file_utils.py CHANGED
@@ -1,105 +1,76 @@
1
- import fitz # PyMuPDF
2
- from pptx import Presentation
3
- from openai import OpenAI
4
  import os
5
-
6
- # === GPT-4 Client Setup ===
7
- client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
8
-
9
- # === Centralized LLM Caller ===
10
- def call_llm(prompt, system_message="You are a helpful assistant.", temperature=0.7):
11
- response = client.chat.completions.create(
12
- model="gpt-4",
13
- messages=[
14
- {"role": "system", "content": system_message},
15
- {"role": "user", "content": prompt}
16
- ],
17
- temperature=temperature
18
- )
19
- return response.choices[0].message.content
20
-
21
- # === File Parsers ===
22
- def extract_text_from_pdf(pdf_path):
23
- text = ""
24
- with fitz.open(pdf_path) as doc:
25
- for page in doc:
26
- text += page.get_text()
27
- return text
28
-
29
-
30
- def extract_text_from_txt(file_path):
31
- with open(file_path, 'r', encoding='utf-8') as f:
32
- return f.read()
33
-
34
-
35
- def extract_text_from_md(file_path):
36
- with open(file_path, 'r', encoding='utf-8') as f:
37
- return f.read()
38
-
39
-
40
- def extract_text_from_pptx(file_path):
41
- text = ""
42
- prs = Presentation(file_path)
43
- for slide in prs.slides:
44
- for shape in slide.shapes:
45
- if hasattr(shape, "text"):
46
- text += shape.text + "\n"
47
- return text
48
-
49
-
50
- def extract_text_from_file(file):
51
- if file is None:
52
- return ""
53
-
54
- name = file.name.lower()
55
- if name.endswith(".pdf"):
56
- return extract_text_from_pdf(file.name)
57
- elif name.endswith(".txt"):
58
- return extract_text_from_txt(file.name)
59
- elif name.endswith(".md"):
60
- return extract_text_from_md(file.name)
61
- elif name.endswith(".pptx"):
62
- return extract_text_from_pptx(file.name)
63
- else:
64
- return "Unsupported file type."
65
-
66
-
67
- # === GPT-4 Powered Content Generators ===
68
- def generate_summary(text):
69
- prompt = f"""Please summarize the following study material in a concise and organized manner:
70
-
71
- {text}
72
- """
73
- return call_llm(prompt, system_message="You are a helpful study assistant.")
74
-
75
-
76
- def generate_flashcards(text):
77
- prompt = f"""Based on the content below, create a set of helpful flashcards.
78
- Each flashcard should be formatted as:
79
- Q: Question?
80
- A: Answer.
81
-
82
- {text}
83
- """
84
- return call_llm(prompt, system_message="You are a flashcard generator assistant.")
85
-
86
-
87
- def generate_quiz(text):
88
- prompt = f"""Create a short quiz (3-5 questions) based on the content below.
89
- Include a mix of multiple choice and short answer questions.
90
-
91
- {text}
92
- """
93
- return call_llm(prompt, system_message="You are a quiz generator assistant.")
94
-
95
-
96
- def answer_question(text, question):
97
- prompt = f"""You are an AI tutor. Answer the following question based only on the content below.
98
-
99
- Content:
100
- {text}
101
-
102
- Question:
103
- {question}
104
- """
105
- return call_llm(prompt, system_message="You are a helpful and accurate tutor that stays grounded in provided content.")
 
1
+ import gradio as gr
 
 
2
  import os
3
+ from file_utils import (
4
+ extract_text_from_file,
5
+ generate_summary,
6
+ generate_flashcards,
7
+ generate_quiz,
8
+ answer_question
9
+ )
10
+
11
+
12
+ def file_dashboard():
13
+ with gr.Column(elem_classes="file-panel") as file_panel:
14
+ gr.Markdown("""
15
+ <div style='text-align: center; margin-bottom: 1rem;'>
16
+ <img src='file/images/chatter_owl.png' width='100'>
17
+ <h2 style='margin-top: 0.5rem;'>πŸ“„ File-Based Learning with <strong>Chatter the Owl</strong></h2>
18
+ <p style='margin-bottom: 1rem;'>Upload your notes, textbooks, or slides, and let me turn them into summaries, quizzes, and flashcards!</p>
19
+ </div>
20
+ """)
21
+
22
+ file_upload = gr.File(label="πŸ“ Upload Your Study Material", file_types=[".pdf", ".txt", ".md", ".pptx"])
23
+ question_box = gr.Textbox(label="πŸ’¬ Ask a question about the uploaded file", lines=2)
24
+
25
+ with gr.Tabs():
26
+ with gr.Tab("πŸ“š Summary"):
27
+ summary_output = gr.Textbox(label="Generated Summary", lines=6, interactive=False)
28
+
29
+ with gr.Tab("🧠 Flashcards"):
30
+ with gr.Accordion("Click to view generated flashcards", open=False):
31
+ flashcard_output = gr.Textbox(label=None, lines=6, interactive=False, show_label=False)
32
+
33
+ with gr.Tab("❓ Quiz"):
34
+ with gr.Accordion("Click to view generated quiz questions", open=False):
35
+ quiz_output = gr.Textbox(label=None, lines=6, interactive=False, show_label=False)
36
+
37
+ with gr.Tab("πŸ’¬ Answer to Your Question"):
38
+ answer_output = gr.Textbox(label="Answer", lines=4, interactive=False)
39
+
40
+ text_state = gr.State("")
41
+
42
+ def handle_file_upload(file):
43
+ if file is None:
44
+ return "", "No file uploaded.", "", "", ""
45
+
46
+ text = extract_text_from_file(file)
47
+ if not text or "Unsupported file type" in text:
48
+ return "", text, "", "", ""
49
+
50
+ summary = generate_summary(text)
51
+ flashcards = generate_flashcards(text)
52
+ quiz = generate_quiz(text)
53
+ return text, summary, flashcards, quiz, ""
54
+
55
+ def handle_question(text, question):
56
+ if not text.strip():
57
+ return "Please upload a file first."
58
+ if not question.strip():
59
+ return "Please enter a question."
60
+ return answer_question(text, question)
61
+
62
+ file_upload.change(
63
+ fn=handle_file_upload,
64
+ inputs=[file_upload],
65
+ outputs=[text_state, summary_output, flashcard_output, quiz_output, answer_output],
66
+ show_progress="minimal"
67
+ )
68
+
69
+ question_box.change(
70
+ fn=handle_question,
71
+ inputs=[text_state, question_box],
72
+ outputs=[answer_output],
73
+ show_progress="minimal"
74
+ )
75
+
76
+ return file_panel