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
|
| 7 |
+
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
|
| 8 |
+
|
| 9 |
+
SUMMARY_STYLES = ["Child-Friendly", "Academic", "Tweet"]
|
| 10 |
+
LANGUAGES = ["English", "Turkish", "French", "German", "Spanish"]
|
| 11 |
+
|
| 12 |
+
PROMPT_TEMPLATES = {
|
| 13 |
+
"English": """
|
| 14 |
+
Please summarize the following text in a {style} style and keep it within approximately {char_limit} characters.
|
| 15 |
+
Then provide 5 relevant keywords.
|
| 16 |
+
|
| 17 |
+
TEXT:
|
| 18 |
+
{text}
|
| 19 |
+
""",
|
| 20 |
+
"Turkish": """
|
| 21 |
+
Lütfen aşağıdaki metni {style} tarzında ve yaklaşık {char_limit} karakter olacak şekilde özetle.
|
| 22 |
+
Ardından 5 anahtar kelime ver.
|
| 23 |
+
|
| 24 |
+
METİN:
|
| 25 |
+
{text}
|
| 26 |
+
""",
|
| 27 |
+
"French": """
|
| 28 |
+
Veuillez résumer le texte suivant dans un style {style}, en environ {char_limit} caractères.
|
| 29 |
+
Fournissez ensuite 5 mots-clés pertinents.
|
| 30 |
+
|
| 31 |
+
TEXTE :
|
| 32 |
+
{text}
|
| 33 |
+
""",
|
| 34 |
+
"German": """
|
| 35 |
+
Fassen Sie den folgenden Text im {style}-Stil mit etwa {char_limit} Zeichen zusammen.
|
| 36 |
+
Geben Sie anschließend 5 relevante Schlüsselwörter an.
|
| 37 |
+
|
| 38 |
+
TEXT:
|
| 39 |
+
{text}
|
| 40 |
+
""",
|
| 41 |
+
"Spanish": """
|
| 42 |
+
Resume el siguiente texto en un estilo {style} y con un límite de aproximadamente {char_limit} caracteres.
|
| 43 |
+
Luego proporciona 5 palabras clave relevantes.
|
| 44 |
+
|
| 45 |
+
TEXTO:
|
| 46 |
+
{text}
|
| 47 |
+
"""
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
QUIZ_PROMPTS = {
|
| 51 |
+
"English": "Based on the text below, generate 2 multiple choice questions (each with 4 options A-D):\n\n{text}",
|
| 52 |
+
"Turkish": "Aşağıdaki metne dayanarak, 4 seçenekli (A, B, C, D) 2 adet çoktan seçmeli soru oluştur:\n\n{text}",
|
| 53 |
+
"French": "Sur la base du texte ci-dessous, générez 2 questions à choix multiples (4 options A à D) :\n\n{text}",
|
| 54 |
+
"German": "Erstelle basierend auf dem folgenden Text 2 Multiple-Choice-Fragen (mit jeweils 4 Optionen A–D):\n\n{text}",
|
| 55 |
+
"Spanish": "Con base en el siguiente texto, genera 2 preguntas de opción múltiple (cada una con 4 opciones A–D):\n\n{text}"
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
def extract_text_from_pdf(file):
|
| 59 |
+
try:
|
| 60 |
+
with pdfplumber.open(file.name) as pdf:
|
| 61 |
+
text = ""
|
| 62 |
+
for page in pdf.pages:
|
| 63 |
+
page_text = page.extract_text()
|
| 64 |
+
if page_text:
|
| 65 |
+
text += page_text + "\n"
|
| 66 |
+
if not text.strip():
|
| 67 |
+
return None, "The PDF appears to contain no extractable text."
|
| 68 |
+
return text.strip(), None
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return None, f"PDF reading error: {str(e)}"
|
| 71 |
+
|
| 72 |
+
def generate_summary_and_keywords(text, summary_lang, style, char_limit):
|
| 73 |
+
prompt_template = PROMPT_TEMPLATES.get(summary_lang, PROMPT_TEMPLATES["English"])
|
| 74 |
+
prompt = prompt_template.format(style=style, char_limit=char_limit, text=text[:2000])
|
| 75 |
+
try:
|
| 76 |
+
response = client.chat.completions.create(
|
| 77 |
+
model="gpt-3.5-turbo",
|
| 78 |
+
messages=[{"role": "user", "content": prompt}],
|
| 79 |
+
temperature=0.7,
|
| 80 |
+
max_tokens=700
|
| 81 |
+
)
|
| 82 |
+
return response.choices[0].message.content
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return f"OpenAI error: {str(e)}"
|
| 85 |
+
|
| 86 |
+
def generate_quiz(summary_text, summary_lang):
|
| 87 |
+
quiz_prompt = QUIZ_PROMPTS.get(summary_lang, QUIZ_PROMPTS["English"]).format(text=summary_text)
|
| 88 |
+
try:
|
| 89 |
+
response = client.chat.completions.create(
|
| 90 |
+
model="gpt-3.5-turbo",
|
| 91 |
+
messages=[{"role": "user", "content": quiz_prompt}],
|
| 92 |
+
temperature=0.7,
|
| 93 |
+
max_tokens=500
|
| 94 |
+
)
|
| 95 |
+
return response.choices[0].message.content
|
| 96 |
+
except Exception as e:
|
| 97 |
+
return f"OpenAI quiz error: {str(e)}"
|
| 98 |
+
|
| 99 |
+
def process_input(text_input, pdf_file, summary_lang, style, char_limit, quiz_check):
|
| 100 |
+
if not char_limit.isdigit():
|
| 101 |
+
return "⚠️ Please enter a numeric character limit."
|
| 102 |
+
text = ""
|
| 103 |
+
error = None
|
| 104 |
+
if pdf_file:
|
| 105 |
+
text, error = extract_text_from_pdf(pdf_file)
|
| 106 |
+
elif text_input and text_input.strip():
|
| 107 |
+
text = text_input.strip()
|
| 108 |
+
if error:
|
| 109 |
+
return f"⚠️ {error}"
|
| 110 |
+
if not text:
|
| 111 |
+
return "⚠️ No text provided."
|
| 112 |
+
|
| 113 |
+
summary = generate_summary_and_keywords(text, summary_lang, style, char_limit)
|
| 114 |
+
if quiz_check:
|
| 115 |
+
quiz = generate_quiz(summary, summary_lang)
|
| 116 |
+
return summary + "\n\n📘 Quiz Questions:\n" + quiz
|
| 117 |
+
else:
|
| 118 |
+
return summary
|
| 119 |
+
|
| 120 |
+
with gr.Blocks() as demo:
|
| 121 |
+
gr.Markdown("## 🧠 Two-Step Summarizer with Reliable Quiz Generator")
|
| 122 |
+
|
| 123 |
+
with gr.Row():
|
| 124 |
+
summary_lang = gr.Dropdown(choices=LANGUAGES, value="English", label="Summary Language")
|
| 125 |
+
summary_style = gr.Dropdown(choices=SUMMARY_STYLES, value="Academic", label="Summary Style")
|
| 126 |
+
char_limit = gr.Textbox(label="Character Limit", value="300")
|
| 127 |
+
|
| 128 |
+
quiz_check = gr.Checkbox(label="Generate Quiz Questions", value=True)
|
| 129 |
+
|
| 130 |
+
with gr.Row():
|
| 131 |
+
text_input = gr.Textbox(lines=8, placeholder="Paste your text here...", label="Text Input")
|
| 132 |
+
pdf_file = gr.File(label="Or Upload PDF")
|
| 133 |
+
|
| 134 |
+
output = gr.Textbox(label="Output", lines=16)
|
| 135 |
+
|
| 136 |
+
run_btn = gr.Button("Summarize")
|
| 137 |
+
|
| 138 |
+
run_btn.click(fn=process_input,
|
| 139 |
+
inputs=[text_input, pdf_file, summary_lang, summary_style, char_limit, quiz_check],
|
| 140 |
+
outputs=output)
|
| 141 |
+
|
| 142 |
+
demo.launch()
|