cloud-sean commited on
Commit
fc36b1f
·
verified ·
1 Parent(s): c90ffd3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import openai
3
+ import os
4
+
5
+ from openai import OpenAI
6
+ client = OpenAI(api_key=os.getenv('OPENAI_API_KEY')
7
+
8
+ st.title('Marketing Content Translation!')
9
+
10
+
11
+ # Text input for translation
12
+ marketing_text = st.text_area('Enter the text to be translated', 'Hello, World!')
13
+
14
+ # Dropdown for selecting target language
15
+ target_language = st.selectbox('Select the target language', ['Spanish', 'French', 'Hindi', 'Other'])
16
+
17
+
18
+
19
+ if target_language == 'Other':
20
+ other_language = st.text_input('Enter the target language', 'Japanese')
21
+ target_language = other_language
22
+
23
+
24
+ # Button to trigger translation
25
+ translate_submit = st.button('Translate')
26
+
27
+ def stream_translation(text, target_language):
28
+ """ Generator that streams the translation response. """
29
+ prompt = f'Translate the following text to {target_language}: \n{text}'
30
+ response = client.chat.completions.create(
31
+ model="gpt-4-turbo",
32
+ messages=[
33
+ {"role": "user", "content": prompt}
34
+ ],
35
+ temperature=1,
36
+ max_tokens=2000,
37
+ top_p=1,
38
+ frequency_penalty=0,
39
+ presence_penalty=0,
40
+ stream=True # Enable streaming
41
+ )
42
+ for chunk in response:
43
+ if chunk.choices[0].delta and chunk.choices[0].delta.content:
44
+ yield chunk.choices[0].delta.content
45
+
46
+ if translate_submit:
47
+ # Using st.write_stream to display the streamed response
48
+ st.write_stream(stream_translation(marketing_text, target_language))