-
Notifications
You must be signed in to change notification settings - Fork 0
/
boketalk-old.py
47 lines (40 loc) · 1.46 KB
/
boketalk-old.py
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
import streamlit as st
import openai
from langdetect import detect
# API key is obtained from the Streamlit secrets
openai.api_key = st.secrets["OPENAI_API_KEY"]
def detect_language(text):
return detect(text)
def translate(text, target_language):
response = openai.Completion.create(
engine="text-davinci-002",
prompt=f"Translate the following {'English' if target_language == 'ja' else 'Japanese'} text to {'Japanese/日本語でお願いします。' if target_language == 'ja' else 'English'}:\n{text}",
max_tokens=60
)
return response.choices[0].text.strip()
def generate_joke(text):
response = openai.Completion.create(
engine="text-davinci-002",
prompt=f"Create a funny joke based on this text:\n{text}",
max_tokens=60
)
return response.choices[0].text.strip()
st.title("TOKYO Boke Talk/東京ボケトーク")
st.markdown(
"""
-- ver.0.1 -- 2023/07/08 [@hortense667](https://twitter.com/hortense667)
"""
)
text = st.text_input("Enter some text")
if st.button("Translate and Generate Joke"):
if text:
target_language = 'ja' if detect_language(text) == 'en' else 'en'
translation = translate(text, target_language)
joke = generate_joke(text)
st.write("Translation:", translation)
st.write("Joke:", joke)
else:
st.write("Please enter some text.")
if st.button("Clear"):
text = ""
st.empty()