liuyizhang commited on
Commit
0dae273
1 Parent(s): 97f95b2

update app.py, add tencent_utils.py

Browse files
Files changed (3) hide show
  1. app.py +13 -2
  2. requirements.txt +8 -0
  3. tencent_utils.py +54 -0
app.py CHANGED
@@ -46,6 +46,9 @@ lama_cleaner_enable = True
46
 
47
  kosmos_enable = False
48
 
 
 
 
49
  if os.environ.get('IS_MY_DEBUG') is not None:
50
  sam_enable = False
51
  ram_enable = False
@@ -93,6 +96,11 @@ if kosmos_enable:
93
 
94
  from kosmos_utils import *
95
 
 
 
 
 
 
96
  config_file = 'GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py'
97
  ckpt_repo_id = "ShilongLiu/GroundingDINO"
98
  ckpt_filenmae = "groundingdino_swint_ogc.pth"
@@ -558,6 +566,9 @@ def get_time_cost(run_task_time, time_cost_str):
558
  def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_threshold, text_threshold,
559
  iou_threshold, inpaint_mode, mask_source_radio, remove_mode, remove_mask_extend, num_relation, kosmos_input, cleaner_size_limit=1080):
560
 
 
 
 
561
  run_task_time = 0
562
  time_cost_str = ''
563
  run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
@@ -943,12 +954,12 @@ if __name__ == "__main__":
943
  ])
944
 
945
  DESCRIPTION = f'### This demo from [Grounded-Segment-Anything](https://github.com/IDEA-Research/Grounded-Segment-Anything). <br>'
946
- if ram_enable:
947
- DESCRIPTION += f'RAM from [RelateAnything](https://github.com/Luodian/RelateAnything). <br>'
948
  if lama_cleaner_enable:
949
  DESCRIPTION += f'Remove(cleaner) from [lama-cleaner](https://github.com/Sanster/lama-cleaner). <br>'
950
  if kosmos_enable:
951
  DESCRIPTION += f'Kosmos-2 from [Kosmos-2](https://github.com/microsoft/unilm/tree/master/kosmos-2). <br>'
 
 
952
  DESCRIPTION += f'Thanks for their excellent work.'
953
  DESCRIPTION += f'<p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings. \
954
  <a href="https://huggingface.co/spaces/yizhangliu/Grounded-Segment-Anything?duplicate=true"><img style="display: inline; margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" /></a></p>'
 
46
 
47
  kosmos_enable = False
48
 
49
+ # qwen_enable = True
50
+ # from qwen_utils import *
51
+
52
  if os.environ.get('IS_MY_DEBUG') is not None:
53
  sam_enable = False
54
  ram_enable = False
 
96
 
97
  from kosmos_utils import *
98
 
99
+ from tencent_utils import get_tmt_client, getTextTrans_tmt
100
+ tmt_client = get_tmt_client()
101
+ def getTextTrans(text, source='zh', target='en'):
102
+ return getTextTrans_tmt(tmt_client, text, source, target)
103
+
104
  config_file = 'GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py'
105
  ckpt_repo_id = "ShilongLiu/GroundingDINO"
106
  ckpt_filenmae = "groundingdino_swint_ogc.pth"
 
566
  def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_threshold, text_threshold,
567
  iou_threshold, inpaint_mode, mask_source_radio, remove_mode, remove_mask_extend, num_relation, kosmos_input, cleaner_size_limit=1080):
568
 
569
+ text_prompt = getTextTrans(text_prompt, source='zh', target='en')
570
+ inpaint_prompt = getTextTrans(inpaint_prompt, source='zh', target='en')
571
+
572
  run_task_time = 0
573
  time_cost_str = ''
574
  run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
 
954
  ])
955
 
956
  DESCRIPTION = f'### This demo from [Grounded-Segment-Anything](https://github.com/IDEA-Research/Grounded-Segment-Anything). <br>'
 
 
957
  if lama_cleaner_enable:
958
  DESCRIPTION += f'Remove(cleaner) from [lama-cleaner](https://github.com/Sanster/lama-cleaner). <br>'
959
  if kosmos_enable:
960
  DESCRIPTION += f'Kosmos-2 from [Kosmos-2](https://github.com/microsoft/unilm/tree/master/kosmos-2). <br>'
961
+ if ram_enable:
962
+ DESCRIPTION += f'RAM from [RelateAnything](https://github.com/Luodian/RelateAnything). <br>'
963
  DESCRIPTION += f'Thanks for their excellent work.'
964
  DESCRIPTION += f'<p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings. \
965
  <a href="https://huggingface.co/spaces/yizhangliu/Grounded-Segment-Anything?duplicate=true"><img style="display: inline; margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" /></a></p>'
requirements.txt CHANGED
@@ -38,4 +38,12 @@ mmcv==2.0.0
38
  mmengine
39
  openmim==0.3.9
40
 
 
 
 
 
 
 
 
 
41
 
 
38
  mmengine
39
  openmim==0.3.9
40
 
41
+ tiktoken
42
+ einops
43
+ transformers_stream_generator==0.0.4
44
+ tensorboard
45
+ bitsandbytes
46
+ auto-gptq
47
+
48
+ tencentcloud-sdk-python
49
 
tencent_utils.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json, os
2
+ from tencentcloud.common import credential
3
+ from tencentcloud.common.profile.client_profile import ClientProfile
4
+ from tencentcloud.common.profile.http_profile import HttpProfile
5
+ from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
6
+ from tencentcloud.tmt.v20180321 import tmt_client, models
7
+
8
+ def get_tmt_client():
9
+ try:
10
+ # 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
11
+ # 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
12
+ # 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
13
+ SecretId = os.environ.get("TENCENTCLOUD_SECRET_ID")
14
+ SecretKey = os.environ.get("TENCENTCLOUD_SECRET_KEY")
15
+ cred = credential.Credential(SecretId, SecretKey)
16
+ # 实例化一个http选项,可选的,没有特殊需求可以跳过
17
+ httpProfile = HttpProfile()
18
+ httpProfile.endpoint = "tmt.tencentcloudapi.com"
19
+
20
+ # 实例化一个client选项,可选的,没有特殊需求可以跳过
21
+ clientProfile = ClientProfile()
22
+ clientProfile.httpProfile = httpProfile
23
+ # 实例化要请求产品的client对象,clientProfile是可选的
24
+ client = tmt_client.TmtClient(cred, "ap-shanghai", clientProfile)
25
+ print(f'client_{client}')
26
+ return client
27
+ except TencentCloudSDKException as err:
28
+ print(f'client_err_{err}')
29
+ return None
30
+
31
+ def getTextTrans_tmt(tmt_client, text, source='zh', target='en'):
32
+ def is_chinese(string):
33
+ for ch in string:
34
+ if u'\u4e00' <= ch <= u'\u9fff':
35
+ return True
36
+ return False
37
+
38
+ if tmt_client is None:
39
+ return text
40
+ if not is_chinese(text) and target == 'en':
41
+ return text
42
+ try:
43
+ req = models.TextTranslateRequest()
44
+ params = {
45
+ "SourceText": text,
46
+ "Source": source,
47
+ "Target": target,
48
+ "ProjectId": 0
49
+ }
50
+ req.from_json_string(json.dumps(params))
51
+ resp = tmt_client.TextTranslate(req)
52
+ return resp.TargetText
53
+ except Exception as e:
54
+ return text