File size: 7,128 Bytes
c8befe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb3f0ad
2e05851
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8befe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9dfdc30
 
 
 
563d09c
c8befe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
563d09c
 
 
 
c8befe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

import os
import gradio as gr
import pdfplumber
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

LANGUAGES = ["English", "Turkish", "Arabic", "French", "German", "Spanish"]

TRANSLATIONS = {
    "English": {
        "summary_lang": "🗣️ Summary Language",
        "char_limit": "🔢 Character Limit (approximate)",
        "text_input": "📥 Paste Text Here",
        "text_placeholder": "Or paste a document here...",
        "pdf_upload": "📄 Or Upload a PDF File",
        "output": "🧠 Output (Summary, Title, Keywords)",
        "run_button": "Summarize"
    },
    "Turkish": {
        "summary_lang": "🗣️ Özetleme Dili",
        "char_limit": "🔢 Karakter Sınırı (yaklaşık)",
        "text_input": "📥 Metni Buraya Yapıştırın",
        "text_placeholder": "Ya da bir belge yapıştırın...",
        "pdf_upload": "📄 Ya da bir PDF Yükleyin",
        "output": "🧠 Çıktı (Özet, Başlık, Anahtar Kelimeler)",
        "run_button": "Özetle"
    },
    "French": {
        "summary_lang": "🗣️ Langue du Résumé",
        "char_limit": "🔢 Limite de caractères (approximative)",
        "text_input": "📥 Collez le texte ici",
        "text_placeholder": "Ou collez un document ici...",
        "pdf_upload": "📄 Ou téléchargez un fichier PDF",
        "output": "🧠 Résultat (Résumé, Titre, Mots-clés)",
        "run_button": "Résumer"
    },
    "German": {
        "summary_lang": "🗣️ Zusammenfassungs-Sprache",
        "char_limit": "🔢 Zeichenbegrenzung (ungefähr)",
        "text_input": "📥 Text hier einfügen",
        "text_placeholder": "Oder fügen Sie hier ein Dokument ein...",
        "pdf_upload": "📄 Oder laden Sie eine PDF-Datei hoch",
        "output": "🧠 Ausgabe (Zusammenfassung, Titel, Schlüsselwörter)",
        "run_button": "Zusammenfassen"
    },
    "Spanish": {
        "summary_lang": "🗣️ Idioma del Resumen",
        "char_limit": "🔢 Límite de caracteres (aproximado)",
        "text_input": "📥 Pega el texto aquí",
        "text_placeholder": "O pega un documento aquí...",
        "pdf_upload": "📄 O sube un archivo PDF",
        "output": "🧠 Resultado (Resumen, Título, Palabras clave)",
        "run_button": "Resumir"
    },
    "Arabic": {
        "summary_lang": "🗣️ لغة الملخص",
        "char_limit": "🔢 الحد التقريبي لعدد الأحرف",
        "text_input": "📥 الصق النص هنا",
        "text_placeholder": "أو الصق مستندًا هنا...",
        "pdf_upload": "📄 أو قم بتحميل ملف PDF",
        "output": "🧠 النتيجة (الملخص، العنوان، الكلمات المفتاحية)",
        "run_button": "تلخيص"
    }
}

def extract_text_from_pdf(file):
    try:
        with pdfplumber.open(file.name) as pdf:
            text = ""
            for page in pdf.pages:
                page_text = page.extract_text()
                if page_text:
                    text += page_text + "\n"
        if not text.strip():
            return None, "The PDF appears to contain no extractable text."
        return text.strip(), None
    except Exception as e:
        return None, f"PDF reading error: {str(e)}"

def translate_text(text, target_lang):
    try:
        prompt = f"Translate the following text to {target_lang}:\n\n{text}"
        response = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[
                {"role": "system", "content": "You are a helpful translator."},
                {"role": "user", "content": prompt}
            ],
            temperature=0.3,
            max_tokens=3000
        )
        return response.choices[0].message.content
    except Exception:
        return text

def analyze_text(text, summary_lang, char_limit):
    translated_text = translate_text(text, summary_lang)
    prompt = f"""You are a helpful assistant.

Please provide the following in {summary_lang}:
1. A clear and concise summary (limited to approximately {char_limit} characters).
2. A suitable title.
3. 5 relevant keywords.

Document:
{translated_text[:3000]}"""
    try:
        response = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": prompt}
            ],
            temperature=0.7,
            max_tokens=400
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"❌ OpenAI Error: {str(e)}"

def analyze_input(summary_lang, char_limit, text_input, pdf_file):
    text = ""
    error = None
    if pdf_file:
        text, error = extract_text_from_pdf(pdf_file)
    elif text_input and text_input.strip():
        text = text_input.strip()

    if error:
        return f"⚠️ PDF Error: {error}"
    if not text:
        return "⚠️ No text provided or extracted."

    return analyze_text(text, summary_lang, char_limit)

def interface_selector(interface_lang):
    t = TRANSLATIONS.get(interface_lang, TRANSLATIONS["English"])
    return (
        gr.update(visible=True),
        gr.update(label=t["summary_lang"]),
        gr.update(label=t["char_limit"]),
        gr.update(label=t["text_input"], placeholder=t["text_placeholder"]),
        gr.update(label=t["pdf_upload"]),
        gr.update(label=t["output"]),
        gr.update(value=t["run_button"])
    )

with gr.Blocks(css="""
.small-textbox textarea {height: 60px !important;}
.small-file-upload {max-height: 40px !important;}
.small-output textarea {height: 90px !important;}
""") as demo: 
    gr.Markdown("## 🌐 Select Interface Language")

    with gr.Accordion("📘 View README / Usage Guide", open=False):
        gr.Markdown("""This application allows you to upload a PDF or paste text, select your preferred summary language, and receive:
- A clear summary ✂️  
- An auto-generated title 🏷️  
- 5 relevant keywords 🔑  
If the content language and summary language differ, the app will auto-translate before summarizing 🌐  
Powered by OpenAI GPT-3.5 and Gradio.""")

    lang_select = gr.Dropdown(label="Interface Language", choices=LANGUAGES, value="English")
    next_btn = gr.Button("Continue")

    with gr.Column(visible=False) as summary_section:
        summary_lang = gr.Dropdown(choices=LANGUAGES, value="English")
        char_limit = gr.Textbox(value="300", elem_classes="small-textbox")
        text_input = gr.Textbox(lines=3, max_lines=5, elem_classes="small-textbox")
        pdf_file = gr.File(elem_classes="small-file-upload")
        output = gr.Textbox(lines=4, elem_classes="small-output")
        run_btn = gr.Button()

    next_btn.click(fn=interface_selector, inputs=[lang_select], outputs=[
        summary_section,
        summary_lang,
        char_limit,
        text_input,
        pdf_file,
        output,
        run_btn
    ])

    run_btn.click(fn=analyze_input, inputs=[summary_lang, char_limit, text_input, pdf_file], outputs=output)

demo.launch()