akarshan11 commited on
Commit
8aae362
·
verified ·
1 Parent(s): 798c21f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  import os
3
  import tempfile
4
- from transformers import pipeline
5
  from deep_translator import GoogleTranslator
6
  from langdetect import detect
7
  from fpdf import FPDF
@@ -35,7 +34,6 @@ def translate_text(text, target_language):
35
 
36
  source_lang = detect_language(text)
37
 
38
- # Skip translation if source and target are the same
39
  if source_lang == LANGUAGES[target_language]:
40
  return text
41
 
@@ -50,8 +48,7 @@ def extract_text_from_document(file):
50
  if not file:
51
  return ""
52
 
53
- # Since file is now a Gradio file object, use its name attribute
54
- file_path = file.name
55
  _, file_extension = os.path.splitext(file_path)
56
 
57
  if file_extension.lower() == '.txt':
@@ -94,16 +91,15 @@ def translate_and_save(input_text, input_file, target_language):
94
 
95
  return pdf_path, translated_text
96
 
97
- # Create Gradio interface
98
  with gr.Blocks(title="Context-Aware Translation Tool") as demo:
99
  gr.Markdown("# Context-Aware Language Translation")
100
  gr.Markdown("This tool translates text while preserving context, idioms, and phrases.")
101
 
102
  with gr.Row():
103
  with gr.Column():
104
- input_text = gr.Textbox(label="Enter text to translate", lines=5)
105
- # Changed type="filepath" to default behavior
106
- input_file = gr.File(label="Or upload a document (.txt, .docx)")
107
  target_language = gr.Dropdown(
108
  label="Target Language",
109
  choices=list(LANGUAGES.keys()),
@@ -112,14 +108,25 @@ with gr.Blocks(title="Context-Aware Translation Tool") as demo:
112
  translate_button = gr.Button("Translate")
113
 
114
  with gr.Column():
115
- output_text = gr.Textbox(label="Translation", lines=5)
116
  output_pdf = gr.File(label="Download as PDF")
117
-
 
118
  translate_button.click(
119
  fn=translate_and_save,
120
- inputs=[input_text, input_file, target_language],
121
- outputs=[output_pdf, output_text]
 
 
 
 
 
 
 
 
122
  )
123
 
124
  if __name__ == "__main__":
 
 
125
  demo.launch(show_error=True)
 
1
  import gradio as gr
2
  import os
3
  import tempfile
 
4
  from deep_translator import GoogleTranslator
5
  from langdetect import detect
6
  from fpdf import FPDF
 
34
 
35
  source_lang = detect_language(text)
36
 
 
37
  if source_lang == LANGUAGES[target_language]:
38
  return text
39
 
 
48
  if not file:
49
  return ""
50
 
51
+ file_path = file.name # Gradio file object provides a 'name' attribute
 
52
  _, file_extension = os.path.splitext(file_path)
53
 
54
  if file_extension.lower() == '.txt':
 
91
 
92
  return pdf_path, translated_text
93
 
94
+ # Create Gradio interface with explicit types
95
  with gr.Blocks(title="Context-Aware Translation Tool") as demo:
96
  gr.Markdown("# Context-Aware Language Translation")
97
  gr.Markdown("This tool translates text while preserving context, idioms, and phrases.")
98
 
99
  with gr.Row():
100
  with gr.Column():
101
+ input_text = gr.Textbox(label="Enter text to translate", lines=5, placeholder="Type your text here")
102
+ input_file = gr.File(label="Or upload a document (.txt, .docx)", file_types=[".txt", ".docx"])
 
103
  target_language = gr.Dropdown(
104
  label="Target Language",
105
  choices=list(LANGUAGES.keys()),
 
108
  translate_button = gr.Button("Translate")
109
 
110
  with gr.Column():
111
+ output_text = gr.Textbox(label="Translation", lines=5, interactive=False)
112
  output_pdf = gr.File(label="Download as PDF")
113
+
114
+ # Define the event with explicit types
115
  translate_button.click(
116
  fn=translate_and_save,
117
+ inputs=[
118
+ input_text,
119
+ input_file,
120
+ target_language
121
+ ],
122
+ outputs=[
123
+ output_pdf,
124
+ output_text
125
+ ],
126
+ _js=None # Avoid any JS overrides that might affect schema
127
  )
128
 
129
  if __name__ == "__main__":
130
+ # Print Gradio version for debugging
131
+ print(f"Gradio version: {gr.__version__}")
132
  demo.launch(show_error=True)