Chapter 5 - I read ancient and modern poems and songs just to be able to write poems for you

Five chapters refers to five character poems. Tang Dynasty Li Bai's "Song of Lao Lao Pavilion": "I heard Niu Zhu chanting five chapters in the past, how can I thank Yuan Jialang now?" The five chapters APP has collected hundreds of thousands of ancient poems, Tang poetry, Song lyrics, Yuan music, and Ming and Qing poems, etc. No, it also has a built-in poetry writing engine, which can help you compose impromptu poetry, compose poems based on propositions, etc. This product was developed to rekindle everyone's love for poetry. Let me explain in detail the production process of this product.

Technology stack and selection

Front-end: requires cross-platform, support for small programs, cross-end framework comparison, choose uniapp. In order to develop quickly, the paid graceui is selected as the interface framework.

Backend: The backend is mainly divided into two parts. One is the addition, deletion, modification and query of poetry data. This is implemented through cloud functions, and the data is mainly stored on elasticsearch.

Algorithm: Deep neural networks are used to write poems and couplets, and tensorflow is used for traning and inference. Model inference requires a lot of resources and is not suitable for serverless (cloud bills will soar), so I purchased a discounted server from Tencent Cloud and installed Flask. Specializes in model inference.

API gateway: Models and search APIs are both accessed through the API gateway. This API gateway is also a very tricky thing. The products made by major cloud vendors have too many flaws: pay as you go (not cost-effective), and you can only add APIs manually. It only supports a few hundred APIs, and the backend is connected to multiple servers, and a load balancing must be set up between the gateway and the server (load balancing requires pay-as-you-go). So I was so angry that I wrote an API gateway myself, which solved all these problems. I also customized many new functions, such as automatic cache, IP speed limit, automatic log, etc. I will introduce it in detail later when I have time.

Design and implementation

system structure

picture

User requests will first be routed through the self-developed API gateway. The API gateway has many functions, such as request caching, writing AccessLog, Bot check, Auth, access speed limit, etc. SCF is responsible for lightweight database queries. The poetry writing model actually I did not use SCF, but used a discounted cloud server. Because the resource consumption was too high, it was not cost-effective to use SCF.

API design

picture

poetry creation model

A combined model is used. The basic model is to encode all the previous words and find the n-th word with the highest probability based on the previous n-1 words. It also combines large models such as GPT and BERT, so the poems written in this way will indeed be of higher quality.

Full Text Search

picture

Currently, a 4-node cluster is built, and the memory configuration is above 8G. After installing the smartcn plug-in, all poetry documents are indexed. When querying, simply do a word segmentation, and then organize the keywords according to or. The following is an example of query.

curl -v -XPOST 'http://127.0.0.1:9200/poem/_search' -d '{"from":0,"query":{"query_string":{"default_operator":"and","query":"李白"}},"size":10}' -H "Content-Type: application/json"

Poetry recommendations

Currently, a common approach is adopted: tag matching. That is, the user is tagged according to static rules, and then matched with the tag of the poem itself, and those with a high degree of matching are recommended. The general idea is to enter all static rules into the database, and then use the rules engine to evaluate each rule for each user. For example, if there is a rule: PoemDate.Hour == 6 || PoemDate.Hour == 7, add a label "sunrise". When the user requests it, just set PoemDate to the current time, and then find the value of this rule Just value it. In human terms, if today is summer, it will recommend you poems describing summer; if you live in the south, it will recommend poems describing Jiangnan.

Recommended page production

I have to mention that to make the recommended homepage, I used the code generated by ChatGPT, because I am not an artist, and the pages I made in the past were very ugly, so this time I tried the CSS generated by ChatGPT. The prompt I used is:

use vuejs to format  a poem which has title  author  and content ,  the page need to be beautiful

It generated a PoemDisplay.vue for me, which seemed to work fine.

picture

APP operation

Currently, only the WeChat applet is available. If you search for "Five Chapters", the first one should be me. I have also been reading the book Growth Hacking recently, and then applied what I learned. I firmly believe that everything from 0 to 1 has come out, and from 1 Getting to 100 should be a matter of method.

Guess you like

Origin blog.csdn.net/quanzan/article/details/134903109