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
| import pathlib import textwrap import mysql.connector import sys import os import google.generativeai as genai import time from config import import_env
from random import randint
import_env()
# Used to securely store your API key # from google.colab import userdata
from IPython.display import display from IPython.display import Markdown
import redis
genai.configure(api_key=os.environ.get('GOOGLE_API_KEY'))
def search_from_google_ai(text): model = genai.GenerativeModel('gemini-pro') response = model.generate_content(text) print(response.text) return response.text
db = mysql.connector.connect( host=os.environ.get('DB_HOST'), user=os.environ.get('DB_USERNAME'), password=os.environ.get('DB_PASSWORD'), database=os.environ.get('DB_DATABASE') )
r = redis.Redis(host=os.environ.get('REDIS_HOST'), port=os.environ.get('REDIS_PORT'), db=os.environ.get('REDIS_DB'), password=os.environ.get('REDIS_PASSWORD'), decode_responses=True, protocol=3)
# r.connection_pool.disconnect()
cursor = db.cursor()
cursor.execute("SELECT name,isbn FROM fetchobj where repeated > 0 order by update_time desc limit 100")
myresult = cursor.fetchall()
for x in myresult: redisCache = r.get(x[1]+"_ai") if(r.exists(x[1]+"_ai")==False): text = "书名:"+ x[0] +",isbn:"+ x[1] +",请把书名翻译成中文,并帮我找出这本书的作者及作者介绍,Genres,系列书名及系列名对应isbn号,详细内容同时告知这本书主题思想,适合几岁阅读,蓝思指数,妈妈通过阅读知识点更好的帮助孩子阅读,是否有音频链接,请有序输出" print(text) result = search_from_google_ai(text) r.set(x[1]+"_ai", result) time.sleep(randint(10,100))
#sys.exit(0)
def to_markdown(text): text = text.replace('•', ' *') return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
# model = genai.GenerativeModel('gemini-pro')
# models/gemini-pro # models/gemini-pro-vision # for m in genai.list_models(): # if 'generateContent' in m.supported_generation_methods: # print(m.name)
# response = model.generate_content("书名:Houghton Mifflin Reading,isbn:9720618089587,请把书名翻译成中文,并帮我找出这本书的作者及作者介绍,Genres,系列书名及系列名对应isbn号,详细内容同时告知这本书主题思想,适合几岁阅读,蓝思指数,妈妈通过阅读知识点更好的帮助孩子阅读,是否有音频链接。")
# print(response.text)
|