jpnarayana commited on
Commit
f1f76d7
·
verified ·
1 Parent(s): f2ba315

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -13
app.py CHANGED
@@ -1,8 +1,10 @@
1
  import streamlit as st
2
- from transformers import T5Tokenizer, T5ForConditionalGeneration
3
- # Streamlit UI
4
  st.set_page_config(page_title="Email Writer with FLAN-T5", layout="centered")
5
- # Load model and tokenizer
 
 
6
  @st.cache_resource
7
  def load_model():
8
  model_name = "google/flan-t5-large"
@@ -12,27 +14,30 @@ def load_model():
12
 
13
  tokenizer, model = load_model()
14
 
15
-
16
  st.title("📧 AI Email Writer")
17
  st.write("Generate professional emails with the help of `google/flan-t5-large` model.")
18
 
19
- # Input box
20
  user_instruction = st.text_area("✍️ Enter your email instruction",
21
  placeholder="e.g. Write a polite follow-up email to a client who hasn’t responded in a week.",
22
  height=150)
23
 
24
  temperature = st.slider("🎯 Creativity (Temperature)", 0.1, 1.0, 0.7, 0.1)
25
- max_tokens = st.slider("📏 Max Tokens", 50, 512, 200, 10)
26
 
27
- # Generate email on button click
28
  if st.button("Generate Email"):
29
- if user_instruction.strip() == "":
30
- st.warning("Please enter an instruction.")
31
  else:
32
  with st.spinner("Generating email..."):
33
- inputs = tokenizer(user_instruction, return_tensors="pt").input_ids
34
- outputs = model.generate(inputs)
35
- email_output = tokenizer.decode(outputs[0])
 
 
 
 
 
36
  st.success("Here’s your generated email:")
37
  st.text_area("📨 Generated Email", value=email_output, height=200)
38
-
 
1
  import streamlit as st
2
+
3
+ # Must be the first Streamlit command
4
  st.set_page_config(page_title="Email Writer with FLAN-T5", layout="centered")
5
+
6
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
7
+
8
  @st.cache_resource
9
  def load_model():
10
  model_name = "google/flan-t5-large"
 
14
 
15
  tokenizer, model = load_model()
16
 
17
+ # Streamlit UI
18
  st.title("📧 AI Email Writer")
19
  st.write("Generate professional emails with the help of `google/flan-t5-large` model.")
20
 
21
+ # Input section
22
  user_instruction = st.text_area("✍️ Enter your email instruction",
23
  placeholder="e.g. Write a polite follow-up email to a client who hasn’t responded in a week.",
24
  height=150)
25
 
26
  temperature = st.slider("🎯 Creativity (Temperature)", 0.1, 1.0, 0.7, 0.1)
27
+ max_tokens = st.slider("📏 Max Output Tokens", 50, 512, 200, 10)
28
 
 
29
  if st.button("Generate Email"):
30
+ if not user_instruction.strip():
31
+ st.warning("Please enter a valid instruction.")
32
  else:
33
  with st.spinner("Generating email..."):
34
+ input_ids = tokenizer(user_instruction, return_tensors="pt").input_ids
35
+ output_ids = model.generate(
36
+ input_ids=input_ids,
37
+ max_new_tokens=max_tokens,
38
+ temperature=temperature,
39
+ do_sample=True
40
+ )
41
+ email_output = tokenizer.decode(output_ids[0], skip_special_tokens=True).strip()
42
  st.success("Here’s your generated email:")
43
  st.text_area("📨 Generated Email", value=email_output, height=200)