nvymshr commited on
Commit
e88e2be
Β·
verified Β·
1 Parent(s): 2911068

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -112
app.py DELETED
@@ -1,112 +0,0 @@
1
- import gradio as gr
2
- import faiss
3
- import numpy as np
4
- from sentence_transformers import SentenceTransformer
5
- from transformers import pipeline
6
- import PyPDF2
7
- import os
8
-
9
- # Load embedding model
10
- retriever = SentenceTransformer("all-MiniLM-L6-v2")
11
-
12
- # Ensure /mnt/data/ directory exists
13
- pdf_dir = "/mnt/data/"
14
- os.makedirs(pdf_dir, exist_ok=True)
15
-
16
- # Auto-detect the most recent PDF in /mnt/data/
17
- pdf_files = sorted([f for f in os.listdir(pdf_dir) if f.endswith(".pdf")], key=lambda x: os.path.getctime(os.path.join(pdf_dir, x)), reverse=True)
18
-
19
- if pdf_files:
20
- pdf_path = os.path.join(pdf_dir, pdf_files[0]) # Use the first detected PDF
21
- print(f"βœ… Using detected PDF: {pdf_path}")
22
- else:
23
- pdf_path = None
24
- print("🚨 No PDF file found in /mnt/data/")
25
-
26
- def extract_text_from_pdf(pdf_path):
27
- if not pdf_path or not os.path.exists(pdf_path):
28
- print("❌ PDF file not found!")
29
- return "Error: Output cannot be generated at this time. Please try again later."
30
-
31
- text = []
32
- try:
33
- with open(pdf_path, "rb") as file:
34
- reader = PyPDF2.PdfReader(file)
35
- for page in reader.pages:
36
- page_text = page.extract_text()
37
- if page_text:
38
- text.append(page_text.strip())
39
- except Exception as e:
40
- print(f"⚠️ PDF Extraction Failed: {str(e)}")
41
- return "Error: Unable to read the PDF file. Please try again later."
42
-
43
- if text:
44
- print(f"βœ… Successfully extracted {len(text)} paragraphs from PDF.")
45
- else:
46
- print("⚠️ No extractable text found in PDF.")
47
-
48
- return "\n".join(text) if text else "Error: No extractable text found in PDF."
49
-
50
- # Process and chunk the text
51
- text = extract_text_from_pdf(pdf_path) if pdf_path else ""
52
- if "Error" in text or not text:
53
- print("❌ No text extracted from PDF.")
54
- chunks = []
55
- else:
56
- words = text.split()
57
- chunk_size = 500
58
- chunks = [" ".join(words[i:i+chunk_size]) for i in range(0, len(words), chunk_size)]
59
- print(f"βœ… Extracted {len(chunks)} text chunks from PDF.")
60
-
61
- # Embed and store chunks in FAISS if available
62
- if chunks:
63
- try:
64
- chunk_vectors = retriever.encode(chunks)
65
- index = faiss.IndexFlatL2(chunk_vectors.shape[1])
66
- index.add(np.array(chunk_vectors))
67
- print("βœ… FAISS index built successfully.")
68
- except Exception as e:
69
- print(f"⚠️ FAISS indexing failed: {str(e)}")
70
- index = None
71
- else:
72
- index = None
73
- print("❌ FAISS index not created. No valid text chunks.")
74
-
75
- # Load a lightweight Hugging Face model
76
- model_name = "microsoft/phi-2" # Using a smaller, more efficient model
77
- try:
78
- qa_pipeline = pipeline("text-generation", model=model_name, device="cpu") # Explicitly set CPU
79
- print("βœ… AI model loaded successfully.")
80
- except Exception as e:
81
- print(f"⚠️ AI model failed to load: {str(e)}")
82
- qa_pipeline = None
83
-
84
- # Search function
85
- def search_plastics(query):
86
- if not index or not chunks:
87
- print("❌ No valid FAISS index or chunks available.")
88
- return "Error: Output cannot be generated at this time. Please try again later."
89
-
90
- query_vector = retriever.encode([query])
91
- _, indices = index.search(np.array(query_vector), k=1)
92
- retrieved_doc = chunks[indices[0][0]]
93
- print(f"πŸ” Retrieved document chunk: {retrieved_doc[:100]}...")
94
-
95
- if not qa_pipeline:
96
- print("❌ AI model is not available.")
97
- return "Error: AI response could not be generated. Please try again later."
98
-
99
- ai_response = qa_pipeline(retrieved_doc, max_length=50, do_sample=True, temperature=0.7)[0]['generated_text']
100
- return ai_response
101
-
102
- # Gradio Interface
103
- iface = gr.Interface(
104
- fn=search_plastics,
105
- inputs="text",
106
- outputs="text",
107
- title="Plastics Knowledge AI",
108
- description="Ask any question about plastics, recycling, and sustainability!",
109
- )
110
-
111
- # Ensure Public Access
112
- iface.launch(share=True, server_name="0.0.0.0")