Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,6 @@ from openpyxl import load_workbook
|
|
3 |
from pptx import Presentation
|
4 |
import gradio as gr
|
5 |
import io
|
6 |
-
from huggingface_hub import InferenceClient
|
7 |
import re
|
8 |
import zipfile
|
9 |
import xml.etree.ElementTree as ET
|
@@ -11,10 +10,6 @@ import filetype
|
|
11 |
|
12 |
# Constants
|
13 |
CHUNK_SIZE = 32000
|
14 |
-
MAX_NEW_TOKENS = 4096
|
15 |
-
|
16 |
-
# Initialize the Mistral chat model
|
17 |
-
client = InferenceClient("mistralai/Mistral-Nemo-Instruct-2407")
|
18 |
|
19 |
# --- Utility Functions ---
|
20 |
|
@@ -168,131 +163,23 @@ def read_document(file, clean=True):
|
|
168 |
return f"Error reading file: {e}", 0
|
169 |
|
170 |
|
171 |
-
|
172 |
-
# --- Chat Functions ---
|
173 |
-
|
174 |
-
def generate_mistral_response(message):
|
175 |
-
"""Generates a response from the Mistral API."""
|
176 |
-
stream = client.text_generation(
|
177 |
-
message,
|
178 |
-
max_new_tokens=MAX_NEW_TOKENS,
|
179 |
-
stream=True,
|
180 |
-
details=True,
|
181 |
-
return_full_text=False
|
182 |
-
)
|
183 |
-
output = ""
|
184 |
-
for response in stream:
|
185 |
-
if not response.token.text == "</s>":
|
186 |
-
output += response.token.text
|
187 |
-
yield output
|
188 |
-
|
189 |
-
|
190 |
-
def chat_document(file, question, clean=True):
|
191 |
-
"""Chats with a document using a single Mistral API call."""
|
192 |
-
content, length = read_document(file, clean)
|
193 |
-
if length > CHUNK_SIZE:
|
194 |
-
content = content[:CHUNK_SIZE] # Limit to max chunk size
|
195 |
-
|
196 |
-
system_prompt = """
|
197 |
-
You are a helpful and informative assistant that can answer questions based on the content of documents.
|
198 |
-
You will receive the content of a document and a question about it.
|
199 |
-
Your task is to provide a concise and accurate answer to the question based solely on the provided document content.
|
200 |
-
If the document does not contain enough information to answer the question, simply state that you cannot answer the question based on the provided information.
|
201 |
-
"""
|
202 |
-
|
203 |
-
message = f"""[INST] [SYSTEM] {system_prompt}
|
204 |
-
Document Content: {content}
|
205 |
-
Question: {question}
|
206 |
-
Answer:"""
|
207 |
-
|
208 |
-
yield from generate_mistral_response(message)
|
209 |
-
|
210 |
-
|
211 |
-
def chat_document_v2(file, question, clean=True):
|
212 |
-
"""Chats with a document using chunk-based Mistral API calls and summarizes the answers."""
|
213 |
-
content, length = read_document(file, clean)
|
214 |
-
chunks = split_content(content)
|
215 |
-
|
216 |
-
system_prompt = """
|
217 |
-
You are a helpful and informative assistant that can answer questions based on the content of documents.
|
218 |
-
You will receive the content of a document and a question about it.
|
219 |
-
Your task is to provide a concise and accurate answer to the question based solely on the provided document content.
|
220 |
-
If the document does not contain enough information to answer the question, simply state that you cannot answer the question based on the provided information.
|
221 |
-
"""
|
222 |
-
|
223 |
-
all_answers = []
|
224 |
-
for chunk in chunks:
|
225 |
-
message = f"""[INST] [SYSTEM] {system_prompt}
|
226 |
-
Document Content: {chunk[:CHUNK_SIZE]}
|
227 |
-
Question: {question}
|
228 |
-
Answer:"""
|
229 |
-
|
230 |
-
response = ""
|
231 |
-
for stream_response in generate_mistral_response(message):
|
232 |
-
response = stream_response # Update with latest response
|
233 |
-
all_answers.append(response)
|
234 |
-
|
235 |
-
# Summarize all answers using Mistral
|
236 |
-
summary_prompt = """
|
237 |
-
You are a helpful and informative assistant that can summarize multiple answers related to the same question.
|
238 |
-
You will receive a list of answers to a question, and your task is to generate a concise and comprehensive summary that incorporates the key information from all the answers.
|
239 |
-
Avoid repeating information unnecessarily and focus on providing the most relevant and accurate summary based on the provided answers.
|
240 |
-
|
241 |
-
Answers:
|
242 |
-
"""
|
243 |
-
|
244 |
-
all_answers_str = "\n".join(all_answers)
|
245 |
-
summary_message = f"""[INST] [SYSTEM] {summary_prompt}
|
246 |
-
{all_answers_str[:30000]}
|
247 |
-
Summary:"""
|
248 |
-
|
249 |
-
yield from generate_mistral_response(summary_message)
|
250 |
-
|
251 |
-
|
252 |
# --- Gradio Interface ---
|
253 |
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
iface2 = gr.Interface(
|
273 |
-
fn=chat_document,
|
274 |
-
inputs=[
|
275 |
-
gr.File(label="Upload a Document"),
|
276 |
-
gr.Textbox(label="Question"),
|
277 |
-
gr.Checkbox(label="Clean and Compress Text", value=True),
|
278 |
-
],
|
279 |
-
outputs=gr.Markdown(label="Answer"),
|
280 |
-
title="Document Chat",
|
281 |
-
description="Upload a document and ask questions about its content.",
|
282 |
-
concurrency_limit = None
|
283 |
-
)
|
284 |
-
with gr.TabItem("Document Chat V2"):
|
285 |
-
iface3 = gr.Interface(
|
286 |
-
fn=chat_document_v2,
|
287 |
-
inputs=[
|
288 |
-
gr.File(label="Upload a Document"),
|
289 |
-
gr.Textbox(label="Question"),
|
290 |
-
gr.Checkbox(label="Clean Text", value=True),
|
291 |
-
],
|
292 |
-
outputs=gr.Markdown(label="Answer"),
|
293 |
-
title="Document Chat V2",
|
294 |
-
description="Upload a document and ask questions about its content (using chunk-based approach).",
|
295 |
-
concurrency_limit =None
|
296 |
-
)
|
297 |
-
|
298 |
-
demo.launch()
|
|
|
3 |
from pptx import Presentation
|
4 |
import gradio as gr
|
5 |
import io
|
|
|
6 |
import re
|
7 |
import zipfile
|
8 |
import xml.etree.ElementTree as ET
|
|
|
10 |
|
11 |
# Constants
|
12 |
CHUNK_SIZE = 32000
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# --- Utility Functions ---
|
15 |
|
|
|
163 |
return f"Error reading file: {e}", 0
|
164 |
|
165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
# --- Gradio Interface ---
|
167 |
|
168 |
+
iface = gr.Interface(
|
169 |
+
fn=read_document,
|
170 |
+
inputs=[
|
171 |
+
gr.File(label="Upload a Document"),
|
172 |
+
gr.Checkbox(label="Clean Text", value=True),
|
173 |
+
],
|
174 |
+
outputs=[
|
175 |
+
gr.Textbox(label="Document Content"),
|
176 |
+
gr.Number(label="Document Length (characters)"),
|
177 |
+
],
|
178 |
+
title="Better Document Reader for Hugging Face Chat Tools",
|
179 |
+
description="Upload a document (PDF, XLSX, PPTX, TXT, CSV, DOC, DOCX and Code or text file) to read its content."
|
180 |
+
"This tool is designed for use with Hugging Face Chat Tools: "
|
181 |
+
"[https://hf.co/chat/tools/66ed8236a35891a61e2bfcf2](https://hf.co/chat/tools/66ed8236a35891a61e2bfcf2)",
|
182 |
+
concurrency_limit = None
|
183 |
+
)
|
184 |
+
|
185 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|