Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ def load_prompts(file_path="prompts.json"):
|
|
| 13 |
except Exception as e:
|
| 14 |
print(f"β οΈ Could not load prompts.json: {e}")
|
| 15 |
return {
|
| 16 |
-
"base_system": "You are a bilingual translator fluent in
|
| 17 |
"styles": {},
|
| 18 |
"direction_prompts": {},
|
| 19 |
"platform_notes": {}
|
|
@@ -56,7 +56,7 @@ def build_system_instruction(direction: str, style: str) -> str:
|
|
| 56 |
return system_instruction
|
| 57 |
|
| 58 |
def gemini_translate(client, text: str, direction: str, style: str):
|
| 59 |
-
"""Translate between
|
| 60 |
system_instruction = build_system_instruction(direction, style)
|
| 61 |
direction_prompt = PROMPTS.get("direction_prompts", {}).get(direction, "")
|
| 62 |
|
|
@@ -112,24 +112,31 @@ def translate(api_key, text, image, direction, style):
|
|
| 112 |
result = f"β Error: {e}"
|
| 113 |
return result
|
| 114 |
|
|
|
|
|
|
|
|
|
|
| 115 |
with gr.Blocks(theme=gr.themes.Soft(), title="Ebonics β English Translator") as demo:
|
| 116 |
gr.Markdown("# Ebonics β Standard English Translator")
|
| 117 |
|
| 118 |
with gr.Row():
|
| 119 |
with gr.Column(scale=1):
|
| 120 |
-
api_key = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
direction = gr.Radio(
|
| 123 |
-
["
|
| 124 |
label="Translation Direction",
|
| 125 |
-
value="
|
| 126 |
)
|
| 127 |
|
| 128 |
style = gr.Dropdown(
|
| 129 |
label="Translation Style",
|
| 130 |
-
choices=["
|
| 131 |
-
value="Casual
|
| 132 |
-
info="Choose translation tone or style (changes with direction)."
|
| 133 |
)
|
| 134 |
|
| 135 |
text_input = gr.Textbox(
|
|
@@ -145,10 +152,17 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Ebonics β English Translator") a
|
|
| 145 |
output_box = gr.Textbox(label="Translated Output", lines=8)
|
| 146 |
|
| 147 |
def update_styles(direction):
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
else:
|
| 151 |
-
return gr.update(
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
direction.change(update_styles, inputs=[direction], outputs=[style])
|
| 154 |
|
|
|
|
| 13 |
except Exception as e:
|
| 14 |
print(f"β οΈ Could not load prompts.json: {e}")
|
| 15 |
return {
|
| 16 |
+
"base_system": "You are a bilingual translator fluent in Ebonics and English.",
|
| 17 |
"styles": {},
|
| 18 |
"direction_prompts": {},
|
| 19 |
"platform_notes": {}
|
|
|
|
| 56 |
return system_instruction
|
| 57 |
|
| 58 |
def gemini_translate(client, text: str, direction: str, style: str):
|
| 59 |
+
"""Translate between Ebonics and Standard English using prompts.json + platform notes."""
|
| 60 |
system_instruction = build_system_instruction(direction, style)
|
| 61 |
direction_prompt = PROMPTS.get("direction_prompts", {}).get(direction, "")
|
| 62 |
|
|
|
|
| 112 |
result = f"β Error: {e}"
|
| 113 |
return result
|
| 114 |
|
| 115 |
+
|
| 116 |
+
# ==== GRADIO UI ====
|
| 117 |
+
|
| 118 |
with gr.Blocks(theme=gr.themes.Soft(), title="Ebonics β English Translator") as demo:
|
| 119 |
gr.Markdown("# Ebonics β Standard English Translator")
|
| 120 |
|
| 121 |
with gr.Row():
|
| 122 |
with gr.Column(scale=1):
|
| 123 |
+
api_key = gr.Textbox(
|
| 124 |
+
label="π Gemini API Key",
|
| 125 |
+
type="password",
|
| 126 |
+
placeholder="Enter your Gemini API key here"
|
| 127 |
+
)
|
| 128 |
|
| 129 |
direction = gr.Radio(
|
| 130 |
+
["Ebonics β English", "English β Ebonics"],
|
| 131 |
label="Translation Direction",
|
| 132 |
+
value="Ebonics β English"
|
| 133 |
)
|
| 134 |
|
| 135 |
style = gr.Dropdown(
|
| 136 |
label="Translation Style",
|
| 137 |
+
choices=["Formal", "Casual", "Academic", "Default"],
|
| 138 |
+
value="Casual",
|
| 139 |
+
info="Choose a translation tone or style (changes with direction)."
|
| 140 |
)
|
| 141 |
|
| 142 |
text_input = gr.Textbox(
|
|
|
|
| 152 |
output_box = gr.Textbox(label="Translated Output", lines=8)
|
| 153 |
|
| 154 |
def update_styles(direction):
|
| 155 |
+
"""Update style options dynamically based on direction."""
|
| 156 |
+
if direction == "Ebonics β English":
|
| 157 |
+
return gr.update(
|
| 158 |
+
choices=["Formal", "Casual", "Academic", "Default"],
|
| 159 |
+
value="Casual"
|
| 160 |
+
)
|
| 161 |
else:
|
| 162 |
+
return gr.update(
|
| 163 |
+
choices=["Modern", "Classic", "Chill", "Energetic", "Default"],
|
| 164 |
+
value="Modern"
|
| 165 |
+
)
|
| 166 |
|
| 167 |
direction.change(update_styles, inputs=[direction], outputs=[style])
|
| 168 |
|