Spaces:
Sleeping
Sleeping
Update tools/ai_technical_doc.py
Browse files- tools/ai_technical_doc.py +44 -1
tools/ai_technical_doc.py
CHANGED
|
@@ -3,6 +3,8 @@ import datetime
|
|
| 3 |
import re
|
| 4 |
from fpdf import FPDF
|
| 5 |
from langdetect import detect
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# === PDF Export Function ===
|
| 8 |
def export_text_to_pdf(text, output_path=None, language="en"):
|
|
@@ -48,7 +50,6 @@ def export_text_to_pdf(text, output_path=None, language="en"):
|
|
| 48 |
pdf.output(output_path)
|
| 49 |
return output_path
|
| 50 |
|
| 51 |
-
|
| 52 |
# === Questions ===
|
| 53 |
QUESTIONS = [
|
| 54 |
("system_name", "What is the name of your AI system?"),
|
|
@@ -67,3 +68,45 @@ QUESTIONS = [
|
|
| 67 |
|
| 68 |
def get_questions():
|
| 69 |
return QUESTIONS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import re
|
| 4 |
from fpdf import FPDF
|
| 5 |
from langdetect import detect
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
# === PDF Export Function ===
|
| 10 |
def export_text_to_pdf(text, output_path=None, language="en"):
|
|
|
|
| 50 |
pdf.output(output_path)
|
| 51 |
return output_path
|
| 52 |
|
|
|
|
| 53 |
# === Questions ===
|
| 54 |
QUESTIONS = [
|
| 55 |
("system_name", "What is the name of your AI system?"),
|
|
|
|
| 68 |
|
| 69 |
def get_questions():
|
| 70 |
return QUESTIONS
|
| 71 |
+
|
| 72 |
+
# === Optional: Run the tool standalone for debugging ===
|
| 73 |
+
def run_tool():
|
| 74 |
+
state = {"step": 0, "answers": {}}
|
| 75 |
+
|
| 76 |
+
def step_by_step_agent(user_input, state):
|
| 77 |
+
step = state["step"]
|
| 78 |
+
answers = state["answers"]
|
| 79 |
+
|
| 80 |
+
if step > 0:
|
| 81 |
+
key, _ = QUESTIONS[step - 1]
|
| 82 |
+
answers[key] = user_input
|
| 83 |
+
|
| 84 |
+
if step < len(QUESTIONS):
|
| 85 |
+
next_question = QUESTIONS[step][1]
|
| 86 |
+
state["step"] += 1
|
| 87 |
+
return next_question, state, None
|
| 88 |
+
|
| 89 |
+
content = "\n".join([f"- **{label}**: {answers.get(key, '')}" for key, label in QUESTIONS])
|
| 90 |
+
detected_lang = detect(content)
|
| 91 |
+
pdf_path = export_text_to_pdf(content, language=detected_lang)
|
| 92 |
+
return "✅ Completed. Download your documentation below.", {"done": True}, pdf_path
|
| 93 |
+
|
| 94 |
+
with gr.Blocks() as demo:
|
| 95 |
+
chatbot = gr.Chatbot(label="🧠 Technical Doc Assistant", value=[{"role": "assistant", "content": QUESTIONS[0][1]}], type="messages")
|
| 96 |
+
msg = gr.Textbox(label="Your answer")
|
| 97 |
+
state_var = gr.State(state)
|
| 98 |
+
file_output = gr.File(label="Download PDF")
|
| 99 |
+
reset_btn = gr.Button("🔁 Restart")
|
| 100 |
+
|
| 101 |
+
def chat_logic(msg_in, state_in):
|
| 102 |
+
reply, updated_state, file = step_by_step_agent(msg_in, state_in)
|
| 103 |
+
messages = [{"role": "user", "content": msg_in}]
|
| 104 |
+
if reply:
|
| 105 |
+
messages.append({"role": "assistant", "content": reply})
|
| 106 |
+
return messages, updated_state, file
|
| 107 |
+
|
| 108 |
+
def reset():
|
| 109 |
+
return [{"role": "assistant", "content": QUESTIONS[0][1]}], {"step": 0, "answers": {}}, None
|
| 110 |
+
|
| 111 |
+
msg.submit(chat_logic, [msg, state_var], [chatbot, state_var, file_output])
|
| 112 |
+
reset_btn.click(reset, outputs=[chatbot, state_var, file_output])
|