Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,18 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
import os, sys, json
|
4 |
from loguru import logger
|
5 |
import paddlehub as hub
|
6 |
import random
|
7 |
|
8 |
-
|
9 |
-
language_translation_model = hub.Module(directory=f'./baidu_translate')
|
10 |
-
def getTextTrans(text, source='zh', target='en'):
|
11 |
-
def is_chinese(string):
|
12 |
-
for ch in string:
|
13 |
-
if u'\u4e00' <= ch <= u'\u9fff':
|
14 |
-
return True
|
15 |
-
return False
|
16 |
-
|
17 |
-
if not is_chinese(text) and target == 'en':
|
18 |
-
return text
|
19 |
-
|
20 |
-
try:
|
21 |
-
text_translation = language_translation_model.translate(text, source, target)
|
22 |
-
return text_translation
|
23 |
-
except Exception as e:
|
24 |
-
return text
|
25 |
-
|
26 |
session_token = os.environ.get('SessionToken')
|
27 |
logger.info(f"session_token_: {session_token}")
|
28 |
|
29 |
def get_api():
|
30 |
api = None
|
31 |
try:
|
32 |
-
api =
|
33 |
# api.refresh_auth()
|
34 |
except Exception as e:
|
35 |
print(f'get_api_error:', e)
|
@@ -41,14 +24,10 @@ def get_response_from_chatgpt(api, text):
|
|
41 |
if api is None:
|
42 |
return "Openai said: I'm too tired. Let me lie down for a few days. If you like, you can visit my home."
|
43 |
try:
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
conversation_id = resp['conversation_id']
|
49 |
-
parent_id = resp['parent_id']
|
50 |
-
# logger.info(f"response_: {response}")
|
51 |
-
logger.info(f"conversation_id_: [{conversation_id}] / parent_id: [{parent_id}]")
|
52 |
except:
|
53 |
response = "Openai said: I'm so tired. Let me lie down for a few days. If you like, you can visit my home."
|
54 |
return response
|
@@ -255,8 +234,6 @@ start_work = """async() => {
|
|
255 |
|
256 |
space_ids = {
|
257 |
"spaces/stabilityai/stable-diffusion":"Stable Diffusion 2.1",
|
258 |
-
# "spaces/runwayml/stable-diffusion-v1-5":"Stable Diffusion 1.5",
|
259 |
-
# "spaces/stabilityai/stable-diffusion-1":"Stable Diffusion 1.0",
|
260 |
}
|
261 |
|
262 |
tab_actions = []
|
|
|
1 |
+
import openai
|
2 |
import gradio as gr
|
3 |
import os, sys, json
|
4 |
from loguru import logger
|
5 |
import paddlehub as hub
|
6 |
import random
|
7 |
|
8 |
+
openai.api_key = os.environ.get('SessionToken')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
session_token = os.environ.get('SessionToken')
|
10 |
logger.info(f"session_token_: {session_token}")
|
11 |
|
12 |
def get_api():
|
13 |
api = None
|
14 |
try:
|
15 |
+
api = openai
|
16 |
# api.refresh_auth()
|
17 |
except Exception as e:
|
18 |
print(f'get_api_error:', e)
|
|
|
24 |
if api is None:
|
25 |
return "Openai said: I'm too tired. Let me lie down for a few days. If you like, you can visit my home."
|
26 |
try:
|
27 |
+
response = openai.Completion.create(engine='text-davinci-003', prompt=text, max_tokens=100)
|
28 |
+
response_str = response["choices"][0]["text"].replace("\n", "")
|
29 |
+
response_str = response_str.split(user_name + ": ", 1)[0].split(bot_name + ": ", 1)[0]
|
30 |
+
logger.info(f"conversation_id_: [{esponse}]")
|
|
|
|
|
|
|
|
|
31 |
except:
|
32 |
response = "Openai said: I'm so tired. Let me lie down for a few days. If you like, you can visit my home."
|
33 |
return response
|
|
|
234 |
|
235 |
space_ids = {
|
236 |
"spaces/stabilityai/stable-diffusion":"Stable Diffusion 2.1",
|
|
|
|
|
237 |
}
|
238 |
|
239 |
tab_actions = []
|