Talip7 commited on
Commit
bc00931
·
verified ·
1 Parent(s): 4244188

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -29
app.py CHANGED
@@ -7,22 +7,13 @@ 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", "French", "German", "Spanish", "Arabic"]
11
-
12
- QUIZ_INSTRUCTIONS = {
13
- "English": "Also generate 2 quiz questions (multiple choice).",
14
- "Turkish": "Ayrıca 2 adet çoktan seçmeli quiz sorusu da üret.",
15
- "French": "Générez également 2 questions à choix multiples.",
16
- "German": "Erzeuge zusätzlich 2 Multiple-Choice-Fragen.",
17
- "Arabic": "قم بإنشاء سؤالين اختيار من متعدد، كل سؤال يتضمن أربعة خيارات (A، B، C، D).",
18
- "Spanish": "También genera 2 preguntas tipo test."
19
- }
20
 
21
  PROMPT_TEMPLATES = {
22
  "English": """
23
- Please summarize the following text in an {style} style and keep it within approximately {char_limit} characters.
24
  Then provide 5 relevant keywords.
25
- {quiz_instruction}
26
 
27
  TEXT:
28
  {text}
@@ -30,39 +21,39 @@ TEXT:
30
  "Turkish": """
31
  Lütfen aşağıdaki metni {style} tarzında ve yaklaşık {char_limit} karakter olacak şekilde özetle.
32
  Ardından 5 anahtar kelime ver.
33
- {quiz_instruction}
34
 
35
  METİN:
36
  {text}
37
  """,
38
  "French": """
39
- Veuillez résumer le texte suivant dans un style {style} en environ {char_limit} caractères.
40
- Ensuite, donnez 5 mots-clés pertinents.
41
- {quiz_instruction}
42
 
43
  TEXTE :
44
  {text}
45
  """,
46
  "German": """
47
- Fasse den folgenden Text in einem {style}-Stil mit ungefähr {char_limit} Zeichen zusammen.
48
- Gib danach 5 relevante Schlüsselwörter an.
49
- {quiz_instruction}
50
 
51
  TEXT:
52
  {text}
53
  """,
54
  "Spanish": """
55
- Resume el siguiente texto en un estilo {style} y con un límite aproximado de {char_limit} caracteres.
56
- Luego, proporciona 5 palabras clave relevantes.
57
- {quiz_instruction}
58
 
59
  TEXTO:
60
  {text}
61
  """,
62
  "Arabic": """
63
- لخص النص التالي بأسلوب {style} وبحدود {char_limit} حرفًا تقريبًا.
64
- ثم قدم 5 كلمات مفتاحية ذات صلة.
65
- {quiz_instruction}
66
 
67
  النص:
68
  {text}
@@ -84,9 +75,8 @@ def extract_text_from_pdf(file):
84
  return None, f"PDF reading error: {str(e)}"
85
 
86
  def generate_output(text, summary_lang, style, char_limit, quiz_check):
87
- quiz_instruction = QUIZ_INSTRUCTIONS.get(summary_lang, QUIZ_INSTRUCTIONS["English"]) if quiz_check else ""
88
  prompt_template = PROMPT_TEMPLATES.get(summary_lang, PROMPT_TEMPLATES["English"])
89
- prompt = prompt_template.format(style=style, char_limit=char_limit, quiz_instruction=quiz_instruction, text=text[:3000])
90
 
91
  try:
92
  response = client.chat.completions.create(
@@ -95,7 +85,7 @@ def generate_output(text, summary_lang, style, char_limit, quiz_check):
95
  {"role": "user", "content": prompt}
96
  ],
97
  temperature=0.7,
98
- max_tokens=600
99
  )
100
  return response.choices[0].message.content
101
  except Exception as e:
@@ -128,7 +118,7 @@ with gr.Blocks() as demo:
128
  text_input = gr.Textbox(lines=8, placeholder="Paste your text here...", label="Text Input")
129
  pdf_file = gr.File(label="Or Upload PDF")
130
 
131
- output = gr.Textbox(label="Output", lines=12)
132
 
133
  run_btn = gr.Button("Summarize")
134
 
 
7
  client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
8
 
9
  SUMMARY_STYLES = ["Child-Friendly", "Academic", "Tweet"]
10
+ LANGUAGES = ["English", "Turkish", "Arabic", "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}
 
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
  "Arabic": """
54
+ لخّص النص التالي بأسلوب {style} وبحدود {char_limit} حرفًا تقريبًا.
55
+ ثم قدم 5 كلمات مفتاحية مهمة.
56
+ قم بإنشاء سؤالين اختيار من متعدد، كل سؤال يتضمن أربعة خيارات (A، B، C، D).
57
 
58
  النص:
59
  {text}
 
75
  return None, f"PDF reading error: {str(e)}"
76
 
77
  def generate_output(text, summary_lang, style, char_limit, quiz_check):
 
78
  prompt_template = PROMPT_TEMPLATES.get(summary_lang, PROMPT_TEMPLATES["English"])
79
+ prompt = prompt_template.format(style=style, char_limit=char_limit, text=text[:3000])
80
 
81
  try:
82
  response = client.chat.completions.create(
 
85
  {"role": "user", "content": prompt}
86
  ],
87
  temperature=0.7,
88
+ max_tokens=800
89
  )
90
  return response.choices[0].message.content
91
  except Exception as e:
 
118
  text_input = gr.Textbox(lines=8, placeholder="Paste your text here...", label="Text Input")
119
  pdf_file = gr.File(label="Or Upload PDF")
120
 
121
+ output = gr.Textbox(label="Output", lines=14)
122
 
123
  run_btn = gr.Button("Summarize")
124