Spaces:
Sleeping
Sleeping
import streamlit as st | |
import docx2txt | |
import openai | |
import os | |
os.environ["OPENAI_API_BASE"] = openai.api_type = "azure" | |
os.environ["OPENAI_API_KEY"] = openai.api_key = os.environ["OPENAI_API_KEY"] | |
os.environ["OPENAI_API_BASE"] = openai.api_base = os.environ["OPENAI_API_BASE"] | |
os.environ["OPENAI_API_VERSION"] = openai.api_version = "2023-03-15-preview" | |
def translate(text, language): | |
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}"}] | |
# Create a completion request to Azure OpenAI | |
completion = openai.ChatCompletion.create( | |
engine="gpt-4-1106-preview", | |
messages = message_text, | |
temperature=0, | |
max_tokens=4000, | |
top_p=0.95, | |
frequency_penalty=0, | |
presence_penalty=0, | |
stop=None | |
) | |
return(completion.choices[0].message.content) | |
st.title("Joe's Translation Demo") | |
file = st.file_uploader("Upload a file", type=["docx"]) | |
language = st.text_input("Enter the language you want to translate to", "Spanish") | |
if st.button("Translate"): | |
if file is not None: | |
file_details = {"FileName":file.name,"FileType":file.type,"FileSize":file.size} | |
# st.write(file_details) | |
text = docx2txt.process(file) | |
with st.spinner('Translating...'): | |
st.write(translate(text, language)) | |