Spaces:
Sleeping
Sleeping
update app.py for docx
Browse files
app.py
CHANGED
|
@@ -1,70 +1,58 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
""
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
with gr.Blocks() as demo:
|
| 64 |
-
with gr.Sidebar():
|
| 65 |
-
gr.LoginButton()
|
| 66 |
-
chatbot.render()
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os, glob, re
|
| 2 |
import gradio as gr
|
| 3 |
+
from docx import Document
|
| 4 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 5 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
+
|
| 7 |
+
DOCS_DIR = "."
|
| 8 |
+
CHUNK_SIZE = 900
|
| 9 |
+
OVERLAP = 150
|
| 10 |
+
|
| 11 |
+
def _read_docx(path):
|
| 12 |
+
doc = Document(path)
|
| 13 |
+
full_text = []
|
| 14 |
+
for para in doc.paragraphs:
|
| 15 |
+
if para.text.strip():
|
| 16 |
+
full_text.append(para.text)
|
| 17 |
+
return "\n".join(full_text)
|
| 18 |
+
|
| 19 |
+
def _chunk(text, size=CHUNK_SIZE, overlap=OVERLAP):
|
| 20 |
+
text = re.sub(r"\s+", " ", text).strip()
|
| 21 |
+
chunks, i = [], 0
|
| 22 |
+
while i < len(text):
|
| 23 |
+
chunks.append(text[i:i+size])
|
| 24 |
+
i += (size - overlap)
|
| 25 |
+
return chunks
|
| 26 |
+
|
| 27 |
+
# 1) Cargar documentos DOCX
|
| 28 |
+
docs = []
|
| 29 |
+
for path in glob.glob(os.path.join(DOCS_DIR, "*.docx")):
|
| 30 |
+
docs.append((path, _read_docx(path)))
|
| 31 |
+
|
| 32 |
+
corpus, sources = [], []
|
| 33 |
+
for src, fulltext in docs:
|
| 34 |
+
for ch in _chunk(fulltext):
|
| 35 |
+
corpus.append(ch)
|
| 36 |
+
sources.append(src)
|
| 37 |
+
|
| 38 |
+
if not corpus:
|
| 39 |
+
corpus = ["(Aún no subiste documentos .docx)"]
|
| 40 |
+
sources = [""]
|
| 41 |
+
|
| 42 |
+
# 2) Vectorizar
|
| 43 |
+
vectorizer = TfidfVectorizer(stop_words="spanish")
|
| 44 |
+
X = vectorizer.fit_transform(corpus)
|
| 45 |
+
|
| 46 |
+
# 3) Función de respuesta
|
| 47 |
+
def answer_fn(message, history):
|
| 48 |
+
if "(Aún no" in corpus[0]:
|
| 49 |
+
return "No hay documentos .docx disponibles."
|
| 50 |
+
q = vectorizer.transform([message])
|
| 51 |
+
sims = cosine_similarity(q, X).ravel()
|
| 52 |
+
top_idx = sims.argsort()[::-1][:3]
|
| 53 |
+
bullets = []
|
| 54 |
+
for i in top_idx:
|
| 55 |
+
frag = corpus[i]
|
| 56 |
+
src = os.path.basename(sources[i])
|
| 57 |
+
bullets.append(f"**{src}** · …{frag[:420]}…")
|
| 58 |
+
return "Fragmentos relevantes:\n\n- " + "\n
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|