import PIL.Image import numpy as np import os,re,pytz,time import streamlit as st from datetime import datetime import google.generativeai as genai import streamlit.components.v1 as components from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing import image UTC_8 = pytz.timezone('Asia/Shanghai') #cache func @st.cache_resource def load_my_model(): model = load_model("model/zha2024_6.h5") return model my_model = load_my_model() target_size = (300, 300) class_labels = {0: '炭黑组', 1: '正常发挥', 2: '炫彩组', 3: '糊糊组', 4: '炸组日常', 5: '凡尔赛',6: '非食物'} predicted_class='' #Set up the Gemini model and API key #https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/gemini?hl=zh-cn MY_KEY= os.environ.get("GOOGLE_API_KEY") genai.configure(api_key=MY_KEY) gemini_model = genai.GenerativeModel('gemini-pro-vision') neutral=os.environ.get("sys_info_0") toxic=os.environ.get("sys_info_1") heartfelt=os.environ.get("sys_info_2") chilly_list=os.environ.get("X").split(",") default_prompt='' generation_config = { "temperature": 0.99, "top_p": 1, "top_k": 40, "max_output_tokens": 2048, "candidate_count":1 } safety_settings = [ { "category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE" }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE" } ] #functions @st.cache_data def preprocess_image_cached(img_path, target_size): img = image.load_img(img_path, target_size=target_size) img_array = image.img_to_array(img) img_array = np.expand_dims(img_array, axis=0) return img_array def chilly_words_killer(text,words_list): for word in words_list: pattern = re.compile(re.escape(word), re.IGNORECASE) text = pattern.sub("**😮", text) return text def get_critic_info(review_style): if review_style == '默认': default_prompt = neutral critic_name = 'SavorBalancer' avatar = '👩🍳' elif review_style == '毒舌👾': default_prompt = toxic critic_name = 'SpicyCritique' avatar = '😈' elif review_style == '暖心🍀': default_prompt = heartfelt critic_name = 'GentleGourmet' avatar = '🤗' else: raise ValueError(f'Invalid review style: {review_style}') return default_prompt, critic_name, avatar def img_score(img_raw_path): global predicted_class img_array = preprocess_image_cached(img_raw_path, target_size) predictions = my_model.predict(img_array) predicted_class_index = np.argmax(predictions, axis=-1) predicted_class = class_labels[predicted_class_index[0]] class_probabilities = {label: prob for label, prob in zip(class_labels.values(), predictions[0])} score={k: round(v * 100, 2) for k, v in class_probabilities.items()} high_score_float=predictions[0,(predicted_class_index[0])] high_score=round(high_score_float*100,2) return score,high_score def score_desc(score): if score > 90: return "这妥妥的属于" elif score >= 70 and score <= 90: return "这大概率属于" elif score >= 40 and score <= 70: return "这可能属于" else: return "我猜这属于" def review_waiting(_class, critic_name): if _class == '非食物': return '图里面好像没有食物吧❓点评可能会说些无关的' elif critic_name == 'SavorBalancer': return '🍴品尝中,正在构思点评' elif critic_name == 'SpicyCritique': return '不要催啦,我这不正在吃吗💢' elif critic_name == 'GentleGourmet': return '正在为你种彩虹🌈' def gemini_bot(default_prompt,img_raw_path,_class): img = PIL.Image.open(img_raw_path) model = gemini_model klass="当前食物类型是:"+_class prompt=klass+default_prompt response = model.generate_content([prompt, img], stream=False, safety_settings=safety_settings, generation_config=generation_config) response.resolve() response_text=f'''{response.text}''' final_response=chilly_words_killer(response_text,chilly_list) return final_response def review(): if predicted_class is not None: with st.spinner(review_waiting(predicted_class, critic_name)): print(f"{datetime.now(UTC_8).strftime('%m-%d %H:%M:%S')}--Start Reviewing") final_response = gemini_bot(default_prompt, img_raw_path, predicted_class) with st.chat_message(critic_name, avatar=avatar): st.write(final_response) st.button("再次点评", key="1") print(f"{datetime.now(UTC_8).strftime('%m-%d %H:%M:%S')}--Complete\n💣💣💣") info('#edfde2','#78817a','🆗点评完毕,内容由AI生成,仅供娱乐',55) def info(bg_color,font_color,text,height): html=f'''