IvanMiao commited on
Commit
01dc73d
·
1 Parent(s): c267590

fix: typo, error handle

Browse files
Files changed (4) hide show
  1. app.py +6 -3
  2. process/interpretation.py +4 -1
  3. process/ocr.py +14 -5
  4. process/translation.py +5 -2
app.py CHANGED
@@ -1,7 +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.translation import get_tranlaton
5
  from process.gradio_css import CUSTOM_CSS
6
 
7
 
@@ -78,7 +78,7 @@ def ai_correct(current_text: str, mistral_key: str):
78
  yield error_msg, error_msg
79
  return
80
 
81
- yield "⏳ AI Correcting text...", "⏳ AI Correcting text...\n\n*Please wait...*"
82
  try:
83
  result = correct_text_with_ai(current_text, mistral_key)
84
  yield result, result
@@ -141,7 +141,7 @@ def translation_workflow(text: str, target_language: str, gemini_key):
141
 
142
  if target_language in ["Deutsch", "English", "Français", "Русский язык", "中文"]:
143
  yield f"⏳ Generating interpretation for target_language: {[target_language]} ..."
144
- result = get_tranlaton(text, gemini_key, target_language)
145
  yield result
146
  else:
147
  yield "not implemented yet"
@@ -206,6 +206,7 @@ with gr.Blocks(theme=gr.themes.Monochrome(), css=CUSTOM_CSS) as demo:
206
  with gr.Tab("Formatted Text"):
207
  text_markdown = gr.Markdown(
208
  value="*Processed text will appear here...*\n\n",
 
209
  )
210
 
211
  # Hook the ocr button to click event
@@ -237,6 +238,7 @@ with gr.Blocks(theme=gr.themes.Monochrome(), css=CUSTOM_CSS) as demo:
237
  gr.Markdown("### COURSE")
238
  interpretation_output = gr.Markdown(
239
  value="*Interpretation will appear here after processing...*\n\n",
 
240
  show_copy_button=True
241
  )
242
 
@@ -260,6 +262,7 @@ with gr.Blocks(theme=gr.themes.Monochrome(), css=CUSTOM_CSS) as demo:
260
  with gr.Column(scale=2):
261
  interpretation_output = gr.Markdown(
262
  value="*Translation will appear here ...*\n\n",
 
263
  show_copy_button=True
264
  )
265
 
 
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_translaton
5
  from process.gradio_css import CUSTOM_CSS
6
 
7
 
 
78
  yield error_msg, error_msg
79
  return
80
 
81
+ yield "⏳ AI Correcting text...", "⏳ AI Correcting text...\n\n"
82
  try:
83
  result = correct_text_with_ai(current_text, mistral_key)
84
  yield result, result
 
141
 
142
  if target_language in ["Deutsch", "English", "Français", "Русский язык", "中文"]:
143
  yield f"⏳ Generating interpretation for target_language: {[target_language]} ..."
144
+ result = get_translaton(text, gemini_key, target_language)
145
  yield result
146
  else:
147
  yield "not implemented yet"
 
206
  with gr.Tab("Formatted Text"):
207
  text_markdown = gr.Markdown(
208
  value="*Processed text will appear here...*\n\n",
209
+ label="Formatted Text"
210
  )
211
 
212
  # Hook the ocr button to click event
 
238
  gr.Markdown("### COURSE")
239
  interpretation_output = gr.Markdown(
240
  value="*Interpretation will appear here after processing...*\n\n",
241
+ label="Interpretation Result",
242
  show_copy_button=True
243
  )
244
 
 
262
  with gr.Column(scale=2):
263
  interpretation_output = gr.Markdown(
264
  value="*Translation will appear here ...*\n\n",
265
+ label="Translation Result",
266
  show_copy_button=True
267
  )
268
 
process/interpretation.py CHANGED
@@ -18,7 +18,10 @@ def get_interpretation(genre: str,
18
  if not text:
19
  return "Error: text 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")
 
18
  if not text:
19
  return "Error: text not found."
20
 
21
+ try:
22
+ client = genai.Client(api_key=api_key)
23
+ except Exception as e:
24
+ return f"ERROR: {str(e)}"
25
 
26
  lang_map ={"DE": "German", "EN": "English", "FR": "French", "RU":"Russian", "ZH": "Chinese"}
27
  learn_lang = lang_map.get(learn_language.upper(), "English")
process/ocr.py CHANGED
@@ -6,11 +6,16 @@ OCR_MODEL = "mistral-ocr-latest"
6
  CHAT_MODEL = "mistral-large-latest"
7
 
8
 
9
- def ocr_from_file(file_path, api_key, mode="image"):
10
 
11
  if not api_key:
12
  raise ValueError("Mistral API Key is required.")
13
- client = Mistral(api_key=api_key)
 
 
 
 
 
14
  uploaded_image = client.files.upload(
15
  file={
16
  "file_name": file_path,
@@ -51,11 +56,15 @@ def get_combined_markdown(ocr_response: OCRResponse) -> str:
51
  return "\n\n".join(markdowns)
52
 
53
 
54
- def correct_text_with_ai(text: str, api_key: str):
55
 
56
  if not api_key:
57
  raise ValueError("Mistral API Key is required.")
58
- client = Mistral(api_key=api_key)
 
 
 
 
59
 
60
  response = client.chat.complete(
61
  model=CHAT_MODEL,
@@ -85,7 +94,7 @@ def correct_text_with_ai(text: str, api_key: str):
85
  return(response.choices[0].message.content)
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:
 
6
  CHAT_MODEL = "mistral-large-latest"
7
 
8
 
9
+ def ocr_from_file(file_path, api_key: str, mode="image"):
10
 
11
  if not api_key:
12
  raise ValueError("Mistral API Key is required.")
13
+
14
+ try:
15
+ client = Mistral(api_key=api_key)
16
+ except Exception as e:
17
+ raise ValueError("API invalid.")
18
+
19
  uploaded_image = client.files.upload(
20
  file={
21
  "file_name": file_path,
 
56
  return "\n\n".join(markdowns)
57
 
58
 
59
+ def correct_text_with_ai(text: str, api_key: str) -> str:
60
 
61
  if not api_key:
62
  raise ValueError("Mistral API Key is required.")
63
+
64
+ try:
65
+ client = Mistral(api_key=api_key)
66
+ except Exception as e:
67
+ return f"ERROR: {str(e)}"
68
 
69
  response = client.chat.complete(
70
  model=CHAT_MODEL,
 
94
  return(response.choices[0].message.content)
95
 
96
 
97
+ def perform_raw_ocr(input_file, api_key: str):
98
  if input_file != None:
99
  file_ext = input_file.name.split('.')[-1].lower()
100
  else:
process/translation.py CHANGED
@@ -9,14 +9,17 @@ 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")
 
9
  Maintain the original meaning and tone as closely as possible.
10
  """
11
 
12
+ def get_translaton(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
+ try:
20
+ client = genai.Client(api_key=api_key)
21
+ except Exception as e:
22
+ return f"ERROR: {str(e)}"
23
 
24
  lang_map = {"Deutsch": "German", "English": "English", "Français": "French", "Русский язык": "Russain", "中文": "Chinese"}
25
  tar_lang = lang_map.get(target_language, "English")