Spaces:
Sleeping
Sleeping
Update Gradio_UI.py
Browse files- Gradio_UI.py +23 -5
Gradio_UI.py
CHANGED
|
@@ -13,14 +13,26 @@ from smolagents.utils import _is_package_available
|
|
| 13 |
|
| 14 |
import gradio as gr
|
| 15 |
from fpdf import FPDF
|
|
|
|
| 16 |
|
| 17 |
-
# === PDF Export Function ===
|
| 18 |
-
def export_text_to_pdf(text, output_path="ai_act_register.pdf"):
|
| 19 |
pdf = FPDF()
|
| 20 |
pdf.add_page()
|
| 21 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 22 |
pdf.set_font("Arial", size=12)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
for line in text.split('\n'):
|
| 25 |
pdf.multi_cell(0, 10, line)
|
| 26 |
|
|
@@ -107,6 +119,7 @@ def stream_to_gradio(agent, task: str, reset_agent_memory: bool = False, additio
|
|
| 107 |
raise ModuleNotFoundError("Install 'gradio': pip install 'smolagents[gradio]'")
|
| 108 |
|
| 109 |
final_output_text = ""
|
|
|
|
| 110 |
for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
|
| 111 |
if hasattr(agent.model, "last_input_token_count"):
|
| 112 |
if isinstance(step_log, ActionStep):
|
|
@@ -118,8 +131,13 @@ def stream_to_gradio(agent, task: str, reset_agent_memory: bool = False, additio
|
|
| 118 |
if message.role == "assistant" and isinstance(message.content, str):
|
| 119 |
final_output_text += message.content + "\n"
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
yield gr.File(value=pdf_path, label="Télécharger le registre PDF")
|
| 124 |
|
| 125 |
# === UI ===
|
|
@@ -129,7 +147,7 @@ class GradioUI:
|
|
| 129 |
raise ModuleNotFoundError("Install 'gradio': pip install 'smolagents[gradio]'")
|
| 130 |
self.agent = agent
|
| 131 |
self.file_upload_folder = file_upload_folder
|
| 132 |
-
if self.file_upload_folder and not os.path.exists(file_upload_folder):
|
| 133 |
os.mkdir(self.file_upload_folder)
|
| 134 |
|
| 135 |
def interact_with_agent(self, prompt, messages):
|
|
|
|
| 13 |
|
| 14 |
import gradio as gr
|
| 15 |
from fpdf import FPDF
|
| 16 |
+
from langdetect import detect
|
| 17 |
|
| 18 |
+
# === PDF Export Function with Language Option ===
|
| 19 |
+
def export_text_to_pdf(text, output_path="ai_act_register.pdf", language="fr"):
|
| 20 |
pdf = FPDF()
|
| 21 |
pdf.add_page()
|
| 22 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 23 |
pdf.set_font("Arial", size=12)
|
| 24 |
|
| 25 |
+
# Optional: Translate heading depending on language
|
| 26 |
+
if language == "en":
|
| 27 |
+
title = "AI Act Compliance Register"
|
| 28 |
+
else:
|
| 29 |
+
title = "Registre de Conformité AI Act"
|
| 30 |
+
|
| 31 |
+
pdf.set_font("Arial", 'B', 14)
|
| 32 |
+
pdf.cell(0, 10, title, ln=True)
|
| 33 |
+
pdf.ln(10)
|
| 34 |
+
pdf.set_font("Arial", size=12)
|
| 35 |
+
|
| 36 |
for line in text.split('\n'):
|
| 37 |
pdf.multi_cell(0, 10, line)
|
| 38 |
|
|
|
|
| 119 |
raise ModuleNotFoundError("Install 'gradio': pip install 'smolagents[gradio]'")
|
| 120 |
|
| 121 |
final_output_text = ""
|
| 122 |
+
|
| 123 |
for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
|
| 124 |
if hasattr(agent.model, "last_input_token_count"):
|
| 125 |
if isinstance(step_log, ActionStep):
|
|
|
|
| 131 |
if message.role == "assistant" and isinstance(message.content, str):
|
| 132 |
final_output_text += message.content + "\n"
|
| 133 |
|
| 134 |
+
try:
|
| 135 |
+
language = detect(final_output_text)
|
| 136 |
+
except:
|
| 137 |
+
language = "fr"
|
| 138 |
+
|
| 139 |
+
pdf_path = export_text_to_pdf(final_output_text, language=language)
|
| 140 |
+
yield gr.ChatMessage(role="assistant", content=f"✅ Document AI Act exporté en PDF ({language.upper()}) : {pdf_path}")
|
| 141 |
yield gr.File(value=pdf_path, label="Télécharger le registre PDF")
|
| 142 |
|
| 143 |
# === UI ===
|
|
|
|
| 147 |
raise ModuleNotFoundError("Install 'gradio': pip install 'smolagents[gradio]'")
|
| 148 |
self.agent = agent
|
| 149 |
self.file_upload_folder = file_upload_folder
|
| 150 |
+
if self.file_upload_folder and not os.path.exists(self.file_upload_folder):
|
| 151 |
os.mkdir(self.file_upload_folder)
|
| 152 |
|
| 153 |
def interact_with_agent(self, prompt, messages):
|