-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
178 lines (150 loc) · 6.85 KB
/
app.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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# streamlit run mixed_language_app_openai.py
import streamlit as st
import openai
import re
import json
from PIL import Image
# import tomli
# Load configuration
with open('config.json', 'r', encoding='utf-8') as config_file:
config = json.load(config_file)
# Set up your OpenAI API key
openai.api_key = config['openai_api_key']
# # Load configuration
# with open('.streamlit/config.toml', 'rb') as config_file:
# config = tomli.load(config_file)
# # Set up your OpenAI API key
# openai.api_key = config['openai_api_key']
# # print(openai.api_key)
def detect_language(text):
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a language detection tool. Respond with only the language name."},
{"role": "user", "content": f"Detect the language of this text: '{text}'. Respond with only the language name from this list: {', '.join(config['supported_languages'])}"}
],
max_tokens=10,
temperature=0,
)
return response.choices[0].message.content.strip()
def translate_text(text, target_language):
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": f"You are a translator. Translate the following text to {target_language}."},
{"role": "user", "content": text}
],
max_tokens=1000,
temperature=0.0,
)
return response.choices[0].message.content.strip()
def convert_to_mixed_language(input_text, primary_language, target_language, model='gpt-4o-mini'):
system_prompt = f"""You are a language learning assistant. Convert the input text from {primary_language} into a mixed-language content where important vocabulary or phrases are in the {target_language} language, surrounded by curly braces {{}}.
Rules:
1. Identify words or phrases that are valuable for language learners to focus on.
2. Convert ONLY these identified words/phrases to the {target_language}.
3. Surround the translated {target_language} words/phrases with curly braces {{}}.
4. Ensure that ALL content within curly braces {{}} is in the {target_language}.
5. Maintain the original sentence structure and grammar for non-translated parts.
6. The text outside the curly braces should remain in the original language.
7. Ensure the mixed output is still readable and understandable.
For example:
If the target language is Chinese and the input is "I love to eat apples", a correct output would be:
"I {{喜欢}} to eat {{苹果}}."
An incorrect output would be:
"I {{love}} to eat {{apples}}." (because the words inside braces are not translated)
Remember: Always translate the content inside the curly braces to the target language.
"""
response = openai.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": input_text}
],
max_tokens=1000,
temperature=0.8,
)
mixed_language_output = response.choices[0].message.content.strip()
return mixed_language_output
def post_process_output(mixed_output, primary_language, target_language):
def translate_if_needed(match):
content = match.group(1)
detected_lang = detect_language(content)
if detected_lang != target_language:
return "{" + translate_text(content, target_language) + "}"
return match.group(0)
return re.sub(r'\{([^}]+)\}', translate_if_needed, mixed_output)
def main():
st.title("Learn-Vocabulary-in-Context")
"""
#### Click the [Convert] button and see the result!
"""
# Language selection
target_language = st.selectbox("Select the target language your are learning:", config['supported_languages'])
primary_language = st.selectbox("Select context language:", [lang for lang in config['supported_languages'] if lang != target_language])
# Input method selection
input_method = st.radio("Choose input method:", ["Example Sentences", "Custom Text"])
if input_method == "Custom Text":
input_text = st.text_area("Enter your text:", "")
else:
example_sentences = config['example_sentences'].get(primary_language, ["No examples available for this language."])
input_text = st.selectbox("Choose an example sentence:", example_sentences)
if st.button("Convert"):
if input_text:
detected_lang = detect_language(input_text)
if detected_lang != primary_language:
input_text = translate_text(input_text, primary_language)
st.info(f"Input text has been translated to {primary_language}:")
# st.write(input_text)
mixed_output = convert_to_mixed_language(input_text, primary_language, target_language)
mixed_output = post_process_output(mixed_output, primary_language, target_language)
st.subheader("Primary-Language Content:")
st.write(input_text)
st.subheader("Mixed-Language Output:")
st.write(mixed_output)
else:
st.warning("Please enter some text or select an example sentence.")
# Add a footer with the Venmo QR code
#st.markdown("<br/>")
st.markdown("---")
st.markdown("""
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">
<style>
.github-link {
text-decoration: none;
color: #333;
font-size: 1.2rem;
}
.github-link:hover {
color: #0366d6;
}
</style>
""", unsafe_allow_html=True)
col1, col2, col3 = st.columns(3)
with col1:
# Add GitHub link with icon
st.markdown("""
<a href="https://github.com/Wonderfon/Learn-Vocabulary-in-Context" target="_blank" class="github-link">
<i class="fab fa-github"></i> Source Code
</a>
""", unsafe_allow_html=True)
st.markdown("""
<a href="https://github.com/Wonderfon
" target="_blank" class="github-link">
<i class="fab fa-github"></i> My Github Profile
</a>
""", unsafe_allow_html=True)
st.markdown("""
<a href="https://notion-next-chi-eight-85.vercel.app/en?theme=simple
" target="_blank" class="github-link">
My Personal Website
</a>
""", unsafe_allow_html=True)
with col2:
st.markdown("###### Buy me an A100 -->_-->")
with col3:
# st.markdown("#### Support My Work")
st.image("venmo.png", width=150)
# run
if __name__ == "__main__":
main()