Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,15 +12,15 @@ from huggingface_hub import InferenceClient
|
|
| 12 |
|
| 13 |
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
| 14 |
|
| 15 |
-
# Free
|
| 16 |
-
|
| 17 |
|
| 18 |
-
# Local embedding model
|
| 19 |
EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
| 20 |
|
| 21 |
-
# Load models
|
| 22 |
embedding_model = SentenceTransformer(EMBEDDING_MODEL)
|
| 23 |
-
llm = InferenceClient(model=
|
| 24 |
|
| 25 |
|
| 26 |
# ============================================================
|
|
@@ -28,7 +28,6 @@ llm = InferenceClient(model=GRANITE_MODEL, token=HF_TOKEN)
|
|
| 28 |
# ============================================================
|
| 29 |
|
| 30 |
def read_pdf(file_obj):
|
| 31 |
-
"""Extract raw text from a PDF file."""
|
| 32 |
reader = PdfReader(file_obj)
|
| 33 |
pages = []
|
| 34 |
for page in reader.pages:
|
|
@@ -39,7 +38,6 @@ def read_pdf(file_obj):
|
|
| 39 |
|
| 40 |
|
| 41 |
def chunk_text(text, chunk_size=800, overlap=200):
|
| 42 |
-
"""Split text into overlapping chunks."""
|
| 43 |
words = text.split()
|
| 44 |
chunks = []
|
| 45 |
start = 0
|
|
@@ -54,7 +52,6 @@ def chunk_text(text, chunk_size=800, overlap=200):
|
|
| 54 |
|
| 55 |
|
| 56 |
def build_vectorstore(chunks):
|
| 57 |
-
"""Embed chunks and build FAISS index."""
|
| 58 |
if not chunks:
|
| 59 |
return None, None
|
| 60 |
|
|
@@ -68,7 +65,6 @@ def build_vectorstore(chunks):
|
|
| 68 |
|
| 69 |
|
| 70 |
def retrieve_context(question, state, k=4):
|
| 71 |
-
"""Retrieve top-k relevant chunks."""
|
| 72 |
index = state.get("index")
|
| 73 |
chunks = state.get("chunks")
|
| 74 |
|
|
@@ -83,7 +79,7 @@ def retrieve_context(question, state, k=4):
|
|
| 83 |
|
| 84 |
|
| 85 |
# ============================================================
|
| 86 |
-
# LLM ANSWERING
|
| 87 |
# ============================================================
|
| 88 |
|
| 89 |
def generate_answer(question, context):
|
|
@@ -100,15 +96,14 @@ def generate_answer(question, context):
|
|
| 100 |
f"Question: {question}\nAnswer:"
|
| 101 |
)
|
| 102 |
|
| 103 |
-
response = llm.
|
| 104 |
prompt,
|
| 105 |
-
max_new_tokens=
|
| 106 |
temperature=0.2,
|
| 107 |
top_p=0.9,
|
| 108 |
-
repetition_penalty=1.05,
|
| 109 |
)
|
| 110 |
|
| 111 |
-
return response.
|
| 112 |
|
| 113 |
|
| 114 |
# ============================================================
|
|
@@ -116,8 +111,6 @@ def generate_answer(question, context):
|
|
| 116 |
# ============================================================
|
| 117 |
|
| 118 |
def load_pdf(file, state):
|
| 119 |
-
"""Reset state and load a new PDF."""
|
| 120 |
-
# Reset everything to avoid memory persistence
|
| 121 |
state["chunks"] = None
|
| 122 |
state["index"] = None
|
| 123 |
|
|
@@ -143,7 +136,6 @@ def load_pdf(file, state):
|
|
| 143 |
|
| 144 |
|
| 145 |
def answer_question(question, state):
|
| 146 |
-
"""Answer a question using the current PDF context."""
|
| 147 |
if not question.strip():
|
| 148 |
return "Please enter a question."
|
| 149 |
|
|
@@ -159,8 +151,8 @@ def answer_question(question, state):
|
|
| 159 |
# GRADIO UI
|
| 160 |
# ============================================================
|
| 161 |
|
| 162 |
-
with gr.Blocks(title="
|
| 163 |
-
gr.Markdown("## PDF Q&A
|
| 164 |
|
| 165 |
state = gr.State({"chunks": None, "index": None})
|
| 166 |
|
|
@@ -168,12 +160,12 @@ with gr.Blocks(title="Granite PDF QA") as demo:
|
|
| 168 |
with gr.Column(scale=1):
|
| 169 |
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 170 |
pdf_status = gr.Textbox(label="Status", interactive=False)
|
| 171 |
-
|
| 172 |
with gr.Column(scale=2):
|
| 173 |
question = gr.Textbox(label="Your question", lines=2)
|
| 174 |
answer = gr.Textbox(label="Answer", lines=8)
|
| 175 |
ask_button = gr.Button("Ask")
|
| 176 |
-
|
| 177 |
pdf_input.upload(load_pdf, inputs=[pdf_input, state], outputs=[pdf_status, state])
|
| 178 |
ask_button.click(answer_question, inputs=[question, state], outputs=answer)
|
| 179 |
|
|
|
|
| 12 |
|
| 13 |
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
| 14 |
|
| 15 |
+
# Free, stable, HF-compatible model
|
| 16 |
+
LLM_MODEL = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 17 |
|
| 18 |
+
# Local embedding model
|
| 19 |
EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
| 20 |
|
| 21 |
+
# Load models
|
| 22 |
embedding_model = SentenceTransformer(EMBEDDING_MODEL)
|
| 23 |
+
llm = InferenceClient(model=LLM_MODEL, token=HF_TOKEN)
|
| 24 |
|
| 25 |
|
| 26 |
# ============================================================
|
|
|
|
| 28 |
# ============================================================
|
| 29 |
|
| 30 |
def read_pdf(file_obj):
|
|
|
|
| 31 |
reader = PdfReader(file_obj)
|
| 32 |
pages = []
|
| 33 |
for page in reader.pages:
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
def chunk_text(text, chunk_size=800, overlap=200):
|
|
|
|
| 41 |
words = text.split()
|
| 42 |
chunks = []
|
| 43 |
start = 0
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
def build_vectorstore(chunks):
|
|
|
|
| 55 |
if not chunks:
|
| 56 |
return None, None
|
| 57 |
|
|
|
|
| 65 |
|
| 66 |
|
| 67 |
def retrieve_context(question, state, k=4):
|
|
|
|
| 68 |
index = state.get("index")
|
| 69 |
chunks = state.get("chunks")
|
| 70 |
|
|
|
|
| 79 |
|
| 80 |
|
| 81 |
# ============================================================
|
| 82 |
+
# LLM ANSWERING
|
| 83 |
# ============================================================
|
| 84 |
|
| 85 |
def generate_answer(question, context):
|
|
|
|
| 96 |
f"Question: {question}\nAnswer:"
|
| 97 |
)
|
| 98 |
|
| 99 |
+
response = llm.text_generation(
|
| 100 |
prompt,
|
| 101 |
+
max_new_tokens=300,
|
| 102 |
temperature=0.2,
|
| 103 |
top_p=0.9,
|
|
|
|
| 104 |
)
|
| 105 |
|
| 106 |
+
return response.strip()
|
| 107 |
|
| 108 |
|
| 109 |
# ============================================================
|
|
|
|
| 111 |
# ============================================================
|
| 112 |
|
| 113 |
def load_pdf(file, state):
|
|
|
|
|
|
|
| 114 |
state["chunks"] = None
|
| 115 |
state["index"] = None
|
| 116 |
|
|
|
|
| 136 |
|
| 137 |
|
| 138 |
def answer_question(question, state):
|
|
|
|
| 139 |
if not question.strip():
|
| 140 |
return "Please enter a question."
|
| 141 |
|
|
|
|
| 151 |
# GRADIO UI
|
| 152 |
# ============================================================
|
| 153 |
|
| 154 |
+
with gr.Blocks(title="PDF Q&A Chatbot") as demo:
|
| 155 |
+
gr.Markdown("## 📄 PDF Q&A Chatbot\nUpload a PDF and ask questions about its content.")
|
| 156 |
|
| 157 |
state = gr.State({"chunks": None, "index": None})
|
| 158 |
|
|
|
|
| 160 |
with gr.Column(scale=1):
|
| 161 |
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 162 |
pdf_status = gr.Textbox(label="Status", interactive=False)
|
| 163 |
+
|
| 164 |
with gr.Column(scale=2):
|
| 165 |
question = gr.Textbox(label="Your question", lines=2)
|
| 166 |
answer = gr.Textbox(label="Answer", lines=8)
|
| 167 |
ask_button = gr.Button("Ask")
|
| 168 |
+
|
| 169 |
pdf_input.upload(load_pdf, inputs=[pdf_input, state], outputs=[pdf_status, state])
|
| 170 |
ask_button.click(answer_question, inputs=[question, state], outputs=answer)
|
| 171 |
|