Spaces:
Sleeping
Sleeping
Commit
·
e2c15c5
1
Parent(s):
35380ba
Novo método de conversão dos textos para .md
Browse files
app.py
CHANGED
|
@@ -7,7 +7,9 @@ import os
|
|
| 7 |
import uuid
|
| 8 |
import threading
|
| 9 |
import concurrent.futures
|
| 10 |
-
from html import escape
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Importações do LangChain
|
| 13 |
from langchain.prompts import PromptTemplate
|
|
@@ -30,6 +32,16 @@ if not os.path.exists('uploads'):
|
|
| 30 |
|
| 31 |
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
@app.route('/')
|
| 34 |
def index():
|
| 35 |
"""Renderiza a página inicial da aplicação."""
|
|
@@ -58,7 +70,7 @@ def process():
|
|
| 58 |
|
| 59 |
if current_mode == 'test':
|
| 60 |
mock_text = form_data.get('mock_text', 'Este é um texto de simulação.')
|
| 61 |
-
mock_html =
|
| 62 |
yield f"data: {json.dumps({'progress': 100, 'message': 'Simulação concluída!', 'partial_result': {'id': 'grok-output', 'content': mock_html}, 'done': True, 'mode': 'atomic' if processing_mode == 'atomic' else 'hierarchical'})}\n\n"
|
| 63 |
if processing_mode == 'atomic':
|
| 64 |
yield f"data: {json.dumps({'partial_result': {'id': 'sonnet-output', 'content': mock_html}})}\n\n"
|
|
@@ -117,20 +129,26 @@ def process():
|
|
| 117 |
|
| 118 |
yield f"data: {json.dumps({'progress': 80, 'message': 'Todos os modelos responderam. Formatando saídas...'})}\n\n"
|
| 119 |
|
| 120 |
-
# GROK
|
| 121 |
grok_text = results.get('grok', '')
|
| 122 |
print(f"--- Resposta Bruta do GROK (Atômico) ---\n{grok_text}\n--------------------------------------")
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
# SONNET
|
| 126 |
sonnet_text = results.get('sonnet', '')
|
| 127 |
print(f"--- Resposta Bruta do Sonnet (Atômico) ---\n{sonnet_text}\n----------------------------------------")
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
# GEMINI
|
| 131 |
gemini_text = results.get('gemini', '')
|
| 132 |
print(f"--- Resposta Bruta do Gemini (Atômico) ---\n{gemini_text}\n----------------------------------------")
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
yield f"data: {json.dumps({'progress': 100, 'message': 'Processamento Atômico concluído!', 'done': True, 'mode': 'atomic'})}\n\n"
|
| 136 |
|
|
@@ -146,7 +164,10 @@ def process():
|
|
| 146 |
return
|
| 147 |
|
| 148 |
print(f"--- Resposta Bruta do GROK (Hierárquico) ---\n{resposta_grok}\n------------------------------------------")
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
prompt_sonnet = PromptTemplate(template=PROMPT_HIERARQUICO_SONNET, input_variables=["solicitacao_usuario", "texto_para_analise"])
|
| 152 |
claude_with_max_tokens = claude_llm.bind(max_tokens=20000)
|
|
@@ -158,7 +179,10 @@ def process():
|
|
| 158 |
return
|
| 159 |
|
| 160 |
print(f"--- Resposta Bruta do Sonnet (Hierárquico) ---\n{resposta_sonnet}\n--------------------------------------------")
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
prompt_gemini = PromptTemplate(template=PROMPT_HIERARQUICO_GEMINI, input_variables=["solicitacao_usuario", "texto_para_analise"])
|
| 164 |
chain_gemini = LLMChain(llm=gemini_llm, prompt=prompt_gemini)
|
|
@@ -169,7 +193,10 @@ def process():
|
|
| 169 |
return
|
| 170 |
|
| 171 |
print(f"--- Resposta Bruta do Gemini (Hierárquico) ---\n{resposta_gemini}\n--------------------------------------------")
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
except Exception as e:
|
| 175 |
print(f"Ocorreu um erro durante o processamento: {e}")
|
|
@@ -189,7 +216,6 @@ def merge():
|
|
| 189 |
|
| 190 |
prompt_merge = PromptTemplate(template=PROMPT_ATOMICO_MERGE, input_variables=["solicitacao_usuario", "texto_para_analise_grok", "texto_para_analise_sonnet", "texto_para_analise_gemini"])
|
| 191 |
|
| 192 |
-
# Cria uma instância do GROK com limite de tokens aumentado para o merge
|
| 193 |
grok_with_max_tokens = grok_llm.bind(max_tokens=100000)
|
| 194 |
chain_merge = LLMChain(llm=grok_with_max_tokens, prompt=prompt_merge)
|
| 195 |
|
|
@@ -206,8 +232,12 @@ def merge():
|
|
| 206 |
yield f"data: {json.dumps({'error': 'Falha no serviço de Merge (GROK): Sem resposta.'})}\n\n"
|
| 207 |
return
|
| 208 |
|
|
|
|
| 209 |
word_count = len(resposta_merge.split())
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
yield f"data: {json.dumps({'progress': 100, 'message': 'Merge concluído!', 'final_result': {'content': merge_html, 'word_count': word_count}, 'done': True})}\n\n"
|
| 213 |
|
|
|
|
| 7 |
import uuid
|
| 8 |
import threading
|
| 9 |
import concurrent.futures
|
| 10 |
+
from html import escape
|
| 11 |
+
import re
|
| 12 |
+
from markdown_it import MarkdownIt # ✅ NOVA IMPORTAÇÃO
|
| 13 |
|
| 14 |
# Importações do LangChain
|
| 15 |
from langchain.prompts import PromptTemplate
|
|
|
|
| 32 |
|
| 33 |
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024
|
| 34 |
|
| 35 |
+
# ✅ Instancia o novo conversor de Markdown
|
| 36 |
+
md = MarkdownIt()
|
| 37 |
+
|
| 38 |
+
def is_html_empty(html: str) -> bool:
|
| 39 |
+
"""Verifica se uma string HTML não contém texto visível."""
|
| 40 |
+
if not html:
|
| 41 |
+
return True
|
| 42 |
+
text_only = re.sub('<[^<]+?>', '', html)
|
| 43 |
+
return not text_only.strip()
|
| 44 |
+
|
| 45 |
@app.route('/')
|
| 46 |
def index():
|
| 47 |
"""Renderiza a página inicial da aplicação."""
|
|
|
|
| 70 |
|
| 71 |
if current_mode == 'test':
|
| 72 |
mock_text = form_data.get('mock_text', 'Este é um texto de simulação.')
|
| 73 |
+
mock_html = md.render(mock_text)
|
| 74 |
yield f"data: {json.dumps({'progress': 100, 'message': 'Simulação concluída!', 'partial_result': {'id': 'grok-output', 'content': mock_html}, 'done': True, 'mode': 'atomic' if processing_mode == 'atomic' else 'hierarchical'})}\n\n"
|
| 75 |
if processing_mode == 'atomic':
|
| 76 |
yield f"data: {json.dumps({'partial_result': {'id': 'sonnet-output', 'content': mock_html}})}\n\n"
|
|
|
|
| 129 |
|
| 130 |
yield f"data: {json.dumps({'progress': 80, 'message': 'Todos os modelos responderam. Formatando saídas...'})}\n\n"
|
| 131 |
|
|
|
|
| 132 |
grok_text = results.get('grok', '')
|
| 133 |
print(f"--- Resposta Bruta do GROK (Atômico) ---\n{grok_text}\n--------------------------------------")
|
| 134 |
+
grok_html = md.render(grok_text)
|
| 135 |
+
if is_html_empty(grok_html):
|
| 136 |
+
grok_html = f"<pre>{escape(grok_text)}</pre>"
|
| 137 |
+
yield f"data: {json.dumps({'partial_result': {'id': 'grok-output', 'content': grok_html}})}\n\n"
|
| 138 |
|
|
|
|
| 139 |
sonnet_text = results.get('sonnet', '')
|
| 140 |
print(f"--- Resposta Bruta do Sonnet (Atômico) ---\n{sonnet_text}\n----------------------------------------")
|
| 141 |
+
sonnet_html = md.render(sonnet_text)
|
| 142 |
+
if is_html_empty(sonnet_html):
|
| 143 |
+
sonnet_html = f"<pre>{escape(sonnet_text)}</pre>"
|
| 144 |
+
yield f"data: {json.dumps({'partial_result': {'id': 'sonnet-output', 'content': sonnet_html}})}\n\n"
|
| 145 |
|
|
|
|
| 146 |
gemini_text = results.get('gemini', '')
|
| 147 |
print(f"--- Resposta Bruta do Gemini (Atômico) ---\n{gemini_text}\n----------------------------------------")
|
| 148 |
+
gemini_html = md.render(gemini_text)
|
| 149 |
+
if is_html_empty(gemini_html):
|
| 150 |
+
gemini_html = f"<pre>{escape(gemini_text)}</pre>"
|
| 151 |
+
yield f"data: {json.dumps({'partial_result': {'id': 'gemini-output', 'content': gemini_html}})}\n\n"
|
| 152 |
|
| 153 |
yield f"data: {json.dumps({'progress': 100, 'message': 'Processamento Atômico concluído!', 'done': True, 'mode': 'atomic'})}\n\n"
|
| 154 |
|
|
|
|
| 164 |
return
|
| 165 |
|
| 166 |
print(f"--- Resposta Bruta do GROK (Hierárquico) ---\n{resposta_grok}\n------------------------------------------")
|
| 167 |
+
grok_html = md.render(resposta_grok)
|
| 168 |
+
if is_html_empty(grok_html):
|
| 169 |
+
grok_html = f"<pre>{escape(resposta_grok)}</pre>"
|
| 170 |
+
yield f"data: {json.dumps({'progress': 33, 'message': 'Claude Sonnet está processando...', 'partial_result': {'id': 'grok-output', 'content': grok_html}})}\n\n"
|
| 171 |
|
| 172 |
prompt_sonnet = PromptTemplate(template=PROMPT_HIERARQUICO_SONNET, input_variables=["solicitacao_usuario", "texto_para_analise"])
|
| 173 |
claude_with_max_tokens = claude_llm.bind(max_tokens=20000)
|
|
|
|
| 179 |
return
|
| 180 |
|
| 181 |
print(f"--- Resposta Bruta do Sonnet (Hierárquico) ---\n{resposta_sonnet}\n--------------------------------------------")
|
| 182 |
+
sonnet_html = md.render(resposta_sonnet)
|
| 183 |
+
if is_html_empty(sonnet_html):
|
| 184 |
+
sonnet_html = f"<pre>{escape(resposta_sonnet)}</pre>"
|
| 185 |
+
yield f"data: {json.dumps({'progress': 66, 'message': 'Gemini está processando...', 'partial_result': {'id': 'sonnet-output', 'content': sonnet_html}})}\n\n"
|
| 186 |
|
| 187 |
prompt_gemini = PromptTemplate(template=PROMPT_HIERARQUICO_GEMINI, input_variables=["solicitacao_usuario", "texto_para_analise"])
|
| 188 |
chain_gemini = LLMChain(llm=gemini_llm, prompt=prompt_gemini)
|
|
|
|
| 193 |
return
|
| 194 |
|
| 195 |
print(f"--- Resposta Bruta do Gemini (Hierárquico) ---\n{resposta_gemini}\n--------------------------------------------")
|
| 196 |
+
gemini_html = md.render(resposta_gemini)
|
| 197 |
+
if is_html_empty(gemini_html):
|
| 198 |
+
gemini_html = f"<pre>{escape(gemini_html)}</pre>"
|
| 199 |
+
yield f"data: {json.dumps({'progress': 100, 'message': 'Processamento concluído!', 'partial_result': {'id': 'gemini-output', 'content': gemini_html}, 'done': True, 'mode': 'hierarchical'})}\n\n"
|
| 200 |
|
| 201 |
except Exception as e:
|
| 202 |
print(f"Ocorreu um erro durante o processamento: {e}")
|
|
|
|
| 216 |
|
| 217 |
prompt_merge = PromptTemplate(template=PROMPT_ATOMICO_MERGE, input_variables=["solicitacao_usuario", "texto_para_analise_grok", "texto_para_analise_sonnet", "texto_para_analise_gemini"])
|
| 218 |
|
|
|
|
| 219 |
grok_with_max_tokens = grok_llm.bind(max_tokens=100000)
|
| 220 |
chain_merge = LLMChain(llm=grok_with_max_tokens, prompt=prompt_merge)
|
| 221 |
|
|
|
|
| 232 |
yield f"data: {json.dumps({'error': 'Falha no serviço de Merge (GROK): Sem resposta.'})}\n\n"
|
| 233 |
return
|
| 234 |
|
| 235 |
+
print(f"--- Resposta Bruta do Merge (GROK) ---\n{resposta_merge}\n------------------------------------")
|
| 236 |
word_count = len(resposta_merge.split())
|
| 237 |
+
|
| 238 |
+
merge_html = md.render(resposta_merge)
|
| 239 |
+
if is_html_empty(merge_html):
|
| 240 |
+
merge_html = f"<pre>{escape(resposta_merge)}</pre>"
|
| 241 |
|
| 242 |
yield f"data: {json.dumps({'progress': 100, 'message': 'Merge concluído!', 'final_result': {'content': merge_html, 'word_count': word_count}, 'done': True})}\n\n"
|
| 243 |
|