Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -2,30 +2,45 @@ import gradio as gr
|
|
| 2 |
import json
|
| 3 |
|
| 4 |
# Placeholder para dados de busca
|
| 5 |
-
# Futuramente será integrado com embeddings e vector DB
|
| 6 |
-
|
| 7 |
SAMPLE_DOCS = [
|
| 8 |
-
{"title": "SEO Multilíngue", "content": "Estratégias de SEO para múltiplos idiomas.
|
| 9 |
-
{"title": "Marketing Digital", "content": "Serviços de marketing digital da MSC.
|
| 10 |
-
{"title": "Agentes de IA", "content": "Criação de agentes inteligentes para automação.
|
|
|
|
|
|
|
| 11 |
]
|
| 12 |
|
| 13 |
def search(query: str, top_k: int = 5):
|
| 14 |
-
"""Realiza busca
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
results = []
|
| 19 |
query_lower = query.lower()
|
| 20 |
|
| 21 |
for doc in SAMPLE_DOCS:
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
if not results:
|
| 26 |
results = SAMPLE_DOCS[:top_k]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Interface Gradio
|
| 31 |
with gr.Blocks(title="MSC Search") as demo:
|
|
@@ -33,18 +48,28 @@ with gr.Blocks(title="MSC Search") as demo:
|
|
| 33 |
gr.Markdown("Busque na documentação da MSC Marketing")
|
| 34 |
|
| 35 |
with gr.Row():
|
| 36 |
-
query_input = gr.Textbox(
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
search_btn = gr.Button("Buscar", variant="primary")
|
| 40 |
-
results_output = gr.
|
| 41 |
|
| 42 |
search_btn.click(search, inputs=[query_input, top_k_input], outputs=results_output)
|
|
|
|
| 43 |
|
| 44 |
gr.Markdown("""
|
| 45 |
---
|
| 46 |
-
**
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
""")
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import json
|
| 3 |
|
| 4 |
# Placeholder para dados de busca
|
|
|
|
|
|
|
| 5 |
SAMPLE_DOCS = [
|
| 6 |
+
{"title": "SEO Multilíngue", "content": "Estratégias de SEO para múltiplos idiomas incluindo português, inglês, espanhol e alemão.", "relevance": 0.95},
|
| 7 |
+
{"title": "Marketing Digital", "content": "Serviços completos de marketing digital da MSC Marketing.", "relevance": 0.90},
|
| 8 |
+
{"title": "Agentes de IA", "content": "Criação de agentes inteligentes para automação de processos de marketing.", "relevance": 0.85},
|
| 9 |
+
{"title": "Geração de Conteúdo", "content": "Ferramentas de IA para geração de conteúdo otimizado para SEO.", "relevance": 0.80},
|
| 10 |
+
{"title": "Chatbots", "content": "Desenvolvimento de chatbots inteligentes para atendimento ao cliente.", "relevance": 0.75},
|
| 11 |
]
|
| 12 |
|
| 13 |
def search(query: str, top_k: int = 5):
|
| 14 |
+
"""Realiza busca nos documentos."""
|
| 15 |
+
if not query.strip():
|
| 16 |
+
return "Por favor, digite uma busca."
|
| 17 |
|
| 18 |
results = []
|
| 19 |
query_lower = query.lower()
|
| 20 |
|
| 21 |
for doc in SAMPLE_DOCS:
|
| 22 |
+
score = 0
|
| 23 |
+
if query_lower in doc["title"].lower():
|
| 24 |
+
score += 0.5
|
| 25 |
+
if query_lower in doc["content"].lower():
|
| 26 |
+
score += 0.3
|
| 27 |
+
if score > 0:
|
| 28 |
+
results.append({**doc, "score": score + doc["relevance"]})
|
| 29 |
|
| 30 |
if not results:
|
| 31 |
results = SAMPLE_DOCS[:top_k]
|
| 32 |
+
for r in results:
|
| 33 |
+
r["score"] = r["relevance"] * 0.5
|
| 34 |
+
|
| 35 |
+
results = sorted(results, key=lambda x: x.get("score", 0), reverse=True)[:top_k]
|
| 36 |
|
| 37 |
+
output = f"## Resultados para: \"{query}\"\n\n"
|
| 38 |
+
for i, doc in enumerate(results, 1):
|
| 39 |
+
output += f"### {i}. {doc['title']}\n"
|
| 40 |
+
output += f"{doc['content']}\n"
|
| 41 |
+
output += f"*Relevância: {doc.get('score', doc['relevance']):.2f}*\n\n"
|
| 42 |
+
|
| 43 |
+
return output
|
| 44 |
|
| 45 |
# Interface Gradio
|
| 46 |
with gr.Blocks(title="MSC Search") as demo:
|
|
|
|
| 48 |
gr.Markdown("Busque na documentação da MSC Marketing")
|
| 49 |
|
| 50 |
with gr.Row():
|
| 51 |
+
query_input = gr.Textbox(
|
| 52 |
+
label="Sua busca",
|
| 53 |
+
placeholder="Digite sua busca...",
|
| 54 |
+
scale=4
|
| 55 |
+
)
|
| 56 |
+
top_k_input = gr.Slider(1, 10, value=5, step=1, label="Resultados", scale=1)
|
| 57 |
|
| 58 |
+
search_btn = gr.Button("🔍 Buscar", variant="primary")
|
| 59 |
+
results_output = gr.Markdown(label="Resultados")
|
| 60 |
|
| 61 |
search_btn.click(search, inputs=[query_input, top_k_input], outputs=results_output)
|
| 62 |
+
query_input.submit(search, inputs=[query_input, top_k_input], outputs=results_output)
|
| 63 |
|
| 64 |
gr.Markdown("""
|
| 65 |
---
|
| 66 |
+
**Datasets MSC Marketing:**
|
| 67 |
+
- [msc-knowledge-base](https://huggingface.co/datasets/Finish-him/msc-knowledge-base)
|
| 68 |
+
- [msc-qa-pairs](https://huggingface.co/datasets/Finish-him/msc-qa-pairs)
|
| 69 |
+
- [msc-embeddings](https://huggingface.co/datasets/Finish-him/msc-embeddings)
|
| 70 |
+
- [msc-instructions](https://huggingface.co/datasets/Finish-him/msc-instructions)
|
| 71 |
+
|
| 72 |
+
*Este Space está em fase de configuração. Em breve teremos busca semântica completa com embeddings.*
|
| 73 |
""")
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|