swaption2009 commited on
Commit
fd28e56
1 Parent(s): bbc2a3d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/python
2
+ # -*- coding: utf-8 -*-
3
+ import os
4
+ import sys
5
+ import cohere
6
+ from streamlit.web import cli as stcli
7
+ from streamlit import runtime
8
+ import streamlit as st
9
+
10
+ COHERE_CLIENT=st.secrets["COHERE_CLIENT"]
11
+ COHERE_MODEL=st.secrets["COHERE_MODEL"]
12
+ BASE_PROMPT="Generate Mandarin by HSK level and English keywords\nenglish: light\nhsk: 1\nmandarin: 所有的灯都闪了一会儿。\npinyin: suǒ yǒu de dēng dōu shǎn le yī huìr。\n--\nenglish: window\nhsk: 1\nmandarin: 所有的窗户都用砖堵住了。\npinyin: suǒ yǒu de chuāng hu dōu yòng zhuān dǔ zhù le。\n--\nenglish: airplane\nhsk: 1\nmandarin: 一架飞机在机场着陆。\npinyin: yī jià fēi jī zài jī chǎng zhuó lù。\n--\nenglish: styles\nhsk: 2\nmandarin: 这些式样均可改动以适应个人不同的爱好.\npinyin: zhè xie shì yàng jūn kě gǎi dòng yǐ shì yìng gè rén bù tóng de ài hào.\n--\nenglish: food.\nhsk: 2\nmandarin: 他以自己的名字命名的冷冻食品成了名牌。\npinyin: tā yǐ zì jǐ de míng zi mìng míng de lěng dòng shí pǐn chéng le míng pái。\n--\nenglish: glasses.\nhsk: 2\nmandarin: 印刷字体太小,我不带眼镜就看不清。\npinyin: yìn shuà zì tǐ tài xiǎo, wǒ bù dài yǎn jìng jiù kàn bù qīng。\n--\nenglish: practice\nhsk: 3\nmandarin: 实践与理论一样重要,但是我们易于珍视后者而轻视前者。\npinyin: shí jiàn yù lǐ lùn yí yàng zhòng yào, dàn shì wǒ men yì yú zhēn shì hǒu zhě ér qīng shì qián zhě。\n--\nenglish: sluggish\nhsk: 3\nmandarin: 很多队员渐渐变得动作迟缓了。这支队所需的是一些新鲜血液。\npinyin: hěn duō duì yuán jiàn jiàn biàn de dòng zuò chí huǎn le。 zhè zhī duì suǒ xū de shì yī xiē xīn xiān xuè yè。\n--\nenglish: medicine\nhsk: 3\nmandarin: 医术在于,当大自然为病者医治疾病的一段期间内,逗得病人高兴。\npinyin: yī zhú zài yú, dàng dà zì rán wèi bìng zhě yī zhì jí bìng de yī duàn qī jiān nèi, dòu dé bìng rén gāo xìng。\n--\n--\nenglish: princess\nhsk: 4\nmandarin: 公主光彩照人地穿着一身雪白的新礼服来了。\npinyin: gōng zhǔ guāng cǎi zhào rén de chuān zhuó yī shēn xuě bái de xīn lǐ fú lái le。\n--\nenglish: parking space\nhsk: 4\nmandarin: 我认为他侵占我的停车位肯定是有意和我作对。\npinyin: wǒ rèn wéi tā qīn zhàn wǒ de tíng chē wèi kěn dìng shì yǒu yì huò wǒ zuò duì。\n--\nenglish: discrepancy\nhsk: 4\nmandarin: 这件事的两种说法有很大出入.\npinyin: zhè jiàn shì de liǎng zhòng shuō fa yǒu hěn dà chū rù.\n--\n"
13
+
14
+
15
+ def generate_texts(keyword, hsk):
16
+ print(f"keyword: {keyword} and hsk: {hsk}")
17
+ co = cohere.Client(COHERE_CLIENT)
18
+ response = co.generate(
19
+ model=COHERE_MODEL,
20
+ prompt=BASE_PROMPT + "english: {keyword}\nhsk: {hsk}\nmandarin:".format(keyword=keyword, hsk=hsk),
21
+ max_tokens=200,
22
+ temperature=0.5,
23
+ k=0,
24
+ p=0.75,
25
+ frequency_penalty=0.5,
26
+ presence_penalty=0.5,
27
+ stop_sequences=["--"],
28
+ return_likelihoods='NONE')
29
+
30
+ print('Prediction: {}'.format(response.generations[0].text))
31
+ return response.generations[0].text
32
+
33
+
34
+ def main():
35
+ st.title("Chinese Sentence Generator with English Keyword and HSK Level")
36
+
37
+ form = st.form(key="user_settings")
38
+ with form:
39
+ en_keyword = st.text_input("English Keyword",
40
+ placeholder="Enter an English keyword, eg. home, key, etc",
41
+ key = "en_keyword")
42
+ hsk_level = st.select_slider("HSK Level",
43
+ options=["1", "2", "3", "4"],
44
+ key = "hsk_level")
45
+
46
+ generate_button = form.form_submit_button("Generate Mandarin Sentence and Pinyin")
47
+
48
+ if generate_button:
49
+ cohere_txts = generate_texts(en_keyword, hsk_level)
50
+ st.markdown("##### " + "Generated Mandarin Sentence: ")
51
+ st.write(cohere_txts)
52
+
53
+ st.write("Check out the prediction on [Google Translate](https://www.google.com/search?q=google+translate&oq=google+translate&aqs=chrome.0.69i59j69i64j0i271l2j69i60.5320j0j1&sourceid=chrome&ie=UTF-8)")
54
+
55
+ if __name__ == "__main__":
56
+ if runtime.exists():
57
+ main()
58
+ else:
59
+ sys.argv = ["streamlit", "run", sys.argv[0]]
60
+ sys.exit(stcli.main())