File size: 9,011 Bytes
c1e5d84
 
 
 
 
 
 
 
 
e6b354c
c1e5d84
e6b354c
 
 
 
 
 
 
c1e5d84
 
e6b354c
 
 
 
 
 
 
 
 
 
 
c1e5d84
e6b354c
c1e5d84
 
 
e6b354c
 
 
 
 
 
 
 
c1e5d84
 
 
 
 
 
 
 
e6b354c
 
 
 
 
 
 
c1e5d84
e6b354c
 
c1e5d84
e6b354c
 
 
 
 
 
 
 
 
 
 
c1e5d84
e6b354c
 
 
 
 
 
 
c1e5d84
e6b354c
 
 
 
 
 
 
 
 
 
 
 
 
c1e5d84
e6b354c
 
 
 
 
 
 
c1e5d84
e6b354c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1e5d84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6b354c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1e5d84
 
e6b354c
 
c1e5d84
 
 
 
 
 
 
 
 
 
 
 
 
 
e6b354c
 
 
 
 
c1e5d84
e6b354c
c1e5d84
e6b354c
 
 
 
c1e5d84
e6b354c
c1e5d84
e6b354c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1e5d84
 
 
 
 
e6b354c
c1e5d84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6b354c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1e5d84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#pip install fastapi   ###for fastapi
#pip install uvicorn   ###for server. to run the api serice from terminal: uvicorn main:app --reload
#pip install gunicorn  ###gunicorn --bind 0.0.0.0:8000 -k uvicorn.workers.UvicornWorker main:app
#pip install python-multipart     ###for UploadFile
#pip install pillow    ###for PIL
#pip install transformers          ###for transformers
#pip install torch     ###for torch
#pip install sentencepiece        ###for AutoTokenizer
#pip install -U cos-python-sdk-v5      ###腾讯云对象存储SDK(COS-SDK)
#pip install -q -U google-generativeai

# from typing import Optional
# from fastapi import FastAPI, Header
# #from transformers import pipeline, EfficientNetImageProcessor, EfficientNetForImageClassification, AutoTokenizer, AutoModelForSeq2SeqLM
# import torch
# from transformers import EfficientNetImageProcessor, EfficientNetForImageClassification, pipeline
# from models import ItemInHistory, ItemUploaded, ServiceLoginInfo
# from openai import OpenAI


# import sys, os, logging
# import json, requests


from fastapi import FastAPI
import sys, logging, os
from models import Item2AI, AI2Item
import google.generativeai as genai
import urllib.parse as urlparse
from qcloud_cos import CosConfig, CosS3Client
from PIL import Image

# init app and logging
app = FastAPI()
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# load google gemini models
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
gemini_pro = genai.GenerativeModel('gemini-pro')
logging.info("google gemini-pro model loaded successfully.")
gemini_pro_vision = genai.GenerativeModel('gemini-pro-vision')
logging.info("google gemini-pro-vision model loaded successfully.")

# init tencent cos
cos_secret_id = os.environ['COS_SECRET_ID']
cos_secret_key = os.environ['COS_SECRET_KEY']
cos_region = 'ap-shanghai'
cos_bucket = '7072-prod-3g52ms9o7a81f23c-1324125412'
token = None
scheme = 'https'
config = CosConfig(Region=cos_region, SecretId=cos_secret_id, SecretKey=cos_secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)
logging.info(f"tencent cos init succeeded.")

# Route to create an item
@app.post("/firstturn/")
async def ai_first_turn(item: Item2AI):
    logging.info("ai_first_turn...")
    logging.info("item:", item)

    # response = gemini_pro.generate_content("What is the meaning of life?")
    # logging.info(response.text)

    url = urlparse.urlparse(item.item_fileurl)
    key = url[2][1::]
    bucket = url[1].split('.')[1]
    contentfile = key.split('/')[1]
    historyid = contentfile.split('.')[0]

    response = client.get_object(
        Bucket = bucket,
        Key = key
    )
    response['Body'].get_stream_to_file(contentfile)

    ai2item = AI2Item(
        upload_id = item.upload_id,
        union_id = item.union_id,
        item_fileurl = item.item_fileurl,
        item_mediatype = item.item_mediatype,
        upload_datetime = item.upload_datetime,
        ai_feedback = ""
    )
    if item.item_mediatype == "image":
        # Opening the image using PIL
        img = Image.open(contentfile)
        logging.info(f"image file {contentfile} is opened.")
        response = gemini_pro_vision.generate_content(["Describe this picture in Chinese with plenty of details.", img])
        ai2item.ai_feedback = response.text
    else:
        ai2item.ai_feedback = "不是image类型,暂不能识别"

    logging.info(ai2item)

    return(ai2item)


# try:
#     ai_model_bc_preprocessor = EfficientNetImageProcessor.from_pretrained("./birds-classifier-efficientnetb2")
#     ai_model_bc_model = EfficientNetForImageClassification.from_pretrained("./birds-classifier-efficientnetb2")
#     logging.info(f"local model dennisjooo/Birds-Classifier-EfficientNetB2 loaded.")
    
# except Exception as e:
#     logging.error(e)

# try:
#     openai_client = OpenAI(
#         api_key=os.environ.get("OPENAI_API_KEY"),
#     )
#     # prompt = """你是一个鸟类学家,用中文回答关于鸟类的问题。你的回答需要满足以下要求:
#     # 1. 你的回答必须是中文
#     # 2. 回答限制在100个字以内"""
#     # conv = Conversation(open_client, prompt, 3)
#     logging.info(f"openai chat model loaded.")
# except Exception as e:
#     logging.error(e)

# try:
#    ai_model_bc_pipe= pipeline("image-classification", model="dennisjooo/Birds-Classifier-EfficientNetB2")
#    logging.info(f"remote model dennisjooo/Birds-Classifier-EfficientNetB2 loaded.")

# except Exception as e:
#    print(e)

#try:
#    ai_model_ez_preprocessor = AutoTokenizer.from_pretrained("./opus-mt-en-zh")
#    ai_model_ez_model = AutoModelForSeq2SeqLM.from_pretrained("./opus-mt-en-zh")
#    print(f"local model Helsinki-NLP/opus-mt-en-zh loaded.")
#except Exception as e:
#    print(e)

#try:
#    ai_model_ez_pipe= pipeline(task="translation_en_to_zh", model="Helsinki-NLP/opus-mt-en-zh", device=0)
#    print(f"remote model Helsinki-NLP/opus-mt-en-zh loaded.")

#except Exception as e:
#    print(e)

# def bird_classifier(image_file: str) -> str:
#     # Opening the image using PIL
#     img = Image.open(image_file)
#     logging.info(f"image file {image_file} is opened.")

#     result:str = ""
#     try:
#         inputs = ai_model_bc_preprocessor(img, return_tensors="pt")

#         # Running the inference
#         with torch.no_grad():
#             logits = ai_model_bc_model(**inputs).logits

#         # Getting the predicted label
#         predicted_label = logits.argmax(-1).item()
#         result = ai_model_bc_model.config.id2label[predicted_label]
#         logging.info(f"{ai_model_bc_model.config.id2label[predicted_label]}:{ai_model_bc_pipe(img)[0]['label']}")
#     except Exception as e:
#         logging.error(e)
        

#     logging.info(result)
#     return result

# def text_en_zh(text_en: str) -> str:
#     text_zh = ""
#     if ai_model_ez_status is MODEL_STATUS.LOCAL:
#         input = ai_model_ez_preprocessor(text_en)
#         translated = ai_model_ez_model.generate(**ai_model_ez_preprocessor(text_en, return_tensors="pt", padding=True))
#         for t in translated:
#             text_zh += ai_model_ez_preprocessor.decode(t, skip_special_tokens=True)
#     elif ai_model_ez_status is MODEL_STATUS.REMOTE:
#         text_zh = ai_model_ez_pipe(text_en)

#     return text_zh


# # Route to list all items uploaded by a specific user by unionid
# # @app.get("/items/{user_unionid}")
# # def list_items(user_unionid: str) -> dict[str, list[ItemInHistory]]:
# #     logging.info("list_items")
# #     logging.info(user_unionid)

# #     items: list[ItemInHistory] = []

# #     response = client.list_objects(
# #         Bucket=cos_bucket,
# #         Prefix=f'{user_unionid}/history/'
# #     )

# #     logging.info(response['Contents'])
    
# #     for obj in response['Contents']:
# #         key:str = obj['Key']
# #         response = client.get_object(
# #             Bucket = cos_bucket,
# #             Key = key
# #         )
# #         localfile = key.split('/')[2]
# #         response['Body'].get_stream_to_file(localfile)

# #         item = itemFromJsonFile(localfile)
# #         items.append(item)

#     return {"items": items}

# # Route to list all items uploaded by a specific user by unionid from header
# @app.get("/items/")
# def list_items_byheader(x_wx_openid: Optional[str]=Header(None)) -> dict[str, list[ItemInHistory]]:
#     logging.info("list_items_byheader")
#     logging.info(x_wx_openid)

#     items: list[ItemInHistory] = []

#     response = client.list_objects(
#         Bucket=cos_bucket,
#         Prefix=f'{x_wx_openid}/history/'
#     )

#     logging.info(response['Contents'])
    
#     for obj in response['Contents']:
#         key:str = obj['Key']
#         response = client.get_object(
#             Bucket = cos_bucket,
#             Key = key
#         )
#         localfile = key.split('/')[2]
#         response['Body'].get_stream_to_file(localfile)

#         item = itemFromJsonFile(localfile)
#         items.append(item)

#     return {"items": items}


# def itemFromJsonFile(jsonfile: str) -> ItemInHistory:
#     f = open(jsonfile, 'r')
#     content = f.read()
#     a = json.loads(content)
#     f.close()
#     return ItemInHistory(history_id = a['history_id'],union_id = a['union_id'],
#                          item_fileurl = a['item_fileurl'],item_mediatype = a["item_mediatype"],
#                          upload_datetime = a["upload_datetime"],ai_feedback = a['ai_feedback'])


# def itemToJsonFile(item: ItemInHistory):
#     history_json = {
#         "history_id": item.history_id,
#         "union_id": item.union_id,
#         "item_fileurl": item.item_fileurl,
#         "item_mediatype": item.item_mediatype,
#         "upload_datetime": item.upload_datetime,
#         "ai_feedback": item.ai_feedback
#     }
#     b = json.dumps(history_json)
#     historyfile = f'{item.history_id}.json'
#     f = open(historyfile, 'w')
#     f.write(b)
#     f.close()

#     return historyfile