Talip7 commited on
Commit
f476279
·
verified ·
1 Parent(s): 039f72a

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -187
app.py DELETED
@@ -1,187 +0,0 @@
1
- import gradio as gr
2
- import pdfplumber
3
- from openai import OpenAI
4
- import os
5
-
6
- client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
7
-
8
-
9
- LANGUAGES = ["English", "Turkish", "Arabic", "French", "German", "Spanish"]
10
-
11
- TRANSLATIONS = {
12
- "English": {
13
- "summary_lang": "🗣️ Summary Language",
14
- "char_limit": "🔢 Character Limit (approximate)",
15
- "text_input": "📥 Paste Text Here",
16
- "text_placeholder": "Or paste a document here...",
17
- "pdf_upload": "📄 Or Upload a PDF File",
18
- "output": "🧠 Output (Summary, Title, Keywords)",
19
- "run_button": "Summarize"
20
- },
21
- "Turkish": {
22
- "summary_lang": "🗣️ Özetleme Dili",
23
- "char_limit": "🔢 Karakter Sınırı (yaklaşık)",
24
- "text_input": "📥 Metni Buraya Yapıştırın",
25
- "text_placeholder": "Ya da bir belge yapıştırın...",
26
- "pdf_upload": "📄 Ya da bir PDF Yükleyin",
27
- "output": "🧠 Çıktı (Özet, Başlık, Anahtar Kelimeler)",
28
- "run_button": "Özetle"
29
- },
30
- "French": {
31
- "summary_lang": "🗣️ Langue du Résumé",
32
- "char_limit": "🔢 Limite de caractères (approximative)",
33
- "text_input": "📥 Collez le texte ici",
34
- "text_placeholder": "Ou collez un document ici...",
35
- "pdf_upload": "📄 Ou téléchargez un fichier PDF",
36
- "output": "🧠 Résultat (Résumé, Titre, Mots-clés)",
37
- "run_button": "Résumer"
38
- },
39
- "German": {
40
- "summary_lang": "🗣️ Zusammenfassungs-Sprache",
41
- "char_limit": "🔢 Zeichenbegrenzung (ungefähr)",
42
- "text_input": "📥 Text hier einfügen",
43
- "text_placeholder": "Oder fügen Sie hier ein Dokument ein...",
44
- "pdf_upload": "📄 Oder laden Sie eine PDF-Datei hoch",
45
- "output": "🧠 Ausgabe (Zusammenfassung, Titel, Schlüsselwörter)",
46
- "run_button": "Zusammenfassen"
47
- },
48
- "Spanish": {
49
- "summary_lang": "🗣️ Idioma del Resumen",
50
- "char_limit": "🔢 Límite de caracteres (aproximado)",
51
- "text_input": "📥 Pega el texto aquí",
52
- "text_placeholder": "O pega un documento aquí...",
53
- "pdf_upload": "📄 O sube un archivo PDF",
54
- "output": "🧠 Resultado (Resumen, Título, Palabras clave)",
55
- "run_button": "Resumir"
56
- },
57
- "Arabic": {
58
- "summary_lang": "🗣️ لغة الملخص",
59
- "char_limit": "🔢 الحد التقريبي لعدد الأحرف",
60
- "text_input": "📥 الصق النص هنا",
61
- "text_placeholder": "أو الصق مستندًا هنا...",
62
- "pdf_upload": "📄 أو قم بتحميل ملف PDF",
63
- "output": "🧠 النتيجة (الملخص، العنوان، الكلمات المفتاحية)",
64
- "run_button": "تلخيص"
65
- }
66
- }
67
-
68
- def extract_text_from_pdf(file):
69
- try:
70
- with pdfplumber.open(file.name) as pdf:
71
- text = ""
72
- for page in pdf.pages:
73
- page_text = page.extract_text()
74
- if page_text:
75
- text += page_text + "\n"
76
- if not text.strip():
77
- return None, "The PDF appears to contain no extractable text. It might be an image-based PDF or encoded improperly."
78
- return text.strip(), None
79
- except Exception as e:
80
- return None, f"PDF reading error: {str(e)}"
81
-
82
- def translate_text(text, target_lang):
83
- try:
84
- prompt = f"Translate the following text to {target_lang}:\n\n{text}"
85
- response = client.chat.completions.create(
86
- model="gpt-3.5-turbo",
87
- messages=[
88
- {"role": "system", "content": "You are a helpful translator."},
89
- {"role": "user", "content": prompt}
90
- ],
91
- temperature=0.3,
92
- max_tokens=3000
93
- )
94
- return response.choices[0].message.content
95
- except Exception:
96
- return text
97
-
98
- def analyze_text(text, summary_lang, char_limit):
99
- translated_text = translate_text(text, summary_lang)
100
- prompt = f"""You are a helpful assistant.
101
-
102
- Please provide the following in {summary_lang}:
103
- 1. A clear and concise summary (limited to approximately {char_limit} characters).
104
- 2. A suitable title.
105
- 3. 5 relevant keywords.
106
-
107
- Document:
108
- {translated_text[:3000]}"""
109
- try:
110
- response = client.chat.completions.create(
111
- model="gpt-3.5-turbo",
112
- messages=[
113
- {"role": "system", "content": "You are a helpful assistant."},
114
- {"role": "user", "content": prompt}
115
- ],
116
- temperature=0.7,
117
- max_tokens=400
118
- )
119
- return response.choices[0].message.content
120
- except Exception as e:
121
- return f"❌ OpenAI Error: {str(e)}"
122
-
123
- def analyze_input(summary_lang, char_limit, text_input, pdf_file):
124
- text = ""
125
- error = None
126
- if pdf_file:
127
- text, error = extract_text_from_pdf(pdf_file)
128
- elif text_input and text_input.strip():
129
- text = text_input.strip()
130
-
131
- if error:
132
- return f"⚠️ PDF Error: {error}"
133
- if not text:
134
- return "⚠️ No text provided or extracted."
135
-
136
- return analyze_text(text, summary_lang, char_limit)
137
-
138
- def interface_selector(interface_lang):
139
- t = TRANSLATIONS.get(interface_lang, TRANSLATIONS["English"])
140
- return (
141
- gr.update(visible=True),
142
- gr.update(label=t["summary_lang"]),
143
- gr.update(label=t["char_limit"]),
144
- gr.update(label=t["text_input"], placeholder=t["text_placeholder"]),
145
- gr.update(label=t["pdf_upload"]),
146
- gr.update(label=t["output"]),
147
- gr.update(value=t["run_button"])
148
- )
149
-
150
- with gr.Blocks() as demo:
151
- gr.Markdown("## 🌐 Select Interface Language")
152
- with gr.Accordion("📘 View README / Usage Guide", open=False):
153
- gr.Markdown("""
154
- This application allows you to upload a PDF or paste text, select your preferred summary language, and receive:
155
-
156
- - A clear summary ✂️
157
- - An auto-generated title 🏷️
158
- - 5 relevant keywords 🔑
159
-
160
- If the content language and summary language differ, the app will auto-translate before summarizing 🌐
161
-
162
- Powered by OpenAI GPT-3.5 and Gradio.
163
- """)
164
- lang_select = gr.Dropdown(label="Interface Language", choices=LANGUAGES, value="English")
165
- next_btn = gr.Button("Continue")
166
-
167
- with gr.Column(visible=False) as summary_section:
168
- summary_lang = gr.Dropdown(choices=LANGUAGES, value="English")
169
- char_limit = gr.Textbox(value="300")
170
- text_input = gr.Textbox(lines=10)
171
- pdf_file = gr.File()
172
- output = gr.Textbox()
173
- run_btn = gr.Button()
174
-
175
- next_btn.click(fn=interface_selector, inputs=[lang_select], outputs=[
176
- summary_section,
177
- summary_lang,
178
- char_limit,
179
- text_input,
180
- pdf_file,
181
- output,
182
- run_btn
183
- ])
184
-
185
- run_btn.click(fn=analyze_input, inputs=[summary_lang, char_limit, text_input, pdf_file], outputs=output)
186
-
187
- demo.launch()