Talip7 commited on
Commit
9e4b762
·
verified ·
1 Parent(s): c5ca5fa

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -13
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", "Arabic", "French", "German", "Spanish"]
11
 
12
  QUIZ_INSTRUCTIONS = {
13
  "English": "Also generate 2 quiz questions (multiple choice).",
@@ -18,6 +18,57 @@ QUIZ_INSTRUCTIONS = {
18
  "Spanish": "También genera 2 preguntas tipo test."
19
  }
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def extract_text_from_pdf(file):
22
  try:
23
  with pdfplumber.open(file.name) as pdf:
@@ -33,19 +84,10 @@ def extract_text_from_pdf(file):
33
  return None, f"PDF reading error: {str(e)}"
34
 
35
  def generate_output(text, summary_lang, style, char_limit, quiz_check):
36
- quiz_prompt = QUIZ_INSTRUCTIONS.get(summary_lang, QUIZ_INSTRUCTIONS["English"]) if quiz_check else ""
37
-
38
- prompt = f"""
39
- You are a helpful assistant.
40
-
41
- Summarize the following text in {summary_lang} using a {style} style.
42
- Keep it within approximately {char_limit} characters.
43
- Then provide 5 keywords.
44
- {quiz_prompt}
45
 
46
- TEXT:
47
- {text[:3000]}
48
- """
49
  try:
50
  response = client.chat.completions.create(
51
  model="gpt-3.5-turbo",
 
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).",
 
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}
29
+ """,
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}
69
+ """
70
+ }
71
+
72
  def extract_text_from_pdf(file):
73
  try:
74
  with pdfplumber.open(file.name) as pdf:
 
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(
93
  model="gpt-3.5-turbo",