Ailyth commited on
Commit
934ceb1
1 Parent(s): c66eacd

first push

Browse files
Files changed (5) hide show
  1. .streamlit/config.toml +2 -0
  2. app.py +236 -0
  3. model/banner.png +0 -0
  4. model/zha2024_6.h5 +3 -0
  5. requirements.txt +2 -0
.streamlit/config.toml ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ [theme]
2
+ base="dark"
app.py ADDED
@@ -0,0 +1,236 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import PIL.Image
2
+ import numpy as np
3
+ import os,re,pytz,time
4
+ import streamlit as st
5
+ from datetime import datetime
6
+ import google.generativeai as genai
7
+ import streamlit.components.v1 as components
8
+ from tensorflow.keras.models import load_model
9
+ from tensorflow.keras.preprocessing import image
10
+
11
+ UTC_8 = pytz.timezone('Asia/Shanghai')
12
+ #Load the model
13
+ my_model = load_model("model/zha2024_5-83.38.h5")
14
+ target_size = (300, 300)
15
+ class_labels = {0: '炭黑组', 1: '正常发挥', 2: '炫彩组', 3: '糊糊组', 4: '炸组日常', 5: '凡尔赛',6: '非食物'}
16
+ predicted_class=''
17
+ #Set up the Gemini model and API key
18
+ #https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/gemini?hl=zh-cn
19
+ MY_KEY= os.environ.get("GOOGLE_API_KEY")
20
+ genai.configure(api_key=MY_KEY)
21
+ gemini_model = genai.GenerativeModel('gemini-pro-vision')
22
+ neutral=os.environ.get("sys_info_0")
23
+ toxic=os.environ.get("sys_info_1")
24
+ heartfelt=os.environ.get("sys_info_2")
25
+ chilly_list=os.environ.get("X").split(",")
26
+ default_prompt=''
27
+
28
+ generation_config = {
29
+ "temperature": 0.99,
30
+ "top_p": 1,
31
+ "top_k": 40,
32
+ "max_output_tokens": 2048,
33
+ "candidate_count":1
34
+ }
35
+ safety_settings = [
36
+ {
37
+ "category": "HARM_CATEGORY_HARASSMENT",
38
+ "threshold": "BLOCK_NONE"
39
+ },
40
+ {
41
+ "category": "HARM_CATEGORY_HATE_SPEECH",
42
+ "threshold": "BLOCK_NONE"
43
+ },
44
+ {
45
+ "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
46
+ "threshold": "BLOCK_NONE"
47
+ },
48
+ {
49
+ "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
50
+ "threshold": "BLOCK_NONE"
51
+ }
52
+ ]
53
+
54
+ #fuctions
55
+ def preprocess_image(img_path):
56
+ img = image.load_img(img_path, target_size=target_size)
57
+ img_array = image.img_to_array(img)
58
+ img_array = np.expand_dims(img_array, axis=0)
59
+ return img_array
60
+
61
+ def chilly_words_killer(text,words_list):
62
+ for word in words_list:
63
+ pattern = re.compile(re.escape(word), re.IGNORECASE)
64
+ text = pattern.sub("**😮", text)
65
+ return text
66
+
67
+ def get_critic_info(review_style):
68
+ if review_style == '默认':
69
+ default_prompt = neutral
70
+ critic_name = 'SavorBalancer'
71
+ avatar = '👩‍🍳'
72
+ elif review_style == '毒舌👾':
73
+ default_prompt = toxic
74
+ critic_name = 'SpicyCritique'
75
+ avatar = '😈'
76
+ elif review_style == '暖心🍀':
77
+ default_prompt = heartfelt
78
+ critic_name = 'GentleGourmet'
79
+ avatar = '🤗'
80
+ else:
81
+ raise ValueError(f'Invalid review style: {review_style}')
82
+ return default_prompt, critic_name, avatar
83
+
84
+ def img_score(img_raw_path):
85
+ global predicted_class
86
+ img_array = preprocess_image(img_raw_path)
87
+ predictions = my_model.predict(img_array)
88
+ predicted_class_index = np.argmax(predictions, axis=-1)
89
+ predicted_class = class_labels[predicted_class_index[0]]
90
+ class_probabilities = {label: prob for label, prob in zip(class_labels.values(), predictions[0])}
91
+
92
+ score={k: round(v * 100, 2) for k, v in class_probabilities.items()}
93
+ high_score_float=predictions[0,(predicted_class_index[0])]
94
+ high_score=round(high_score_float*100,2)
95
+ return score,high_score
96
+
97
+ def score_desc(score):
98
+ if score > 90:
99
+ return "这妥妥的属于"
100
+ elif score >= 70 and score <= 90:
101
+ return "这大概率属于"
102
+ elif score >= 40 and score <= 70:
103
+ return "这可能属于"
104
+ else:
105
+ return "我猜这属于"
106
+
107
+
108
+ def review_waiting(_class, critic_name):
109
+ if _class == '非食物':
110
+ return '图里面好像没有食物吧❓'
111
+ elif critic_name == 'SavorBalancer':
112
+ return '🍴品尝中,正在构思点评'
113
+ elif critic_name == 'SpicyCritique':
114
+ return '不要催啦,我这不正在吃吗💢'
115
+ elif critic_name == 'GentleGourmet':
116
+ return '正在为你种彩虹🌈'
117
+
118
+
119
+ def gemini_bot(default_prompt,img_raw_path,_class):
120
+ img = PIL.Image.open(img_raw_path)
121
+ model = gemini_model
122
+ klass="当前食物类型是:"+_class
123
+ prompt=klass+default_prompt
124
+ response = model.generate_content([prompt, img],
125
+ stream=False,
126
+ safety_settings=safety_settings,
127
+ generation_config=generation_config)
128
+ response.resolve()
129
+ response_text=f'''{response.text}'''
130
+ final_response=chilly_words_killer(response_text,chilly_list)
131
+ return final_response
132
+
133
+ def review():
134
+ if predicted_class is not None:
135
+ with st.spinner(review_waiting(predicted_class, critic_name)):
136
+ print(f"{datetime.now(UTC_8).strftime('%m-%d %H:%M:%S')}--Start Reviewing")
137
+ final_response = gemini_bot(default_prompt, img_raw_path, predicted_class)
138
+ with st.chat_message(critic_name, avatar=avatar):
139
+ st.write(final_response)
140
+ st.button("再次点评", key="1")
141
+ print(f"{datetime.now(UTC_8).strftime('%m-%d %H:%M:%S')}--Complete\n💣💣💣")
142
+ info('#edfde2','#78817a','🆗点评完毕,内容有AI生成,仅供娱乐',55)
143
+
144
+ def info(bg_color,font_color,text,height):
145
+ html=f'''<html><style>
146
+ body {{
147
+ margin: 0;
148
+ padding: 0;
149
+ color: #3b4740;
150
+ background-color: transparent;
151
+ }}
152
+ .container {{
153
+ max-width: 100%;
154
+ margin: 0 auto;
155
+ padding: 20px;
156
+ font-size: 15px;
157
+ color:{font_color}
158
+ display: flex;
159
+ justify-content: space-between;
160
+ align-items: center;
161
+ background-color: #f8e9a000;
162
+ padding: 15px;
163
+ border-radius: 10px;
164
+ line-height: 1.6;
165
+ border: #fde2e4 0px solid;
166
+ background-color:{bg_color} ;
167
+ }}
168
+ </style><body><div class="container">
169
+ {text}
170
+ </body></html>'''
171
+ components.html(html,height=height)
172
+
173
+ #Streamlit UI
174
+ #Guide: https://docs.streamlit.io/library/api-reference
175
+ #st.header("🧨ZhazuEvaluator")
176
+ #st.subheader('', divider='rainbow')
177
+ st.image('https://cdnjson.com/images/2024/01/30/banner7f1835c564c9b79c.png')
178
+
179
+ # Upload an image
180
+ bg_color='#e1f1fa'
181
+ border_font_color='#78817a'
182
+ css=f'''<style>
183
+ [data-testid="stFileUploadDropzone"]{{background-color:{bg_color};color:{border_font_color}}}
184
+ [data-testid="baseButton-secondary"]{{background-color:{bg_color};border:1px {border_font_color} solid;color:{border_font_color}}}
185
+ [data-testid="baseButton-secondary"]>div[data-testid="stMarkdownContainer"] {{border: none;}}
186
+ div[data-testid="stFileDropzoneInstructions"]>div>span::after {{
187
+ content:"✨来上传一张你的得意之作,PC可直接拖放上传";
188
+ visibility:visible;
189
+ display:block;
190
+ }}
191
+
192
+ </style>'''
193
+ st.markdown(css,unsafe_allow_html=True)
194
+
195
+ img_raw_path = st.file_uploader("", type=['png', 'jpg', 'jpeg','webp'])
196
+
197
+ col1, col2 = st.columns(2)
198
+ my_image = ""
199
+ if not img_raw_path is None:
200
+ my_image = img_raw_path.read()
201
+ my_image = PIL.Image.open(img_raw_path)
202
+ print(f"{datetime.now(UTC_8).strftime('%m-%d %H:%M:%S')}--IMG uploaded")
203
+ with col1:
204
+ st.image(my_image, caption='✅图片已上传', width=350)
205
+
206
+ # Predict the class of the image
207
+ if my_image:
208
+ with st.spinner('💥正在打分中...'):
209
+ print(f"{datetime.now(UTC_8).strftime('%m-%d %H:%M:%S')}--Start Classification")
210
+ score,high_score=img_score(img_raw_path)
211
+ with col2:
212
+ st.bar_chart(score, color='#fdd3de',width=412)
213
+ score_noti=f"📝{score_desc(high_score)}{predicted_class}➡️得分:{high_score}"
214
+ info('#edfde2','#78817a',score_noti,55)
215
+
216
+ review_style= st.radio(
217
+ "请选择点评文字风格",
218
+ ["默认", "毒舌👾", "暖心🍀"],
219
+ index=0, horizontal=True
220
+ )
221
+ default_prompt, critic_name, avatar=get_critic_info(review_style)
222
+
223
+ #review
224
+ if my_image:
225
+ review()
226
+
227
+ announcements='''注意事项\n
228
+ 1.上传的图片有一定概率不会被识别,可能出现点评完全和图片无关的情况,特别是非食物图片\n
229
+ 2.如果AI开始说车轱辘话,不断重复某个句式,内容也相关性不大,请重新点评。\n
230
+ 3.毒舌点评可能会出现轻微冒犯用语,请不要放在心上。
231
+ '''
232
+ st.warning(announcements)
233
+ left_blank, centre,last_blank = st.columns([3.4,2,3])
234
+ with centre:
235
+ st.image("https://visitor-badge.laobi.icu/badge?page_id=Ailyth/z2024&left_text=MyDearVisitors&left_color=pink&right_color=Paleturquoise")
236
+
model/banner.png ADDED
model/zha2024_6.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:43b983819c030871eeae11ed79816b599402ccc2fc8d7d03dadc6a85c8539e52
3
+ size 135402752
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ google.generativeai
2
+ tensorflow