ankban commited on
Commit
035d1f3
·
verified ·
1 Parent(s): 1cc60d6

Update file_utils.py

Browse files
Files changed (1) hide show
  1. file_utils.py +19 -105
file_utils.py CHANGED
@@ -1,112 +1,26 @@
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
- MAX_TOKENS = 7000 # Safety buffer below GPT-4's 8192 limit
9
 
10
- # === Centralized LLM Caller ===
11
- def call_llm(prompt, system_message="You are a helpful assistant.", temperature=0.7):
12
- # Rough token approximation: 1 token ≈ 4 characters (English average)
13
- approx_tokens = len(prompt) // 4
14
- if approx_tokens > MAX_TOKENS:
15
- prompt = prompt[:MAX_TOKENS * 4] # truncate by character length
16
- prompt += "\n[NOTE: Truncated due to length limit.]"
17
 
18
- response = client.chat.completions.create(
19
- model="gpt-4",
20
- messages=[
21
- {"role": "system", "content": system_message},
22
- {"role": "user", "content": prompt}
23
- ],
24
- temperature=temperature
25
- )
26
- return response.choices[0].message.content
27
 
28
- # === File Parsers ===
29
- def extract_text_from_pdf(pdf_path):
30
- text = ""
31
- with fitz.open(pdf_path) as doc:
32
- for page in doc:
33
- text += page.get_text()
34
- return text
35
 
 
 
36
 
37
- def extract_text_from_txt(file_path):
38
- with open(file_path, 'r', encoding='utf-8') as f:
39
- return f.read()
 
 
 
40
 
41
-
42
- def extract_text_from_md(file_path):
43
- with open(file_path, 'r', encoding='utf-8') as f:
44
- return f.read()
45
-
46
-
47
- def extract_text_from_pptx(file_path):
48
- text = ""
49
- prs = Presentation(file_path)
50
- for slide in prs.slides:
51
- for shape in slide.shapes:
52
- if hasattr(shape, "text"):
53
- text += shape.text + "\n"
54
- return text
55
-
56
-
57
- def extract_text_from_file(file):
58
- if file is None:
59
- return ""
60
-
61
- name = file.name.lower()
62
- if name.endswith(".pdf"):
63
- return extract_text_from_pdf(file.name)
64
- elif name.endswith(".txt"):
65
- return extract_text_from_txt(file.name)
66
- elif name.endswith(".md"):
67
- return extract_text_from_md(file.name)
68
- elif name.endswith(".pptx"):
69
- return extract_text_from_pptx(file.name)
70
- else:
71
- return "Unsupported file type."
72
-
73
-
74
- # === GPT-4 Powered Content Generators ===
75
- def generate_summary(text):
76
- prompt = f"""Please summarize the following study material in a concise and organized manner:
77
-
78
- {text}
79
- """
80
- return call_llm(prompt, system_message="You are a helpful study assistant.")
81
-
82
-
83
- def generate_flashcards(text):
84
- prompt = f"""Based on the content below, create a set of helpful flashcards.
85
- Each flashcard should be formatted as:
86
- Q: Question?
87
- A: Answer.
88
-
89
- {text}
90
- """
91
- return call_llm(prompt, system_message="You are a flashcard generator assistant.")
92
-
93
-
94
- def generate_quiz(text):
95
- prompt = f"""Create a short quiz (3-5 questions) based on the content below.
96
- Include a mix of multiple choice and short answer questions.
97
-
98
- {text}
99
- """
100
- return call_llm(prompt, system_message="You are a quiz generator assistant.")
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
- 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
+ from file_module import file_dashboard as file_viewer_dashboard
3
+ from file_module import file_study_guide_dashboard
 
4
 
 
 
 
5
 
6
+ def file_based_learning_router():
7
+ with gr.Column(scale=4, elem_id="main-column") as router:
8
+ view = gr.State("study") # default view
 
 
 
 
9
 
10
+ file_view_panel = gr.Column(visible=False)
11
+ file_study_panel = gr.Column(visible=True)
 
 
 
 
 
 
 
12
 
13
+ with file_study_panel:
14
+ file_study_guide_dashboard()
 
 
 
 
 
15
 
16
+ with file_view_panel:
17
+ file_viewer_dashboard()
18
 
19
+ def show_view(mode):
20
+ return (
21
+ gr.update(visible=(mode == "study")),
22
+ gr.update(visible=(mode == "file")),
23
+ mode
24
+ )
25
 
26
+ return router, show_view, file_study_panel, file_view_panel