cringo commited on
Commit
bc4f7b2
·
verified ·
1 Parent(s): 857dd15
Files changed (1) hide show
  1. app.py +83 -6
app.py CHANGED
@@ -1,14 +1,91 @@
 
 
 
 
1
  import gradio as gr
 
2
 
3
- def echo(a, b, c):
4
- return f"Echo: {a}, {b}, {c}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  demo = gr.Interface(
7
- fn=echo,
8
- inputs=["text", "text", "text"],
 
 
 
 
9
  outputs="text",
10
- api_name="predict"
 
 
11
  )
12
 
13
  if __name__ == "__main__":
14
- demo.launch()
 
1
+ import os
2
+ import threading
3
+ import tempfile
4
+ import requests
5
  import gradio as gr
6
+ from pathlib import Path
7
 
8
+ from bbb import BBB
9
+
10
+ TELEGRAM_BOT_TOKEN = os.environ["TELEGRAM_BOT_TOKEN"]
11
+ MODEL_PATH = "/models/sentence-transformers/LaBSE"
12
+
13
+ def send_telegram_document(chat_id: str, file_path: str):
14
+ """Upload a document to a Telegram chat."""
15
+ url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendDocument"
16
+ with open(file_path, "rb") as f:
17
+ files = {"document": f}
18
+ data = {"chat_id": chat_id}
19
+ resp = requests.post(url, data=data, files=files)
20
+ resp.raise_for_status()
21
+ return resp.json()
22
+
23
+ def process_in_background(source_url: str, target_url: str, chat_id: str):
24
+ """Background task: download EPUBs, run BBB, send result."""
25
+ try:
26
+ src_path = Path(tempfile.mktemp(suffix=".epub"))
27
+ tgt_path = Path(tempfile.mktemp(suffix=".epub"))
28
+ out_path = Path(tempfile.mktemp(suffix=".epub"))
29
+
30
+ for url, dest in [(source_url, src_path), (target_url, tgt_path)]:
31
+ r = requests.get(url, timeout=60)
32
+ r.raise_for_status()
33
+ dest.write_bytes(r.content)
34
+
35
+ cpu_cores = int(os.environ.get("CPU_CORES", 2))
36
+ bbb = BBB(
37
+ source_epub_path=str(src_path),
38
+ target_epub_path=str(tgt_path),
39
+ output=str(out_path),
40
+ auto_match_chapter_threshold=0.6,
41
+ model=MODEL_PATH,
42
+ threads=cpu_cores,
43
+ )
44
+ bbb.run()
45
+
46
+ send_telegram_document(chat_id, str(out_path))
47
+
48
+ except Exception as e:
49
+ url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
50
+ requests.post(url, json={
51
+ "chat_id": chat_id,
52
+ "text": f"❌ Processing failed: {str(e)}"
53
+ })
54
+ finally:
55
+ for p in [src_path, tgt_path, out_path]:
56
+ try:
57
+ p.unlink(missing_ok=True)
58
+ except:
59
+ pass
60
+
61
+ def process_epubs(source_url: str, target_url: str, chat_id: str) -> str:
62
+ """
63
+ Gradio function that can be called via the API.
64
+ It starts the actual work in a background thread and returns immediately.
65
+ """
66
+ if not source_url or not target_url or not chat_id:
67
+ return "Missing parameters"
68
+
69
+ thread = threading.Thread(
70
+ target=process_in_background,
71
+ args=(source_url, target_url, chat_id),
72
+ daemon=True
73
+ )
74
+ thread.start()
75
+ return f"🚀 Processing started for chat {chat_id}. You will receive the bilingual EPUB when it's ready."
76
 
77
  demo = gr.Interface(
78
+ fn=process_epubs,
79
+ inputs=[
80
+ gr.Textbox(label="source_url"),
81
+ gr.Textbox(label="target_url"),
82
+ gr.Textbox(label="chat_id"),
83
+ ],
84
  outputs="text",
85
+ title="Bilingual Book Builder (API)",
86
+ flagging_mode="never",
87
+ api_name="run"
88
  )
89
 
90
  if __name__ == "__main__":
91
+ demo.launch()