asaeed23 commited on
Commit
e812a8d
Β·
verified Β·
1 Parent(s): 1222b5e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tempfile
3
+ from utils import extract_pdf_by_page, split_by_chapter, build_faiss_index, retrieve_text, generate_notes_questions
4
+
5
+ chunks = []
6
+ faiss_index = None
7
+ chunk_texts = []
8
+ groq_api_key = ""
9
+
10
+ def upload_pdf(file, use_chapter_split):
11
+ global chunks, faiss_index, chunk_texts
12
+ with tempfile.NamedTemporaryFile(delete=False) as tmp:
13
+ tmp.write(file.read())
14
+ tmp_path = tmp.name
15
+
16
+ pages = extract_pdf_by_page(tmp_path)
17
+ chunks = split_by_chapter(pages) if use_chapter_split else pages
18
+ faiss_index, chunk_texts, chunks = build_faiss_index(chunks)
19
+ return f"βœ… Uploaded and indexed {len(chunks)} chunks."
20
+
21
+ def set_api_key(api_key):
22
+ global groq_api_key
23
+ groq_api_key = api_key
24
+ return "βœ… API key set!"
25
+
26
+ def process_query(query):
27
+ if not groq_api_key:
28
+ return "❌ Please provide a valid Groq API key."
29
+ results = retrieve_text(query, faiss_index, chunk_texts, chunks)
30
+ output = generate_notes_questions(results[0]['text'], groq_api_key)
31
+ return f"πŸ“˜ **{results[0].get('title', 'Page')}** (pages: {results[0].get('pages')})\n\n{output}"
32
+
33
+ with gr.Blocks(title="RAG PDF Notes & Questions Generator") as demo:
34
+ gr.Markdown("# πŸ“š PDF Chapter Notes & Question Generator using Groq LLM")
35
+
36
+ with gr.Row():
37
+ pdf = gr.File(label="Upload PDF", file_types=[".pdf"])
38
+ use_chapter = gr.Checkbox(label="Split by Chapter", value=True)
39
+ upload_btn = gr.Button("Process PDF")
40
+
41
+ output_text = gr.Textbox(label="Upload Status")
42
+ upload_btn.click(fn=upload_pdf, inputs=[pdf, use_chapter], outputs=output_text)
43
+
44
+ api_key_input = gr.Textbox(label="πŸ” Groq API Key", type="password")
45
+ api_btn = gr.Button("Set API Key")
46
+ api_output = gr.Textbox(label="API Key Status")
47
+ api_btn.click(fn=set_api_key, inputs=[api_key_input], outputs=api_output)
48
+
49
+ query_input = gr.Textbox(label="Enter chapter title or page keyword")
50
+ query_btn = gr.Button("Generate Notes & Questions")
51
+ result_output = gr.Markdown()
52
+ query_btn.click(fn=process_query, inputs=[query_input], outputs=[result_output])
53
+
54
+ demo.launch()