Juna190825 commited on
Commit
0e751e1
·
verified ·
1 Parent(s): 55372df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +287 -16
app.py CHANGED
@@ -1,3 +1,286 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from fastapi import FastAPI
3
  from fastapi.middleware.cors import CORSMiddleware
@@ -12,7 +295,6 @@ from huggingface_hub import HfApi, CommitOperationAdd
12
  import time
13
  from datetime import datetime
14
  from langdetect import detect
15
- from huggingface_hub import update_dataset_card
16
 
17
  import json
18
  import threading
@@ -76,10 +358,8 @@ def commit_logs_to_hf(manual=False):
76
  return f"Committed logs to {repo_path}"
77
 
78
  def update_dataset_card_info():
79
- api = HfApi()
80
-
81
  # Build the README.md content dynamically
82
- readme = f"""
83
  # Zomi Translator Logs
84
 
85
  **License:** MIT
@@ -88,20 +368,12 @@ def update_dataset_card_info():
88
  Daily logs of Zomi ↔ English translations.
89
  """
90
 
91
- api.create_commit(
92
  repo_id=HF_DATASET,
93
  repo_type="dataset",
94
- commit_message="Update dataset card metadata",
95
- operations=[
96
- CommitOperationAdd(
97
- path_in_repo="README.md",
98
- path_or_fileobj=readme.encode("utf-8")
99
- )
100
- ]
101
  )
102
 
103
-
104
-
105
 
106
  def manual_commit(password: str):
107
  if not ADMIN_PASSWORD:
@@ -116,7 +388,6 @@ def manual_commit(password: str):
116
  def append_log_async(input_text, output_text, direction):
117
  log_queue.put({
118
  "ts": datetime.utcnow().isoformat() + "Z",
119
- # "src_text_hash": hashlib.sha256(input_text.encode("utf-8")).hexdigest(),
120
  "src_text": input_text,
121
  "tgt_text": output_text[:500],
122
  "direction": direction,
@@ -278,4 +549,4 @@ app = gr.mount_gradio_app(app, demo, path="/")
278
  if __name__ == "__main__":
279
  # Respect PORT env var (used by Hugging Face Spaces)
280
  port = int(os.getenv("PORT", "7860"))
281
- uvicorn.run(app, host="0.0.0.0", port=port)
 
1
+ # import gradio as gr
2
+ # from fastapi import FastAPI
3
+ # from fastapi.middleware.cors import CORSMiddleware
4
+ # from pydantic import BaseModel
5
+ # from gradio_client import Client
6
+ # import uvicorn
7
+ # import os
8
+
9
+
10
+ # ############ logging, and committing translation ##############
11
+ # from huggingface_hub import HfApi, CommitOperationAdd
12
+ # import time
13
+ # from datetime import datetime
14
+ # from langdetect import detect
15
+ # from huggingface_hub import update_dataset_card
16
+
17
+ # import json
18
+ # import threading
19
+ # import queue
20
+ # import hashlib
21
+
22
+ # HF_DATASET = "Juna190825/zomi-translation-logs"
23
+ # HF_TOKEN = os.getenv("HF_TOKEN")
24
+ # ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")
25
+
26
+
27
+ # api = HfApi(token=HF_TOKEN)
28
+ # log_queue = queue.Queue()
29
+
30
+ # LOG_DIR = "/data"
31
+ # BUFFER_FILE = os.path.join(LOG_DIR, "log_buffer.jsonl")
32
+
33
+ # COMMIT_INTERVAL_SECONDS = 900 # every 15 minutes
34
+
35
+ # def append_log(input_text: str, output_text: str):
36
+ # os.makedirs(LOG_DIR, exist_ok=True)
37
+
38
+ # record = {
39
+ # "ts": datetime.utcnow().isoformat() + "Z",
40
+ # "src_text": input_text[:500],
41
+ # "tgt_text": output_text[:500],
42
+ # "app": "zomi-translator",
43
+ # "version": "1.0.0"
44
+ # }
45
+
46
+ # with open(BUFFER_FILE, "a", encoding="utf-8") as f:
47
+ # f.write(json.dumps(record, ensure_ascii=False) + "\n")
48
+
49
+ # def commit_logs_to_hf(manual=False):
50
+ # if not HF_TOKEN or not os.path.exists(BUFFER_FILE):
51
+ # return "No logs to commit."
52
+
53
+ # if not os.path.exists(BUFFER_FILE) or os.path.getsize(BUFFER_FILE) < 10:
54
+ # return "No new logs."
55
+
56
+ # with open(BUFFER_FILE, "r", encoding="utf-8") as f:
57
+ # data = f.read().strip()
58
+
59
+ # if not data:
60
+ # return "No new logs."
61
+
62
+ # date_str = datetime.utcnow().strftime("%Y-%m-%d")
63
+ # repo_path = f"logs/{date_str}.jsonl"
64
+
65
+ # api.upload_file(
66
+ # path_or_fileobj=BUFFER_FILE,
67
+ # path_in_repo=repo_path,
68
+ # repo_id=HF_DATASET,
69
+ # repo_type="dataset",
70
+ # commit_message="Manual log commit" if manual else "Auto daily log commit"
71
+ # )
72
+
73
+ # # Clear buffer after successful commit
74
+ # open(BUFFER_FILE, "w").close()
75
+
76
+ # return f"Committed logs to {repo_path}"
77
+
78
+ # def update_dataset_card_info():
79
+ # api = HfApi()
80
+
81
+ # # Build the README.md content dynamically
82
+ # readme = f"""
83
+ # # Zomi Translator Logs
84
+
85
+ # **License:** MIT
86
+ # **Languages:** Zomi, English
87
+
88
+ # Daily logs of Zomi ↔ English translations.
89
+ # """
90
+
91
+ # api.create_commit(
92
+ # repo_id=HF_DATASET,
93
+ # repo_type="dataset",
94
+ # commit_message="Update dataset card metadata",
95
+ # operations=[
96
+ # CommitOperationAdd(
97
+ # path_in_repo="README.md",
98
+ # path_or_fileobj=readme.encode("utf-8")
99
+ # )
100
+ # ]
101
+ # )
102
+
103
+
104
+
105
+
106
+ # def manual_commit(password: str):
107
+ # if not ADMIN_PASSWORD:
108
+ # return "❌ Admin password not configured."
109
+
110
+ # if password != ADMIN_PASSWORD:
111
+ # return "❌ Invalid admin password."
112
+
113
+ # result = commit_logs_to_hf(manual=True)
114
+ # return f"✅ {result}"
115
+
116
+ # def append_log_async(input_text, output_text, direction):
117
+ # log_queue.put({
118
+ # "ts": datetime.utcnow().isoformat() + "Z",
119
+ # # "src_text_hash": hashlib.sha256(input_text.encode("utf-8")).hexdigest(),
120
+ # "src_text": input_text,
121
+ # "tgt_text": output_text[:500],
122
+ # "direction": direction,
123
+ # "app": "zomi-translator",
124
+ # "version": "1.0.0"
125
+ # })
126
+
127
+ # def async_commit_worker():
128
+ # buffer = []
129
+ # while True:
130
+ # try:
131
+ # # Collect up to 50 logs or 60 seconds
132
+ # start = time.time()
133
+ # while len(buffer) < 50 and (time.time() - start < 60):
134
+ # try:
135
+ # buffer.append(log_queue.get(timeout=1))
136
+ # except queue.Empty:
137
+ # pass
138
+
139
+ # if buffer:
140
+ # os.makedirs(LOG_DIR, exist_ok=True)
141
+ # date_str = datetime.utcnow().strftime("%Y-%m-%d")
142
+ # batch_file = os.path.join(LOG_DIR, f"{date_str}.jsonl")
143
+
144
+ # with open(batch_file, "a", encoding="utf-8") as f:
145
+ # for record in buffer:
146
+ # f.write(json.dumps(record, ensure_ascii=False) + "\n")
147
+
148
+ # commit_logs_to_hf() # commits the batch file
149
+ # update_dataset_card_info()
150
+ # buffer.clear()
151
+ # except Exception as e:
152
+ # print("Async commit failed:", e)
153
+
154
+ # ###############################################################
155
+
156
+ # app = FastAPI()
157
+
158
+ # # Enable CORS
159
+ # app.add_middleware(
160
+ # CORSMiddleware,
161
+ # allow_origins=["*"],
162
+ # allow_credentials=True,
163
+ # allow_methods=["*"],
164
+ # allow_headers=["*"],
165
+ # )
166
+
167
+ # # Initialize client once
168
+ # translator_client = Client("Chatboong/Gemini_Translator")
169
+
170
+
171
+ # def call_translator(text: str):
172
+ # msg = f"Translate Zomi to English, if it is English translate it to Zomi: '{text}'\n"
173
+
174
+ # stream = translator_client.predict(
175
+ # message=msg,
176
+ # lang="English",
177
+ # is_streaming=True,
178
+ # api_name="/chat",
179
+ # )
180
+
181
+ # output = ""
182
+ # for chunk in stream:
183
+ # output += str(chunk)
184
+
185
+ # # Remove prefix
186
+ # prefix = "Translate Zomi to English, if it is English translate it to Zomi: "
187
+ # prefix2 = "Zomi pan English in tei in, English ahih leh Zomi in tei in: "
188
+ # prefix3 = 'Zomi-in tei in, English ahih leh Zomi-in tei in: '
189
+ # if output.startswith(prefix):
190
+ # output = output[len(prefix):].strip()
191
+ # # Remove surrounding quotes
192
+ # if (output.startswith('"') and output.endswith('"')) or (output.startswith("'") and output.endswith("'")):
193
+ # output = output[1:-1].strip()
194
+
195
+ # elif output.startswith(prefix2):
196
+ # output = output[len(prefix2):].strip()
197
+ # # Remove surrounding quotes
198
+ # if (output.startswith('"') and output.endswith('"')) or (output.startswith("'") and output.endswith("'")):
199
+ # output = output[1:-1].strip()
200
+ # elif output.startswith(prefix3):
201
+ # output = output[len(prefix3):].strip()
202
+ # # Remove surrounding quotes
203
+ # if (output.startswith('"') and output.endswith('"')) or (output.startswith("'") and output.endswith("'")):
204
+ # output = output[1:-1].strip()
205
+ # append_log(text, output)
206
+ # return output
207
+
208
+ # def detect_direction(text: str) -> str:
209
+ # try:
210
+ # lang = detect(text)
211
+ # if lang == "en":
212
+ # return "en-zomi"
213
+ # else:
214
+ # return "zomi-en"
215
+ # except:
216
+ # return "en-zomi"
217
+
218
+
219
+ # def translate_zomi(text: str):
220
+ # direction = detect_direction(text)
221
+ # output = call_translator(text) # your existing streaming code
222
+ # append_log_async(text, output, direction)
223
+ # return output
224
+
225
+
226
+
227
+ # class ChatRequest(BaseModel):
228
+ # message: str
229
+
230
+
231
+ # @app.post("/chat")
232
+ # async def chat_api(req: ChatRequest):
233
+ # translation = translate_zomi(req.message)
234
+ # return {"translation": translation}
235
+
236
+
237
+ # def chat_ui(message: str):
238
+ # return translate_zomi(message)
239
+
240
+
241
+ # with gr.Blocks() as demo:
242
+ # gr.Markdown("### Zomi Translator")
243
+
244
+ # inp = gr.Textbox(label="Input")
245
+ # out = gr.Textbox(label="Output")
246
+
247
+ # inp.submit(chat_ui, inp, out)
248
+
249
+ # if ADMIN_PASSWORD:
250
+ # gr.Markdown("### Admin (Protected)")
251
+
252
+ # admin_pw = gr.Textbox(
253
+ # label="Admin Password",
254
+ # type="password",
255
+ # placeholder="Enter admin password"
256
+ # )
257
+
258
+ # commit_btn = gr.Button("📦 Commit Logs Now")
259
+ # status = gr.Textbox(label="Commit Status", interactive=False)
260
+
261
+ # commit_btn.click(
262
+ # manual_commit,
263
+ # inputs=admin_pw,
264
+ # outputs=status
265
+ # ).then(
266
+ # lambda: "",
267
+ # None,
268
+ # admin_pw
269
+ # )
270
+
271
+
272
+ # if HF_TOKEN:
273
+ # threading.Thread(target=async_commit_worker, daemon=True).start()
274
+
275
+ # # Mount Gradio under the FastAPI app
276
+ # app = gr.mount_gradio_app(app, demo, path="/")
277
+
278
+ # if __name__ == "__main__":
279
+ # # Respect PORT env var (used by Hugging Face Spaces)
280
+ # port = int(os.getenv("PORT", "7860"))
281
+ # uvicorn.run(app, host="0.0.0.0", port=port)
282
+
283
+
284
  import gradio as gr
285
  from fastapi import FastAPI
286
  from fastapi.middleware.cors import CORSMiddleware
 
295
  import time
296
  from datetime import datetime
297
  from langdetect import detect
 
298
 
299
  import json
300
  import threading
 
358
  return f"Committed logs to {repo_path}"
359
 
360
  def update_dataset_card_info():
 
 
361
  # Build the README.md content dynamically
362
+ readme_content = f"""
363
  # Zomi Translator Logs
364
 
365
  **License:** MIT
 
368
  Daily logs of Zomi ↔ English translations.
369
  """
370
 
371
+ api.update_repo_card(
372
  repo_id=HF_DATASET,
373
  repo_type="dataset",
374
+ card_data=readme_content
 
 
 
 
 
 
375
  )
376
 
 
 
377
 
378
  def manual_commit(password: str):
379
  if not ADMIN_PASSWORD:
 
388
  def append_log_async(input_text, output_text, direction):
389
  log_queue.put({
390
  "ts": datetime.utcnow().isoformat() + "Z",
 
391
  "src_text": input_text,
392
  "tgt_text": output_text[:500],
393
  "direction": direction,
 
549
  if __name__ == "__main__":
550
  # Respect PORT env var (used by Hugging Face Spaces)
551
  port = int(os.getenv("PORT", "7860"))
552
+ uvicorn.run(app, host="0.0.0.0", port=port)