Update src/streamlit_app.py
Browse files- src/streamlit_app.py +89 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,91 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
"
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
+
# --- Page Configuration ---
|
| 4 |
+
st.set_page_config(
|
| 5 |
+
page_title="Keyboard Layout Fixer",
|
| 6 |
+
page_icon="⌨️",
|
| 7 |
+
layout="centered",
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
# --- Image and Title ---
|
| 11 |
+
# Display the keyboard image you provided
|
| 12 |
+
st.image(
|
| 13 |
+
"https://i.imgur.com/kQoK6c9.png",
|
| 14 |
+
caption="Apple Magic Keyboard with Ukrainian/English Layout"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
st.title("⌨️ Keyboard Layout Fixer")
|
| 18 |
+
st.markdown(
|
| 19 |
+
"""
|
| 20 |
+
Ever typed a whole sentence only to realize you forgot to switch keyboard layouts? 🤦
|
| 21 |
+
For example, typing `руддщ` instead of `hello`.
|
| 22 |
+
|
| 23 |
+
This simple tool instantly converts text typed in the wrong layout between **Ukrainian** and **English**.
|
| 24 |
+
"""
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# --- Translation Dictionaries and Function ---
|
| 28 |
+
|
| 29 |
+
# Provided Ukrainian to English layout map
|
| 30 |
+
ukr_to_eng_keyboard_layout = {
|
| 31 |
+
'й': 'q', 'ц': 'w', 'у': 'e', 'к': 'r', 'е': 't', 'н': 'y', 'г': 'u', 'ш': 'i', 'щ': 'o', 'з': 'p', 'х': '[', 'ї': ']', 'ф': 'a', 'і': 's', 'в': 'd', 'а': 'f', 'п': 'g', 'р': 'h', 'о': 'j', 'л': 'k', 'д': 'l', 'ж': ';', 'є': "'", 'я': 'z', 'ч': 'x', 'с': 'c', 'м': 'v', 'и': 'b', 'т': 'n', 'ь': 'm', 'б': ',', 'ю': '.',
|
| 32 |
+
'Й': 'Q', 'Ц': 'W', 'У': 'E', 'К': 'R', 'Е': 'T', 'Н': 'Y', 'Г': 'U', 'Ш': 'I', 'Щ': 'O', 'З': 'P', 'Х': '{', 'Ї': '}', 'Ф': 'A', 'І': 'S', 'В': 'D', 'А': 'F', 'П': 'G', 'Р': 'H', 'О': 'J', 'Л': 'K', 'Д': 'L', 'Ж': ':', 'Є': '"', 'Я': 'Z', 'Ч': 'X', 'С': 'C', 'М': 'V', 'И': 'B', 'Т': 'N', 'Ь': 'M', 'Б': '<', 'Ю': '>',
|
| 33 |
+
"'": '`', '₴': '~', '.': '/', ',': '?'
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# Create the reverse map for English to Ukrainian
|
| 37 |
+
eng_to_ukr_keyboard_layout = {v: k for k, v in ukr_to_eng_keyboard_layout.items()}
|
| 38 |
+
|
| 39 |
+
def fix_layout(text, layout_map):
|
| 40 |
+
"""
|
| 41 |
+
Translates text from one keyboard layout to another using a dictionary.
|
| 42 |
+
"""
|
| 43 |
+
# Use a list comprehension and join for a more efficient implementation
|
| 44 |
+
return "".join([layout_map.get(char, char) for char in text])
|
| 45 |
+
|
| 46 |
+
# --- Sidebar for Options ---
|
| 47 |
+
st.sidebar.header("Settings")
|
| 48 |
+
direction = st.sidebar.radio(
|
| 49 |
+
"Choose conversion direction:",
|
| 50 |
+
("🇺🇦 Ukrainian → 🇬🇧 English", "🇬🇧 English → 🇺🇦 Ukrainian"),
|
| 51 |
+
key="direction_radio"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# --- Main App Interface ---
|
| 55 |
+
st.header("Input")
|
| 56 |
+
|
| 57 |
+
# Choose the appropriate label and placeholder based on the direction
|
| 58 |
+
if direction == "🇺🇦 Ukrainian → 🇬🇧 English":
|
| 59 |
+
input_label = "Enter text typed in Ukrainian layout (that was meant to be English):"
|
| 60 |
+
placeholder_text = "руддщ, цщкдв!" # Intended: "hello, world!"
|
| 61 |
+
layout_map = ukr_to_eng_keyboard_layout
|
| 62 |
+
else:
|
| 63 |
+
input_label = "Enter text typed in English layout (that was meant to be Ukrainian):"
|
| 64 |
+
placeholder_text = "ghbdsn, cdsn!" # Intended: "привіт, світ!"
|
| 65 |
+
layout_map = eng_to_ukr_keyboard_layout
|
| 66 |
+
|
| 67 |
+
# Text area for user input
|
| 68 |
+
original_text = st.text_area(
|
| 69 |
+
label=input_label,
|
| 70 |
+
value="",
|
| 71 |
+
placeholder=f"e.g., '{placeholder_text}'",
|
| 72 |
+
height=150,
|
| 73 |
+
key="input_text_area"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# --- Translation Logic and Output ---
|
| 77 |
+
if original_text:
|
| 78 |
+
translated_text = fix_layout(original_text, layout_map)
|
| 79 |
+
|
| 80 |
+
st.header("Result")
|
| 81 |
+
st.code(translated_text, language=None)
|
| 82 |
+
|
| 83 |
+
# Simple copy "instruction"
|
| 84 |
+
st.info("The result text is displayed in the box above for easy copying.")
|
| 85 |
+
|
| 86 |
+
else:
|
| 87 |
+
st.info("Enter some text in the box above to see the conversion.")
|
| 88 |
+
|
| 89 |
+
# --- Footer ---
|
| 90 |
+
st.markdown("---")
|
| 91 |
+
st.markdown("Made with ❤️ using [Streamlit](https://streamlit.io).")
|