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