ankban commited on
Commit
05fd677
Β·
verified Β·
1 Parent(s): 213f85f

Update file_utils.py

Browse files
Files changed (1) hide show
  1. file_utils.py +60 -118
file_utils.py CHANGED
@@ -1,119 +1,61 @@
1
- import fitz # PyMuPDF
2
- from pptx import Presentation
3
- from openai import OpenAI
4
  import os
5
-
6
- client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
7
-
8
-
9
- def extract_text_from_pdf(pdf_path):
10
- text = ""
11
- with fitz.open(pdf_path) as doc:
12
- for page in doc:
13
- text += page.get_text()
14
- return text
15
-
16
-
17
- def extract_text_from_txt(file_path):
18
- with open(file_path, 'r', encoding='utf-8') as f:
19
- return f.read()
20
-
21
-
22
- def extract_text_from_md(file_path):
23
- with open(file_path, 'r', encoding='utf-8') as f:
24
- return f.read()
25
-
26
-
27
- def extract_text_from_pptx(file_path):
28
- text = ""
29
- prs = Presentation(file_path)
30
- for slide in prs.slides:
31
- for shape in slide.shapes:
32
- if hasattr(shape, "text"):
33
- text += shape.text + "\n"
34
- return text
35
-
36
-
37
- def extract_text_from_file(file):
38
- if file is None:
39
- return ""
40
-
41
- name = file.name.lower()
42
- if name.endswith(".pdf"):
43
- return extract_text_from_pdf(file.name)
44
- elif name.endswith(".txt"):
45
- return extract_text_from_txt(file.name)
46
- elif name.endswith(".md"):
47
- return extract_text_from_md(file.name)
48
- elif name.endswith(".pptx"):
49
- return extract_text_from_pptx(file.name)
50
- else:
51
- return "Unsupported file type."
52
-
53
-
54
- def generate_summary(text):
55
- prompt = f"""Please summarize the following study material in a concise and organized manner:
56
-
57
- {text}
58
- """
59
- response = client.chat.completions.create(
60
- model="gpt-4",
61
- messages=[
62
- {"role": "system", "content": "You are a helpful study assistant."},
63
- {"role": "user", "content": prompt}
64
- ]
65
- )
66
- return response.choices[0].message.content
67
-
68
-
69
- def generate_flashcards(text):
70
- prompt = f"""Based on the content below, create a set of helpful flashcards.
71
- Each flashcard should be formatted as:
72
- Q: Question?
73
- A: Answer.
74
-
75
- {text}
76
- """
77
- response = client.chat.completions.create(
78
- model="gpt-4",
79
- messages=[
80
- {"role": "system", "content": "You are a flashcard generator assistant."},
81
- {"role": "user", "content": prompt}
82
- ]
83
- )
84
- return response.choices[0].message.content
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
- response = client.chat.completions.create(
94
- model="gpt-4",
95
- messages=[
96
- {"role": "system", "content": "You are a quiz generator assistant."},
97
- {"role": "user", "content": prompt}
98
- ]
99
- )
100
- return response.choices[0].message.content
101
-
102
-
103
- def answer_question(text, question):
104
- prompt = f"""You are an AI tutor. Answer the following question based only on the content below.
105
-
106
- Content:
107
- {text}
108
-
109
- Question:
110
- {question}
111
- """
112
- response = client.chat.completions.create(
113
- model="gpt-4",
114
- messages=[
115
- {"role": "system", "content": "You are a helpful and accurate tutor that stays grounded in provided content."},
116
- {"role": "user", "content": prompt}
117
- ]
118
- )
119
- return response.choices[0].message.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() as file_panel:
14
+ gr.Markdown("""
15
+ <div style='text-align: center;'>
16
+ <img src='file/images/chatter_owl.png' width='120'>
17
+ <h2>πŸ“„ File-Based Learning with <strong>Chatter the Owl</strong></h2>
18
+ <p>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")
24
+
25
+ with gr.Tabs():
26
+ with gr.Tab("πŸ“š Summary"):
27
+ summary_output = gr.Textbox(label="Generated Summary", lines=8, interactive=False)
28
+ with gr.Tab("🧠 Flashcards"):
29
+ flashcard_output = gr.Textbox(label="Generated Flashcards", lines=8, interactive=False)
30
+ with gr.Tab("❓ Quiz"):
31
+ quiz_output = gr.Textbox(label="Generated Quiz Questions", lines=8, interactive=False)
32
+ with gr.Tab("πŸ’¬ Answer to Your Question"):
33
+ answer_output = gr.Textbox(label="Answer", lines=4, interactive=False)
34
+
35
+ text_state = gr.State("")
36
+
37
+ def handle_file_upload(file):
38
+ text = extract_text_from_file(file)
39
+ summary = generate_summary(text)
40
+ flashcards = generate_flashcards(text)
41
+ quiz = generate_quiz(text)
42
+ return text, summary, flashcards, quiz
43
+
44
+ def handle_question(text, question):
45
+ if not text.strip() or not question.strip():
46
+ return "Please upload a file and enter a question."
47
+ return answer_question(text, question)
48
+
49
+ file_upload.change(
50
+ fn=handle_file_upload,
51
+ inputs=[file_upload],
52
+ outputs=[text_state, summary_output, flashcard_output, quiz_output]
53
+ )
54
+
55
+ question_box.change(
56
+ fn=handle_question,
57
+ inputs=[text_state, question_box],
58
+ outputs=[answer_output]
59
+ )
60
+
61
+ return file_panel