feat: translation, add dropdowns
Browse files- .gitignore +6 -0
- app.py +57 -13
- process/interpretation.py +8 -10
- process/ocr.py +4 -1
- process/translation.py +32 -0
.gitignore
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.gradio/
|
| 2 |
+
.env
|
| 3 |
+
__pycache__/
|
| 4 |
+
process/__pycache__/
|
| 5 |
+
pyproject.toml
|
| 6 |
+
uv.lock
|
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from process.ocr import perform_raw_ocr, correct_text_with_ai
|
| 3 |
from process.interpretation import get_interpretation
|
|
|
|
| 4 |
from process.gradio_css import CUSTOM_CSS
|
| 5 |
|
| 6 |
|
|
@@ -52,7 +53,7 @@ def ai_correct(current_text: str):
|
|
| 52 |
yield error_msg, error_msg + "\n\n"
|
| 53 |
|
| 54 |
|
| 55 |
-
def interpretation_workflow(text: str, genre: str, learn_language, target_language):
|
| 56 |
if not GEMINI_API_KEY:
|
| 57 |
yield "Error: Gemini api key not found."
|
| 58 |
return
|
|
@@ -63,15 +64,33 @@ def interpretation_workflow(text: str, genre: str, learn_language, target_langua
|
|
| 63 |
yield "Error: Language not selected"
|
| 64 |
|
| 65 |
if genre.lower() in ["general", "news"]:
|
| 66 |
-
yield f"⏳ Generating interpretation for {genre}..."
|
| 67 |
result = get_interpretation(genre.lower(), GEMINI_API_KEY, text, learn_language, target_language)
|
| 68 |
yield result
|
| 69 |
else:
|
| 70 |
yield "not implemented yet"
|
| 71 |
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# --- API Key ---
|
| 77 |
with gr.Accordion("API Configuration", open=True):
|
|
@@ -103,7 +122,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
|
|
| 103 |
|
| 104 |
# --- Text Processing ---
|
| 105 |
gr.Markdown("---")
|
| 106 |
-
with gr.Tab("Text
|
| 107 |
|
| 108 |
with gr.Row():
|
| 109 |
with gr.Column(scale=1):
|
|
@@ -113,20 +132,20 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
|
|
| 113 |
file_types=["image", ".pdf", ".txt"]
|
| 114 |
)
|
| 115 |
process_button = gr.Button("1. File Process (OCR/Read)", variant="primary")
|
| 116 |
-
ai_correct_button = gr.Button("2. AI Correct", variant="
|
| 117 |
with gr.Column(scale=2):
|
| 118 |
gr.Markdown("### Processed result")
|
| 119 |
with gr.Tabs():
|
| 120 |
with gr.Tab("Raw Text"):
|
| 121 |
text_display = gr.Textbox(
|
| 122 |
-
label="Raw
|
| 123 |
lines=15,
|
| 124 |
max_lines=20,
|
| 125 |
show_copy_button=True,
|
| 126 |
value="",
|
| 127 |
interactive=True
|
| 128 |
)
|
| 129 |
-
with gr.Tab("Formatted
|
| 130 |
text_markdown = gr.Markdown(
|
| 131 |
value="*Processed text will appear here...*\n\n",
|
| 132 |
)
|
|
@@ -146,14 +165,14 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
|
|
| 146 |
)
|
| 147 |
|
| 148 |
# --- Text Interpertation ---
|
| 149 |
-
with gr.Tab("🎓
|
| 150 |
gr.Markdown("### Configure Interpretation Settings")
|
| 151 |
|
| 152 |
with gr.Row():
|
| 153 |
with gr.Column(scale=1):
|
| 154 |
-
prof_language_seletor = gr.
|
| 155 |
-
learn_language_seletor = gr.
|
| 156 |
-
style_seletor = gr.
|
| 157 |
interpret_button = gr.Button("Generate Interpretation", variant="primary")
|
| 158 |
|
| 159 |
with gr.Column(scale=2):
|
|
@@ -169,5 +188,30 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
|
|
| 169 |
outputs=interpretation_output
|
| 170 |
)
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
if __name__ == "__main__":
|
| 173 |
-
demo.launch(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from process.ocr import perform_raw_ocr, correct_text_with_ai
|
| 3 |
from process.interpretation import get_interpretation
|
| 4 |
+
from process.translation import get_tranlaton
|
| 5 |
from process.gradio_css import CUSTOM_CSS
|
| 6 |
|
| 7 |
|
|
|
|
| 53 |
yield error_msg, error_msg + "\n\n"
|
| 54 |
|
| 55 |
|
| 56 |
+
def interpretation_workflow(text: str, genre: str, learn_language: str, target_language: str):
|
| 57 |
if not GEMINI_API_KEY:
|
| 58 |
yield "Error: Gemini api key not found."
|
| 59 |
return
|
|
|
|
| 64 |
yield "Error: Language not selected"
|
| 65 |
|
| 66 |
if genre.lower() in ["general", "news"]:
|
| 67 |
+
yield f"⏳ Generating interpretation for genre: {[genre]}...(10s - 2min)"
|
| 68 |
result = get_interpretation(genre.lower(), GEMINI_API_KEY, text, learn_language, target_language)
|
| 69 |
yield result
|
| 70 |
else:
|
| 71 |
yield "not implemented yet"
|
| 72 |
|
| 73 |
|
| 74 |
+
def translation_workflow(text: str, target_language: str):
|
| 75 |
+
if not GEMINI_API_KEY:
|
| 76 |
+
yield "Error: Gemini api key not found."
|
| 77 |
+
return
|
| 78 |
+
if not text or text.strip() == "":
|
| 79 |
+
yield "Error: Text is empty"
|
| 80 |
+
return
|
| 81 |
+
if not target_language:
|
| 82 |
+
yield "Error: Language not selected"
|
| 83 |
+
|
| 84 |
+
if target_language in ["Deutsch", "English", "Français", "Русский язык", "中文"]:
|
| 85 |
+
yield f"⏳ Generating interpretation for target_language: {[target_language]}..."
|
| 86 |
+
result = get_tranlaton(text, GEMINI_API_KEY, target_language)
|
| 87 |
+
yield result
|
| 88 |
+
else:
|
| 89 |
+
yield "not implemented yet"
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
with gr.Blocks(theme=gr.themes.Monochrome(), css=CUSTOM_CSS) as demo:
|
| 93 |
+
gr.Markdown("# 📚 LogosAI - Intensive Reading in Any Language", elem_classes=["section-header"])
|
| 94 |
|
| 95 |
# --- API Key ---
|
| 96 |
with gr.Accordion("API Configuration", open=True):
|
|
|
|
| 122 |
|
| 123 |
# --- Text Processing ---
|
| 124 |
gr.Markdown("---")
|
| 125 |
+
with gr.Tab("Text"):
|
| 126 |
|
| 127 |
with gr.Row():
|
| 128 |
with gr.Column(scale=1):
|
|
|
|
| 132 |
file_types=["image", ".pdf", ".txt"]
|
| 133 |
)
|
| 134 |
process_button = gr.Button("1. File Process (OCR/Read)", variant="primary")
|
| 135 |
+
ai_correct_button = gr.Button("2. AI Correct", variant="primary")
|
| 136 |
with gr.Column(scale=2):
|
| 137 |
gr.Markdown("### Processed result")
|
| 138 |
with gr.Tabs():
|
| 139 |
with gr.Tab("Raw Text"):
|
| 140 |
text_display = gr.Textbox(
|
| 141 |
+
label="Raw Text(editable)",
|
| 142 |
lines=15,
|
| 143 |
max_lines=20,
|
| 144 |
show_copy_button=True,
|
| 145 |
value="",
|
| 146 |
interactive=True
|
| 147 |
)
|
| 148 |
+
with gr.Tab("Formatted Text"):
|
| 149 |
text_markdown = gr.Markdown(
|
| 150 |
value="*Processed text will appear here...*\n\n",
|
| 151 |
)
|
|
|
|
| 165 |
)
|
| 166 |
|
| 167 |
# --- Text Interpertation ---
|
| 168 |
+
with gr.Tab("🎓 Interpretation"):
|
| 169 |
gr.Markdown("### Configure Interpretation Settings")
|
| 170 |
|
| 171 |
with gr.Row():
|
| 172 |
with gr.Column(scale=1):
|
| 173 |
+
prof_language_seletor = gr.Dropdown(["DE", "EN", "FR", "RU", "ZH"], label="Prof's Language", value="EN")
|
| 174 |
+
learn_language_seletor = gr.Dropdown(["DE", "EN", "FR", "RU", "ZH"], label="Language to Learn", value="EN")
|
| 175 |
+
style_seletor = gr.Dropdown(["General", "Paper", "News", "Narrative", "Poem", "Philosophy"], label="Genre")
|
| 176 |
interpret_button = gr.Button("Generate Interpretation", variant="primary")
|
| 177 |
|
| 178 |
with gr.Column(scale=2):
|
|
|
|
| 188 |
outputs=interpretation_output
|
| 189 |
)
|
| 190 |
|
| 191 |
+
with gr.Tab("Translation"):
|
| 192 |
+
gr.Markdown("### Configure Translation Settings")
|
| 193 |
+
with gr.Row():
|
| 194 |
+
with gr.Column(scale=1):
|
| 195 |
+
target_language_selector = gr.Dropdown(
|
| 196 |
+
["Deutsch", "English", "Français", "Русский язык", "中文"],
|
| 197 |
+
value="English",
|
| 198 |
+
label="Target Language",
|
| 199 |
+
interactive=True)
|
| 200 |
+
translation_button = gr.Button("Translate!", variant="primary")
|
| 201 |
+
|
| 202 |
+
with gr.Column(scale=2):
|
| 203 |
+
interpretation_output = gr.Markdown(
|
| 204 |
+
value="*Translation will appear here ...*\n\n",
|
| 205 |
+
show_copy_button=True
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
translation_button.click(
|
| 209 |
+
fn=translation_workflow,
|
| 210 |
+
inputs=[text_display, target_language_selector],
|
| 211 |
+
outputs=interpretation_output
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
|
| 216 |
if __name__ == "__main__":
|
| 217 |
+
demo.launch()
|
process/interpretation.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from google import genai
|
| 2 |
from google.genai import types
|
| 3 |
-
from process.sys_prompt import GENERAL_PROMPT
|
| 4 |
|
| 5 |
|
| 6 |
NARRATIVE_PROMPT = ""
|
|
@@ -8,10 +8,10 @@ POEM_PROMPT = ""
|
|
| 8 |
PHILO_PROMPT = ""
|
| 9 |
|
| 10 |
def get_interpretation(genre: str,
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
if not api_key:
|
| 17 |
return "Error: Gemini API Key not found."
|
|
@@ -20,7 +20,7 @@ def get_interpretation(genre: str,
|
|
| 20 |
|
| 21 |
client = genai.Client(api_key=api_key)
|
| 22 |
|
| 23 |
-
lang_map ={"
|
| 24 |
learn_lang = lang_map.get(learn_language.upper(), "English")
|
| 25 |
prof_lang = lang_map.get(prof_language.upper(), "English")
|
| 26 |
genres = {
|
|
@@ -30,10 +30,8 @@ def get_interpretation(genre: str,
|
|
| 30 |
"poem": POEM_PROMPT,
|
| 31 |
"philosophy": PHILO_PROMPT
|
| 32 |
}
|
| 33 |
-
if genre.lower()
|
| 34 |
-
sys_prompt = genres[
|
| 35 |
-
elif genre.lower() == "news":
|
| 36 |
-
sys_prompt = genres["news"].replace("[LEARN_LANGUAGE]", learn_lang).replace("[PROF_LANGUAGE]", prof_lang)
|
| 37 |
|
| 38 |
response = client.models.generate_content(
|
| 39 |
model="gemini-2.5-flash-preview-05-20",
|
|
|
|
| 1 |
from google import genai
|
| 2 |
from google.genai import types
|
| 3 |
+
from process.sys_prompt import GENERAL_PROMPT, NEWS_PROMPT
|
| 4 |
|
| 5 |
|
| 6 |
NARRATIVE_PROMPT = ""
|
|
|
|
| 8 |
PHILO_PROMPT = ""
|
| 9 |
|
| 10 |
def get_interpretation(genre: str,
|
| 11 |
+
api_key: str,
|
| 12 |
+
text: str,
|
| 13 |
+
learn_language: str,
|
| 14 |
+
prof_language: str) -> str:
|
| 15 |
|
| 16 |
if not api_key:
|
| 17 |
return "Error: Gemini API Key not found."
|
|
|
|
| 20 |
|
| 21 |
client = genai.Client(api_key=api_key)
|
| 22 |
|
| 23 |
+
lang_map ={"DE": "German", "EN": "English", "FR": "French", "RU":"Russian", "ZH": "Chinese"}
|
| 24 |
learn_lang = lang_map.get(learn_language.upper(), "English")
|
| 25 |
prof_lang = lang_map.get(prof_language.upper(), "English")
|
| 26 |
genres = {
|
|
|
|
| 30 |
"poem": POEM_PROMPT,
|
| 31 |
"philosophy": PHILO_PROMPT
|
| 32 |
}
|
| 33 |
+
if genre.lower() in ["general", "news"]:
|
| 34 |
+
sys_prompt = genres[genre.lower()].replace("[LEARN_LANGUAGE]", learn_lang).replace("[PROF_LANGUAGE]", prof_lang)
|
|
|
|
|
|
|
| 35 |
|
| 36 |
response = client.models.generate_content(
|
| 37 |
model="gemini-2.5-flash-preview-05-20",
|
process/ocr.py
CHANGED
|
@@ -86,7 +86,10 @@ def correct_text_with_ai(text: str, api_key: str):
|
|
| 86 |
|
| 87 |
|
| 88 |
def perform_raw_ocr(input_file, api_key):
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
if file_ext == "txt":
|
| 92 |
with open(input_file, "r", encoding="utf-8") as f:
|
|
|
|
| 86 |
|
| 87 |
|
| 88 |
def perform_raw_ocr(input_file, api_key):
|
| 89 |
+
if input_file != None:
|
| 90 |
+
file_ext = input_file.name.split('.')[-1].lower()
|
| 91 |
+
else:
|
| 92 |
+
return "File/Text not found"
|
| 93 |
|
| 94 |
if file_ext == "txt":
|
| 95 |
with open(input_file, "r", encoding="utf-8") as f:
|
process/translation.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from google import genai
|
| 2 |
+
from google.genai import types
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
SYS_PROMPT_TRANSLATION = """
|
| 6 |
+
You are an expert translator.
|
| 7 |
+
Your sole purpose is to accurately and faithfully translate the provided text into the [TARGET_LANGUAGE].
|
| 8 |
+
Do not add any extra information, explanations, or stylistic changes.
|
| 9 |
+
Maintain the original meaning and tone as closely as possible.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
def get_tranlaton(text: str, api_key: str, target_language: str) -> str:
|
| 13 |
+
|
| 14 |
+
if not api_key:
|
| 15 |
+
return "Error: Gemini API Key not found."
|
| 16 |
+
if not text:
|
| 17 |
+
return "Error: text not found."
|
| 18 |
+
|
| 19 |
+
client = genai.Client(api_key=api_key)
|
| 20 |
+
|
| 21 |
+
lang_map = {"Deutsch": "German", "English": "English", "Français": "French", "Русский язык": "Russain", "中文": "Chinese"}
|
| 22 |
+
tar_lang = lang_map.get(target_language, "English")
|
| 23 |
+
sys_prompt = SYS_PROMPT_TRANSLATION.replace("[TARGET_LANGUAGE]", tar_lang)
|
| 24 |
+
response = client.models.generate_content(
|
| 25 |
+
model="gemini-2.5-flash-preview-05-20",
|
| 26 |
+
config=types.GenerateContentConfig(
|
| 27 |
+
system_instruction=sys_prompt,
|
| 28 |
+
temperature=0.1,
|
| 29 |
+
),
|
| 30 |
+
contents=[text]
|
| 31 |
+
)
|
| 32 |
+
return response.text
|