Talip7 commited on
Commit
ba8e2f6
·
verified ·
1 Parent(s): a878591

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -121
app.py DELETED
@@ -1,121 +0,0 @@
1
-
2
- import os
3
- import gradio as gr
4
- import pdfplumber
5
- from openai import OpenAI
6
-
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
- Also generate 2 multiple choice questions, each with 4 options (A, B, C, D).
17
-
18
- TEXT:
19
- {text}
20
- """,
21
- "Turkish": """
22
- Lütfen aşağıdaki metni {style} tarzında ve yaklaşık {char_limit} karakter olacak şekilde özetle.
23
- Ardından 5 anahtar kelime ver.
24
- Ayrıca 2 adet çoktan seçmeli quiz sorusu üret, her biri dört seçenek (A, B, C, D) içersin.
25
-
26
- METİN:
27
- {text}
28
- """,
29
- "French": """
30
- Veuillez résumer le texte suivant dans un style {style}, en environ {char_limit} caractères.
31
- Fournissez ensuite 5 mots-clés pertinents.
32
- Générez également 2 questions à choix multiples avec 4 options (A, B, C, D).
33
-
34
- TEXTE :
35
- {text}
36
- """,
37
- "German": """
38
- Fassen Sie den folgenden Text im {style}-Stil mit etwa {char_limit} Zeichen zusammen.
39
- Geben Sie anschließend 5 relevante Schlüsselwörter an.
40
- Erstellen Sie außerdem 2 Multiple-Choice-Fragen mit jeweils 4 Optionen (A, B, C, D).
41
-
42
- TEXT:
43
- {text}
44
- """,
45
- "Spanish": """
46
- Resume el siguiente texto en un estilo {style} y con un límite de aproximadamente {char_limit} caracteres.
47
- Luego proporciona 5 palabras clave relevantes.
48
- También genera 2 preguntas de opción múltiple con 4 opciones (A, B, C, D).
49
-
50
- TEXTO:
51
- {text}
52
- """
53
- }
54
-
55
- def extract_text_from_pdf(file):
56
- try:
57
- with pdfplumber.open(file.name) as pdf:
58
- text = ""
59
- for page in pdf.pages:
60
- page_text = page.extract_text()
61
- if page_text:
62
- text += page_text + "\n"
63
- if not text.strip():
64
- return None, "The PDF appears to contain no extractable text."
65
- return text.strip(), None
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(
75
- model="gpt-3.5-turbo",
76
- messages=[
77
- {"role": "user", "content": prompt}
78
- ],
79
- temperature=0.7,
80
- max_tokens=800
81
- )
82
- return response.choices[0].message.content
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:
92
- text, error = extract_text_from_pdf(pdf_file)
93
- elif text_input and text_input.strip():
94
- text = text_input.strip()
95
- if error:
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")
112
-
113
- output = gr.Textbox(label="Output", lines=14)
114
-
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()