Update app.py
Browse files
app.py
CHANGED
@@ -20,40 +20,60 @@ logging.basicConfig(
|
|
20 |
)
|
21 |
logger = logging.getLogger(__name__)
|
22 |
|
|
|
23 |
class LatexFormatter:
|
24 |
-
"""Classe pour
|
25 |
|
26 |
@staticmethod
|
27 |
-
def
|
28 |
-
"""
|
29 |
-
# Remplace
|
30 |
-
text = re.sub(r'(
|
|
|
|
|
|
|
31 |
return text
|
32 |
-
|
33 |
@staticmethod
|
34 |
-
def
|
35 |
-
"""Formate les
|
36 |
-
# Ajoute des
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
text = re.sub(r'\$\$(.*?)\$\$', r'\n\n$$\1$$\n\n', text, flags=re.DOTALL)
|
38 |
return text
|
39 |
-
|
40 |
@staticmethod
|
41 |
def enhance_latex_display(text: str) -> str:
|
42 |
-
"""Améliore l'affichage LaTeX
|
43 |
-
#
|
44 |
-
text = text.replace('\\[', '$$').replace('\\]', '$$')
|
45 |
-
text = text.replace('\\(', '$').replace('\\)', '$')
|
|
|
|
|
|
|
46 |
|
47 |
-
#
|
48 |
-
text =
|
49 |
|
50 |
-
#
|
51 |
-
text =
|
52 |
-
|
53 |
-
|
|
|
54 |
|
55 |
return text
|
56 |
|
|
|
|
|
|
|
|
|
|
|
57 |
class GeminiClient:
|
58 |
"""Classe pour gérer les interactions avec l'API Gemini"""
|
59 |
def __init__(self, api_key: str):
|
@@ -99,22 +119,41 @@ def format_text_with_latex(text: str) -> str:
|
|
99 |
return text
|
100 |
|
101 |
def stream_response(container, response: Generator) -> None:
|
102 |
-
"""
|
103 |
-
mode = 'starting'
|
104 |
-
thinking_placeholder = None
|
105 |
-
answer_placeholder = None
|
106 |
-
thinking_text = ""
|
107 |
-
answer_text = ""
|
108 |
|
109 |
-
# Configuration CSS pour
|
110 |
st.markdown("""
|
111 |
<style>
|
112 |
-
|
113 |
-
.katex
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
</style>
|
116 |
-
|
117 |
-
|
118 |
try:
|
119 |
for chunk in response:
|
120 |
logger.debug(f"Chunk reçu: {chunk}")
|
|
|
20 |
)
|
21 |
logger = logging.getLogger(__name__)
|
22 |
|
23 |
+
|
24 |
class LatexFormatter:
|
25 |
+
"""Classe améliorée pour le formatage LaTeX"""
|
26 |
|
27 |
@staticmethod
|
28 |
+
def cleanup_latex_fractions(text: str) -> str:
|
29 |
+
"""Nettoie et formate correctement les fractions LaTeX"""
|
30 |
+
# Remplace \frac{x}{y} par \frac{x}{y}
|
31 |
+
text = re.sub(r'\\frac\{([^}]*)\}\{([^}]*)\}', r'$$\\frac{\1}{\2}$$', text)
|
32 |
+
# Gère les cas spéciaux comme \left et \right
|
33 |
+
text = re.sub(r'\\left\(', r'\\left(', text)
|
34 |
+
text = re.sub(r'\\right\)', r'\\right)', text)
|
35 |
return text
|
36 |
+
|
37 |
@staticmethod
|
38 |
+
def format_inline_math(text: str) -> str:
|
39 |
+
"""Formate les expressions mathématiques en ligne"""
|
40 |
+
# Ajoute des espaces autour des expressions inline
|
41 |
+
text = re.sub(r'(?<!\\)\$([^$]+)(?<!\\)\$', r' $\1$ ', text)
|
42 |
+
return text
|
43 |
+
|
44 |
+
@staticmethod
|
45 |
+
def format_display_math(text: str) -> str:
|
46 |
+
"""Formate les expressions mathématiques en mode display"""
|
47 |
+
# Ajoute des sauts de ligne autour des expressions display
|
48 |
text = re.sub(r'\$\$(.*?)\$\$', r'\n\n$$\1$$\n\n', text, flags=re.DOTALL)
|
49 |
return text
|
50 |
+
|
51 |
@staticmethod
|
52 |
def enhance_latex_display(text: str) -> str:
|
53 |
+
"""Améliore globalement l'affichage LaTeX"""
|
54 |
+
# Prétraitement
|
55 |
+
text = text.replace('\\[', '$$').replace('\\]', '$$')
|
56 |
+
text = text.replace('\\(', '$').replace('\\)', '$')
|
57 |
+
|
58 |
+
# Nettoyage des fractions
|
59 |
+
text = LatexFormatter.cleanup_latex_fractions(text)
|
60 |
|
61 |
+
# Formatage du mode inline
|
62 |
+
text = LatexFormatter.format_inline_math(text)
|
63 |
|
64 |
+
# Formatage du mode display
|
65 |
+
text = LatexFormatter.format_display_math(text)
|
66 |
+
|
67 |
+
# Assure que chaque ligne d'équation est bien espacée
|
68 |
+
text = re.sub(r'(\n\s*\$\$[^$]+\$\$)', r'\n\n\1\n\n', text)
|
69 |
|
70 |
return text
|
71 |
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
class GeminiClient:
|
78 |
"""Classe pour gérer les interactions avec l'API Gemini"""
|
79 |
def __init__(self, api_key: str):
|
|
|
119 |
return text
|
120 |
|
121 |
def stream_response(container, response: Generator) -> None:
|
122 |
+
"""Fonction de streaming améliorée avec meilleur support LaTeX"""
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
+
# Configuration CSS améliorée pour le rendu LaTeX
|
125 |
st.markdown("""
|
126 |
<style>
|
127 |
+
/* Amélioration du rendu LaTeX */
|
128 |
+
.katex {
|
129 |
+
font-size: 1.2em !important;
|
130 |
+
padding: 0.2em 0;
|
131 |
+
}
|
132 |
+
.katex-display {
|
133 |
+
margin: 1.5em 0 !important;
|
134 |
+
overflow: auto hidden;
|
135 |
+
background: rgba(248, 249, 250, 0.05);
|
136 |
+
padding: 0.5em;
|
137 |
+
border-radius: 4px;
|
138 |
+
}
|
139 |
+
/* Amélioration de l'espacement des fractions */
|
140 |
+
.katex .frac-line {
|
141 |
+
border-bottom-width: 0.08em;
|
142 |
+
}
|
143 |
+
.katex .mfrac .frac-line {
|
144 |
+
margin: 0.1em 0;
|
145 |
+
}
|
146 |
+
/* Meilleur rendu sur mobile */
|
147 |
+
@media (max-width: 768px) {
|
148 |
+
.katex { font-size: 1.1em !important; }
|
149 |
+
.katex-display {
|
150 |
+
padding: 0.3em;
|
151 |
+
margin: 1em 0 !important;
|
152 |
+
}
|
153 |
+
}
|
154 |
</style>
|
155 |
+
""", unsafe_allow_html=True)
|
156 |
+
|
157 |
try:
|
158 |
for chunk in response:
|
159 |
logger.debug(f"Chunk reçu: {chunk}")
|