RASMUS commited on
Commit
6fcfdbf
1 Parent(s): e3ae0f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -9
app.py CHANGED
@@ -8,6 +8,21 @@ import time
8
  Init_system_prompt = "You are an AI Assistant that tries to teach kids various subjects. You are given learning material and you task is to ask questions given the material and then you also grade answers and give feedback how to improve the answers"
9
  system_message = {"role": "system", "content": Init_system_prompt}
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  import os
12
  from PIL import Image
13
 
@@ -27,17 +42,18 @@ def print_files(files):
27
  print(file.file)
28
 
29
 
30
- def create_data(files):
31
  question_context = ''
32
  for file in files:
33
  if file.name.endswith('png') or file.name.endswith('.jpg'):
34
  try:
35
- question_context += (pytesseract.image_to_string(Image.open(file.name))) + '\n\n'
36
  except Exception as e:
37
  print(e)
38
  pass
 
39
 
40
- return question_context
41
 
42
 
43
  ########### TAB 3 (CHAT) FUNCTIONS #############################
@@ -45,22 +61,22 @@ def create_data(files):
45
  def user(user_message, history):
46
  return "", history + [[user_message, None]]
47
 
48
- def bot(history, messages_history, api_key, system_prompt, teksti_contexti, temperature, max_tokens, chatgpt_model, max_context_size_for_question):
49
  user_message = history[-1][0]
50
 
51
- bot_message, messages_history = ask_gpt(user_message, messages_history, api_key, system_prompt, teksti_contexti, temperature, max_tokens, chatgpt_model, max_context_size_for_question)
52
  messages_history += [{"role": "assistant", "content": bot_message}]
53
  history[-1][1] = bot_message
54
  time.sleep(0.2)
55
  return history, messages_history, str(messages_history)
56
 
57
- def ask_gpt(message, messages_history, api_key, system_prompt, context, temperature, max_tokens, chatgpt_model, max_context_size_for_question):
58
  messages_history, system_prompt, _ = init_history(messages_history, system_prompt)
59
  if len(messages_history) < 1:
60
  messages_history = [{"role": "system", "content": system_prompt}]
61
  max_possible_position = len(context)- max_context_size_for_question
62
  start = random.randint(0,max_possible_position)
63
- messages_history += [{"role": "user", "content": context[start:start+max_context_size_for_question] + '\n Please ask a question about the previous paragramph?'}]
64
 
65
  openai.api_key = api_key
66
  response = openai.ChatCompletion.create(
@@ -90,6 +106,7 @@ with gr.Blocks() as demo:
90
  with gr.Row():
91
  api_key = gr.Textbox(value='', type='password', label='Insert OPENAI API-key here')
92
  with gr.Row():
 
93
  files = gr.File(file_count='multiple', file_types=['image'], interactivate = True)
94
  create_context_btn = gr.Button(value='Recognize text and create context')
95
  with gr.Row():
@@ -125,12 +142,12 @@ with gr.Blocks() as demo:
125
 
126
 
127
  # TAB 1 (UPLOAD) Interactive elements:
128
- create_context_btn.click(create_data, files, teksti_contexti)
129
 
130
 
131
  # TAB 3 (CHAT) Interactive elements:
132
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
133
- bot, [chatbot, state, api_key, system_prompt, teksti_contexti, temperature, max_tokens, chatgpt_model, max_context_size_for_question], [chatbot, state, msg_log]
134
  )
135
  clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state, system_prompt], [state, system_prompt, msg_log])
136
 
 
8
  Init_system_prompt = "You are an AI Assistant that tries to teach kids various subjects. You are given learning material and you task is to ask questions given the material and then you also grade answers and give feedback how to improve the answers"
9
  system_message = {"role": "system", "content": Init_system_prompt}
10
 
11
+ system_prompts = {
12
+ "English": "You are an AI Assistant that tries to teach kids various subjects. You are given learning material and you task is to ask questions given the material and then you also grade answers and give feedback how to improve the answers",
13
+ "Finnish": "Olet tekoälyavustaja jonka tehtävänä on auttaa lapsia oppimaan koulussa. Sinulle annetaan oppimateriaalia tekstinä ja sinun tehtäväsi on kysyä kysymyksiä annetusta tekstistä, arvostella vastauksia ja antaa palautetta kuinka vastauksia voidaan parantaa."
14
+ }
15
+
16
+ question_strings = {
17
+ "English": "\n Please ask a question about the previous paragramph: Question:",
18
+ "Finnish": "\n Kysy kysymys edellisen kappaleen perusteella. Kysymys:",
19
+ }
20
+
21
+ lang_mapping = {
22
+ "English": "en",
23
+ "Finnish": "fin"
24
+ }
25
+
26
  import os
27
  from PIL import Image
28
 
 
42
  print(file.file)
43
 
44
 
45
+ def create_data(files, language_selection):
46
  question_context = ''
47
  for file in files:
48
  if file.name.endswith('png') or file.name.endswith('.jpg'):
49
  try:
50
+ question_context += (pytesseract.image_to_string(Image.open(file.name), lang=lang_mapping[language_selection])) + '\n\n'
51
  except Exception as e:
52
  print(e)
53
  pass
54
+ system_prompt = system_prompts[language_selection]
55
 
56
+ return question_context, system_prompt
57
 
58
 
59
  ########### TAB 3 (CHAT) FUNCTIONS #############################
 
61
  def user(user_message, history):
62
  return "", history + [[user_message, None]]
63
 
64
+ def bot(history, messages_history, api_key, system_prompt, teksti_contexti, temperature, max_tokens, chatgpt_model, max_context_size_for_question, language_selection):
65
  user_message = history[-1][0]
66
 
67
+ bot_message, messages_history = ask_gpt(user_message, messages_history, api_key, system_prompt, teksti_contexti, temperature, max_tokens, chatgpt_model, max_context_size_for_question, language_selection)
68
  messages_history += [{"role": "assistant", "content": bot_message}]
69
  history[-1][1] = bot_message
70
  time.sleep(0.2)
71
  return history, messages_history, str(messages_history)
72
 
73
+ def ask_gpt(message, messages_history, api_key, system_prompt, context, temperature, max_tokens, chatgpt_model, max_context_size_for_question, language_selection):
74
  messages_history, system_prompt, _ = init_history(messages_history, system_prompt)
75
  if len(messages_history) < 1:
76
  messages_history = [{"role": "system", "content": system_prompt}]
77
  max_possible_position = len(context)- max_context_size_for_question
78
  start = random.randint(0,max_possible_position)
79
+ messages_history += [{"role": "user", "content": context[start:start+max_context_size_for_question] + question_strings[language_selection]}]
80
 
81
  openai.api_key = api_key
82
  response = openai.ChatCompletion.create(
 
106
  with gr.Row():
107
  api_key = gr.Textbox(value='', type='password', label='Insert OPENAI API-key here')
108
  with gr.Row():
109
+ language_selection = gr.Dropdown(value='English', choices=["English","Finnish"], label='Select language')
110
  files = gr.File(file_count='multiple', file_types=['image'], interactivate = True)
111
  create_context_btn = gr.Button(value='Recognize text and create context')
112
  with gr.Row():
 
142
 
143
 
144
  # TAB 1 (UPLOAD) Interactive elements:
145
+ create_context_btn.click(create_data, [files, language_selection], [teksti_contexti, system_prompt])
146
 
147
 
148
  # TAB 3 (CHAT) Interactive elements:
149
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
150
+ bot, [chatbot, state, api_key, system_prompt, teksti_contexti, temperature, max_tokens, chatgpt_model, max_context_size_for_question, language_selection], [chatbot, state, msg_log]
151
  )
152
  clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state, system_prompt], [state, system_prompt, msg_log])
153