Spaces:
Runtime error
Runtime error
Commit
·
2f1b753
1
Parent(s):
5042040
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
import streamlit as st
|
3 |
+
import os
|
4 |
+
from langchain.chat_models import ChatOpenAI
|
5 |
+
from langchain.prompts import ChatPromptTemplate
|
6 |
+
from langchain.chains import LLMChain, SequentialChain
|
7 |
+
|
8 |
+
# Function to translate and summarize the email
|
9 |
+
def translate_and_summarize(api_key: str, email: str) -> dict:
|
10 |
+
os.environ['OPENAI_API_KEY'] = api_key # Set the OpenAI API key as an environment variable
|
11 |
+
llm = ChatOpenAI()
|
12 |
+
|
13 |
+
# Identify the email's language
|
14 |
+
template1 = "Return the language this email is written in:\n{email}.\nONLY return the language it was written in."
|
15 |
+
prompt1 = ChatPromptTemplate.from_template(template1)
|
16 |
+
chain_1 = LLMChain(llm=llm, prompt=prompt1, output_key="language")
|
17 |
+
|
18 |
+
# Translate the email to English
|
19 |
+
template2 = "Translate this email from {language} to English. Here is the email:\n" + email
|
20 |
+
prompt2 = ChatPromptTemplate.from_template(template2)
|
21 |
+
chain_2 = LLMChain(llm=llm, prompt=prompt2, output_key="translated_email")
|
22 |
+
|
23 |
+
# Provide a summary in English
|
24 |
+
template3 = "Create a short summary of this email:\n{translated_email}"
|
25 |
+
prompt3 = ChatPromptTemplate.from_template(template3)
|
26 |
+
chain_3 = LLMChain(llm=llm, prompt=prompt3, output_key="summary")
|
27 |
+
|
28 |
+
# Provide a reply in English
|
29 |
+
template4 = "Reply to the sender of the email giving a plausible reply based on the {summary} and a promise to address issues"
|
30 |
+
prompt4 = ChatPromptTemplate.from_template(template4)
|
31 |
+
chain_4 = LLMChain(llm=llm, prompt=prompt4, output_key="reply")
|
32 |
+
|
33 |
+
# Provide a translation back to the original language
|
34 |
+
template5 = "Translate the {reply} back to the original {language} of the email."
|
35 |
+
prompt5 = ChatPromptTemplate.from_template(template5)
|
36 |
+
chain_5 = LLMChain(llm=llm, prompt=prompt5, output_key="translated_reply")
|
37 |
+
|
38 |
+
|
39 |
+
seq_chain = SequentialChain(chains=[chain_1, chain_2, chain_3, chain_4, chain_5],
|
40 |
+
input_variables=['email'],
|
41 |
+
output_variables=['language', 'translated_email', 'summary', 'reply', 'translated_reply'],
|
42 |
+
verbose=True)
|
43 |
+
return seq_chain(email)
|
44 |
+
|
45 |
+
def main():
|
46 |
+
st.title("Chain Example: Language, Summary, Translate, Respond & Translate")
|
47 |
+
st.write("Translates an email to English, provides a summary, plausible reply and translates back to the sender")
|
48 |
+
|
49 |
+
api_key = st.text_input("Enter your OpenAI API Key:", type="password") # Set as a password input
|
50 |
+
email = st.text_area("Enter the email to translate and summarize:")
|
51 |
+
|
52 |
+
if st.button("Translate"):
|
53 |
+
if api_key and email:
|
54 |
+
try:
|
55 |
+
result = translate_and_summarize(api_key, email)
|
56 |
+
st.write(f"**Language:** {result['language']}")
|
57 |
+
st.write(f"**Summary:** {result['summary']}")
|
58 |
+
st.write(f"**Translated Email:** {result['translated_email']}")
|
59 |
+
st.write(f"**Reply in English:** {result['reply']}")
|
60 |
+
st.write(f"**Reply in Original Language:** {result['translated_reply']}")
|
61 |
+
except Exception as e:
|
62 |
+
st.write(f"Error: {e}")
|
63 |
+
else:
|
64 |
+
st.write("Please provide both the API Key and Email.")
|
65 |
+
|
66 |
+
if __name__ == "__main__":
|
67 |
+
main()
|