monra commited on
Commit
1111830
1 Parent(s): 8c25546

Add instructions to reply in the same language as the user

Browse files
Files changed (2) hide show
  1. requirements.txt +1 -0
  2. server/backend.py +21 -6
requirements.txt CHANGED
@@ -10,3 +10,4 @@ streamlit==1.21.0
10
  selenium
11
  fake-useragent
12
  freeGPT
 
 
10
  selenium
11
  fake-useragent
12
  freeGPT
13
+ googletrans==4.0.0-rc1
server/backend.py CHANGED
@@ -1,5 +1,6 @@
1
  import threading
2
  import re
 
3
  from flask import request
4
  from datetime import datetime
5
  from requests import get
@@ -52,16 +53,14 @@ class Backend_Api:
52
 
53
  extra = [{'role': 'user', 'content': blob}]
54
 
 
 
 
 
55
  conversation = [{'role': 'system', 'content': system_message}] + \
56
  extra + special_instructions[jailbreak] + \
57
  _conversation + [prompt]
58
 
59
- def filter_jailbroken_response(response):
60
- response = re.sub(r'GPT:.*?ACT:', '', response, flags=re.DOTALL)
61
- response = re.sub(r'ACT:', '', response)
62
-
63
- return response
64
-
65
  def stream():
66
  response = None
67
 
@@ -98,3 +97,19 @@ class Backend_Api:
98
  'success': False,
99
  "error": f"an error occurred {str(e)}"
100
  }, 400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import threading
2
  import re
3
+ from googletrans import Translator
4
  from flask import request
5
  from datetime import datetime
6
  from requests import get
 
53
 
54
  extra = [{'role': 'user', 'content': blob}]
55
 
56
+ if special_instructions[jailbreak]:
57
+ set_response_language(
58
+ prompt['content'], special_instructions[jailbreak])
59
+
60
  conversation = [{'role': 'system', 'content': system_message}] + \
61
  extra + special_instructions[jailbreak] + \
62
  _conversation + [prompt]
63
 
 
 
 
 
 
 
64
  def stream():
65
  response = None
66
 
 
97
  'success': False,
98
  "error": f"an error occurred {str(e)}"
99
  }, 400
100
+
101
+
102
+ def filter_jailbroken_response(response):
103
+ response = re.sub(r'GPT:.*?ACT:', '', response, flags=re.DOTALL)
104
+ response = re.sub(r'ACT:', '', response)
105
+ return response
106
+
107
+
108
+ def set_response_language(prompt, special_instructions_list):
109
+ print(prompt)
110
+ translator = Translator()
111
+ detected_language = translator.detect(prompt).lang
112
+ language_instructions = f"You will respond in the language: {detected_language}. "
113
+ if special_instructions_list:
114
+ special_instructions_list[0]['content'] = language_instructions + \
115
+ special_instructions_list[0]['content']