cloud-sean commited on
Commit
c189563
·
verified ·
1 Parent(s): 69585b8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import streamlit as st
3
+ import docx2txt
4
+ import openai
5
+ import os
6
+
7
+ os.environ["OPENAI_API_BASE"] = openai.api_type = "azure"
8
+ os.environ["OPENAI_API_KEY"] = openai.api_key = os.environ["OPENAI_API_KEY"]
9
+ os.environ["OPENAI_API_BASE"] = openai.api_base = os.environ["OPENAI_API_BASE"]
10
+ os.environ["OPENAI_API_VERSION"] = openai.api_version = "2023-03-15-preview"
11
+
12
+ def translate(text, language):
13
+ message_text = [{"role":"system","content":f"You are an AI translator that translates documents to {language}. Do not put anything before or after the text you want to translate. Just return the translated text. At the end of your translation generate a table of important information that could be helpful for quick identification by a human (this section should be in english).: \n \n Text to Translate: {text}"}]
14
+ # Create a completion request to Azure OpenAI
15
+ completion = openai.ChatCompletion.create(
16
+ engine="gpt-4-1106-preview",
17
+ messages = message_text,
18
+ temperature=0,
19
+ max_tokens=4000,
20
+ top_p=0.95,
21
+ frequency_penalty=0,
22
+ presence_penalty=0,
23
+ stop=None
24
+ )
25
+ return(completion.choices[0].message.content)
26
+
27
+ st.title("Joe's Translation Demo")
28
+
29
+ file = st.file_uploader("Upload a file", type=["docx"])
30
+ language = st.text_input("Enter the language you want to translate to", "Spanish")
31
+ if st.button("Translate"):
32
+ if file is not None:
33
+ file_details = {"FileName":file.name,"FileType":file.type,"FileSize":file.size}
34
+ # st.write(file_details)
35
+ text = docx2txt.process(file)
36
+ with st.spinner('Translating...'):
37
+ st.write(translate(text, language))
38
+