Spaces:
Runtime error
Runtime error
File size: 3,249 Bytes
2f1b753 |
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 |
# Import necessary libraries
import streamlit as st
import os
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.chains import LLMChain, SequentialChain
# Function to translate and summarize the email
def translate_and_summarize(api_key: str, email: str) -> dict:
os.environ['OPENAI_API_KEY'] = api_key # Set the OpenAI API key as an environment variable
llm = ChatOpenAI()
# Identify the email's language
template1 = "Return the language this email is written in:\n{email}.\nONLY return the language it was written in."
prompt1 = ChatPromptTemplate.from_template(template1)
chain_1 = LLMChain(llm=llm, prompt=prompt1, output_key="language")
# Translate the email to English
template2 = "Translate this email from {language} to English. Here is the email:\n" + email
prompt2 = ChatPromptTemplate.from_template(template2)
chain_2 = LLMChain(llm=llm, prompt=prompt2, output_key="translated_email")
# Provide a summary in English
template3 = "Create a short summary of this email:\n{translated_email}"
prompt3 = ChatPromptTemplate.from_template(template3)
chain_3 = LLMChain(llm=llm, prompt=prompt3, output_key="summary")
# Provide a reply in English
template4 = "Reply to the sender of the email giving a plausible reply based on the {summary} and a promise to address issues"
prompt4 = ChatPromptTemplate.from_template(template4)
chain_4 = LLMChain(llm=llm, prompt=prompt4, output_key="reply")
# Provide a translation back to the original language
template5 = "Translate the {reply} back to the original {language} of the email."
prompt5 = ChatPromptTemplate.from_template(template5)
chain_5 = LLMChain(llm=llm, prompt=prompt5, output_key="translated_reply")
seq_chain = SequentialChain(chains=[chain_1, chain_2, chain_3, chain_4, chain_5],
input_variables=['email'],
output_variables=['language', 'translated_email', 'summary', 'reply', 'translated_reply'],
verbose=True)
return seq_chain(email)
def main():
st.title("Chain Example: Language, Summary, Translate, Respond & Translate")
st.write("Translates an email to English, provides a summary, plausible reply and translates back to the sender")
api_key = st.text_input("Enter your OpenAI API Key:", type="password") # Set as a password input
email = st.text_area("Enter the email to translate and summarize:")
if st.button("Translate"):
if api_key and email:
try:
result = translate_and_summarize(api_key, email)
st.write(f"**Language:** {result['language']}")
st.write(f"**Summary:** {result['summary']}")
st.write(f"**Translated Email:** {result['translated_email']}")
st.write(f"**Reply in English:** {result['reply']}")
st.write(f"**Reply in Original Language:** {result['translated_reply']}")
except Exception as e:
st.write(f"Error: {e}")
else:
st.write("Please provide both the API Key and Email.")
if __name__ == "__main__":
main() |