Spaces:
Running
Running
Update app.py
Browse files11 unmodified lines121314151617185 unmodified lines242526272829303162 unmodified lines949596979899100130 unmodified lines23123223323423523623710 unmodified lines24824925025125225325418 unmodified lines2732742752762772782791 unmodified line2812822832842852862874 unmodified lines29229329429529629729811 unmodified linesfrom docx.oxml.ns import nsdecls, qn# ---------------------------------------------------------------------------# 1. CORE API LINGUISTIC PIPELINE (WORD-BY-WORD SELECTION OVER MODEL 1.5 FLASH)# ---------------------------------------------------------------------------def process_live_ai_request(word, api_key):"""5 unmodified linesif not clean_key:return "Missing Key", "Missing Key", "Enter valid key above"# Updated API gateway url string pointing exactly to stable gemini-1.5-flashurl = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={clean_key}"headers = {"Content-Type": "application/json"}prompt = f"""62 unmodified linesmorphology = lines[2] if len(lines) > 2 else "error"return transliteration, translation, morphology.replace(".", "")else:return f"API Error ({response.status_code})", "Rate Limit Active", "error"except Exception as e:return "Connection Timeout", "Network Error", "error"130 unmodified linesreturn "".join(html_elements)# ---------------------------------------------------------------------------# 5. PACED WORD-BY-WORD EXECUTION ROUTER (FIXES EXCEPTION 429)# ---------------------------------------------------------------------------def process_pipeline(api_key, ms_name, book, ch, ver, raw_input):if not raw_input.strip():10 unmodified linesword_packages = []for tok in tokens:# Pacing step spacing out sequential web hits by exactly 400 milliseconds to avoid 429 lockstime.sleep(0.4)tl, ts, mo = process_live_ai_request(tok, api_key)word_packages.append((tok, tl, ts, mo))18 unmodified linesdoc.save(file_path)elapsed = time.time() - start_timestatus = f"Success! Processed {len(tokens)} words in {elapsed:.2f} seconds across Gemini 1.5 Flash."return status, html_canvas, gr.update(value=file_path, visible=True)1 unmodified linereturn "", "Success: Canvas workspace cleared cleanly.", "", gr.update(visible=False)# ---------------------------------------------------------------------------# 6. GRADIO UI LAYOUT CONFIGURATIONS (WITH EXPLICIT VISIBILITY FIX)# ---------------------------------------------------------------------------bei_css = """body, .gradio-container { background-color: #F4F3EF !important; }4 unmodified lines.clear-btn { background-color: #718096 !important; color: white !important; font-weight: bold !important; border-radius: 4px !important; margin: 5px 0; }.copy-btn { background-color: #D4AF37 !important; color: #1B4D3E !important; font-weight: bold !important; border-radius: 4px !important; margin: 5px 0; border: 1px solid #1B4D3E !important; }/* FIX: Triple override rules forcing dark charcoal on text layouts inside guide box */.api-guide-box { background-color: #E2E8F0 !important; border-left: 5px solid #1B4D3E !important; padding: 12px; border-radius: 4px; margin-bottom: 10px; }.api-guide-box p, .api-guide-box span, .api-guide-box b, .api-guide-box strong, .api-guide-box a {color: #2D3748 !important;11 unmodified lines121314151617185 unmodified lines242526272829303162 unmodified lines949596979899100130 unmodified lines23123223323423523623710 unmodified lines24824925025125225325418 unmodified lines2732742752762772782791 unmodified line2812822832842852862874 unmodified lines29229329429529629711 unmodified linesfrom docx.oxml.ns import nsdecls, qn# ---------------------------------------------------------------------------# 1. CORE API LINGUISTIC PIPELINE (WORD-BY-WORD WITH CORRECT 1.5 FLASH ROUTING)# ---------------------------------------------------------------------------def process_live_ai_request(word, api_key):"""5 unmodified linesif not clean_key:return "Missing Key", "Missing Key", "Enter valid key above"# FIX: Corrected model string path to 'gemini-1.5-flash-latest' to eliminate HTTP 404 routing errorsurl = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key={clean_key}"headers = {"Content-Type": "application/json"}prompt = f"""62 unmodified linesmorphology = lines[2] if len(lines) > 2 else "error"return transliteration, translation, morphology.replace(".", "")else:return f"API Error ({response.status_code})", "Connection Interrupted", "error"except Exception as e:return "Connection Timeout", "Network Error", "error"130 unmodified linesreturn "".join(html_elements)# ---------------------------------------------------------------------------# 5. PACED WORD-BY-WORD EXECUTION ROUTER# ---------------------------------------------------------------------------def process_pipeline(api_key, ms_name, book, ch, ver, raw_input):if not raw_input.strip():10 unmodified linesword_packages = []for tok in tokens:# 400 millisecond delay to ensure continuous queries never trigger rate lockstime.sleep(0.4)tl, ts, mo = process_live_ai_request(tok, api_key)word_packages.append((tok, tl, ts, mo))18 unmodified linesdoc.save(file_path)elapsed = time.time() - start_timestatus = f"Success! Processed {len(tokens)} words in {elapsed:.2f} seconds."return status, html_canvas, gr.update(value=file_path, visible=True)1 unmodified linereturn "", "Success: Canvas workspace cleared cleanly.", "", gr.update(visible=False)# ---------------------------------------------------------------------------# 6. GRADIO UI LAYOUT CONFIGURATIONS# ---------------------------------------------------------------------------bei_css = """body, .gradio-container { background-color: #F4F3EF !important; }4 unmodified lines.clear-btn { background-color: #718096 !important; color: white !important; font-weight: bold !important; border-radius: 4px !important; margin: 5px 0; }.copy-btn { background-color: #D4AF37 !important; color: #1B4D3E !important; font-weight: bold !important; border-radius: 4px !important; margin: 5px 0; border: 1px solid #1B4D3E !important; }.api-guide-box { background-color: #E2E8F0 !important; border-left: 5px solid #1B4D3E !important; padding: 12px; border-radius: 4px; margin-bottom: 10px; }.api-guide-box p, .api-guide-box span, .api-guide-box b, .api-guide-box strong, .api-guide-box a {color: #2D3748 !important;
app.py+8-9