Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from openai import OpenAI
|
|
| 7 |
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
|
| 8 |
|
| 9 |
SUMMARY_STYLES = ["Child-Friendly", "Academic", "Tweet"]
|
| 10 |
-
LANGUAGES = ["English", "Turkish", "
|
| 11 |
|
| 12 |
PROMPT_TEMPLATES = {
|
| 13 |
"English": """
|
|
@@ -49,14 +49,6 @@ También genera 2 preguntas de opción múltiple con 4 opciones (A, B, C, D).
|
|
| 49 |
|
| 50 |
TEXTO:
|
| 51 |
{text}
|
| 52 |
-
""",
|
| 53 |
-
"Arabic": """
|
| 54 |
-
لخّص النص التالي بأسلوب {style} وبحدود {char_limit} حرفًا تقريبًا.
|
| 55 |
-
ثم قدم 5 كلمات مفتاحية مهمة.
|
| 56 |
-
قم بإنشاء سؤالين اختيار من متعدد، كل سؤال يتضمن أربعة خيارات (A، B، C، D).
|
| 57 |
-
|
| 58 |
-
النص:
|
| 59 |
-
{text}
|
| 60 |
"""
|
| 61 |
}
|
| 62 |
|
|
@@ -74,9 +66,9 @@ def extract_text_from_pdf(file):
|
|
| 74 |
except Exception as e:
|
| 75 |
return None, f"PDF reading error: {str(e)}"
|
| 76 |
|
| 77 |
-
def generate_output(text, summary_lang, style, char_limit
|
| 78 |
prompt_template = PROMPT_TEMPLATES.get(summary_lang, PROMPT_TEMPLATES["English"])
|
| 79 |
-
prompt = prompt_template.format(style=style, char_limit=char_limit, text=text[:
|
| 80 |
|
| 81 |
try:
|
| 82 |
response = client.chat.completions.create(
|
|
@@ -91,7 +83,9 @@ def generate_output(text, summary_lang, style, char_limit, quiz_check):
|
|
| 91 |
except Exception as e:
|
| 92 |
return f"OpenAI error: {str(e)}"
|
| 93 |
|
| 94 |
-
def process_input(text_input, pdf_file, summary_lang, style, char_limit
|
|
|
|
|
|
|
| 95 |
text = ""
|
| 96 |
error = None
|
| 97 |
if pdf_file:
|
|
@@ -102,18 +96,16 @@ def process_input(text_input, pdf_file, summary_lang, style, char_limit, quiz_ch
|
|
| 102 |
return f"⚠️ {error}"
|
| 103 |
if not text:
|
| 104 |
return "⚠️ No text provided."
|
| 105 |
-
return generate_output(text, summary_lang, style, char_limit
|
| 106 |
|
| 107 |
with gr.Blocks() as demo:
|
| 108 |
-
gr.Markdown("## 🧠 Smart Note Summarizer")
|
| 109 |
|
| 110 |
with gr.Row():
|
| 111 |
summary_lang = gr.Dropdown(choices=LANGUAGES, value="English", label="Summary Language")
|
| 112 |
summary_style = gr.Dropdown(choices=SUMMARY_STYLES, value="Academic", label="Summary Style")
|
| 113 |
char_limit = gr.Textbox(label="Character Limit", value="300")
|
| 114 |
|
| 115 |
-
quiz_check = gr.Checkbox(label="Generate Quiz Questions", value=True)
|
| 116 |
-
|
| 117 |
with gr.Row():
|
| 118 |
text_input = gr.Textbox(lines=8, placeholder="Paste your text here...", label="Text Input")
|
| 119 |
pdf_file = gr.File(label="Or Upload PDF")
|
|
@@ -123,7 +115,7 @@ with gr.Blocks() as demo:
|
|
| 123 |
run_btn = gr.Button("Summarize")
|
| 124 |
|
| 125 |
run_btn.click(fn=process_input,
|
| 126 |
-
inputs=[text_input, pdf_file, summary_lang, summary_style, char_limit
|
| 127 |
outputs=output)
|
| 128 |
|
| 129 |
demo.launch()
|
|
|
|
| 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": """
|
|
|
|
| 49 |
|
| 50 |
TEXTO:
|
| 51 |
{text}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
"""
|
| 53 |
}
|
| 54 |
|
|
|
|
| 66 |
except Exception as e:
|
| 67 |
return None, f"PDF reading error: {str(e)}"
|
| 68 |
|
| 69 |
+
def generate_output(text, summary_lang, style, char_limit):
|
| 70 |
prompt_template = PROMPT_TEMPLATES.get(summary_lang, PROMPT_TEMPLATES["English"])
|
| 71 |
+
prompt = prompt_template.format(style=style, char_limit=char_limit, text=text[:2000])
|
| 72 |
|
| 73 |
try:
|
| 74 |
response = client.chat.completions.create(
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
return f"OpenAI error: {str(e)}"
|
| 85 |
|
| 86 |
+
def process_input(text_input, pdf_file, summary_lang, style, char_limit):
|
| 87 |
+
if not char_limit.isdigit():
|
| 88 |
+
return "⚠️ Please enter a numeric character limit."
|
| 89 |
text = ""
|
| 90 |
error = None
|
| 91 |
if pdf_file:
|
|
|
|
| 96 |
return f"⚠️ {error}"
|
| 97 |
if not text:
|
| 98 |
return "⚠️ No text provided."
|
| 99 |
+
return generate_output(text, summary_lang, style, char_limit)
|
| 100 |
|
| 101 |
with gr.Blocks() as demo:
|
| 102 |
+
gr.Markdown("## 🧠 Smart Note Summarizer (Stable Version)")
|
| 103 |
|
| 104 |
with gr.Row():
|
| 105 |
summary_lang = gr.Dropdown(choices=LANGUAGES, value="English", label="Summary Language")
|
| 106 |
summary_style = gr.Dropdown(choices=SUMMARY_STYLES, value="Academic", label="Summary Style")
|
| 107 |
char_limit = gr.Textbox(label="Character Limit", value="300")
|
| 108 |
|
|
|
|
|
|
|
| 109 |
with gr.Row():
|
| 110 |
text_input = gr.Textbox(lines=8, placeholder="Paste your text here...", label="Text Input")
|
| 111 |
pdf_file = gr.File(label="Or Upload PDF")
|
|
|
|
| 115 |
run_btn = gr.Button("Summarize")
|
| 116 |
|
| 117 |
run_btn.click(fn=process_input,
|
| 118 |
+
inputs=[text_input, pdf_file, summary_lang, summary_style, char_limit],
|
| 119 |
outputs=output)
|
| 120 |
|
| 121 |
demo.launch()
|