ashishc628 commited on
Commit
e7f5bed
·
verified ·
1 Parent(s): d5219c3

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +137 -0
main.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+ import os
4
+ from dotenv import load_dotenv
5
+ import PyPDF2
6
+ import tempfile
7
+
8
+ # Load API Key
9
+ load_dotenv()
10
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
11
+
12
+ # -------------------------------
13
+ # Translate text using OpenAI
14
+ # -------------------------------
15
+ def translate_text(input_text, source_lang, target_lang):
16
+ if not input_text.strip():
17
+ return "⚠️ Please enter text to translate."
18
+
19
+ if source_lang == target_lang:
20
+ return "⚠️ Source and target languages must be different."
21
+
22
+ prompt = f"Translate the following text from {source_lang} to {target_lang}:\n\n{input_text}"
23
+
24
+ try:
25
+ resp = client.responses.create(
26
+ model="gpt-4.1-mini",
27
+ input=prompt
28
+ )
29
+ translation = resp.output[0].content[0].text
30
+ return translation
31
+ except Exception as e:
32
+ return f"❌ Error: {str(e)}"
33
+
34
+
35
+ # -------------------------------
36
+ # Process PDF → extract → translate
37
+ # -------------------------------
38
+ def translate_pdf(pdf_file, source_lang, target_lang):
39
+ if pdf_file is None:
40
+ return "⚠️ Please upload a PDF.", None
41
+
42
+ if source_lang == target_lang:
43
+ return "⚠️ Source and target languages must be different.", None
44
+
45
+ try:
46
+ # Extract text from PDF
47
+ reader = PyPDF2.PdfReader(pdf_file.name)
48
+ text = ""
49
+ for page in reader.pages:
50
+ text += page.extract_text() + "\n"
51
+
52
+ # Translate using OpenAI
53
+ prompt = f"Translate the following PDF text from {source_lang} to {target_lang}:\n\n{text[:12000]}"
54
+
55
+ resp = client.responses.create(
56
+ model="gpt-4.1-mini",
57
+ input=prompt
58
+ )
59
+ translated_text = resp.output[0].content[0].text
60
+
61
+ # Save translated file
62
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
63
+ with open(temp_file.name, "w", encoding="utf-8") as f:
64
+ f.write(translated_text)
65
+
66
+ return translated_text, temp_file.name
67
+
68
+ except Exception as e:
69
+ return f"❌ Error processing PDF: {str(e)}", None
70
+
71
+
72
+ # -------------------------------
73
+ # GRADIO UI
74
+ # -------------------------------
75
+ language_list = ["English", "Spanish", "French", "German", "Hindi", "Chinese", "Japanese"]
76
+
77
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="indigo")) as demo:
78
+ gr.Markdown(
79
+ """
80
+ # **AI Translation App**
81
+ Translate text or full PDF documents into any language using OpenAI.
82
+ """
83
+ )
84
+
85
+ with gr.Tabs():
86
+ # ------------------ TEXT TRANSLATOR ------------------
87
+ with gr.Tab("✍️ Text Translator"):
88
+ with gr.Row():
89
+ source_lang = gr.Dropdown(language_list, label="Source Language", value="English")
90
+ target_lang = gr.Dropdown(language_list, label="Target Language", value="Spanish")
91
+
92
+ input_box = gr.Textbox(
93
+ label="Enter text to translate",
94
+ placeholder="Type or paste your text here...",
95
+ lines=6
96
+ )
97
+
98
+ translate_btn = gr.Button("Translate", variant="primary")
99
+
100
+ output_box = gr.Textbox(
101
+ label="Translated Text",
102
+ lines=6
103
+ )
104
+
105
+ translate_btn.click(
106
+ translate_text,
107
+ inputs=[input_box, source_lang, target_lang],
108
+ outputs=output_box
109
+ )
110
+
111
+ # ------------------ PDF TRANSLATOR ------------------
112
+ with gr.Tab("📄 PDF Translator"):
113
+ with gr.Row():
114
+ source_lang_pdf = gr.Dropdown(language_list, label="Source Language", value="English")
115
+ target_lang_pdf = gr.Dropdown(language_list, label="Target Language", value="French")
116
+
117
+ pdf_file = gr.File(label="Upload PDF", file_types=[".pdf"])
118
+
119
+ translate_pdf_btn = gr.Button("Translate PDF", variant="primary")
120
+
121
+ pdf_output = gr.Textbox(label="Translated PDF Text", lines=12)
122
+ download_btn = gr.File(label="Download translated file")
123
+
124
+ def pdf_translate_wrapper(pdf, src, tgt):
125
+ text, file_path = translate_pdf(pdf, src, tgt)
126
+ return text, file_path
127
+
128
+ translate_pdf_btn.click(
129
+ pdf_translate_wrapper,
130
+ inputs=[pdf_file, source_lang_pdf, target_lang_pdf],
131
+ outputs=[pdf_output, download_btn]
132
+ )
133
+
134
+
135
+
136
+ # Run the app
137
+ demo.launch()