TeddyYao commited on
Commit
72bae22
·
verified ·
1 Parent(s): 1ec4f90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -5,19 +5,25 @@ import streamlit as st
5
  from PIL import Image
6
  import pytesseract
7
 
 
 
 
8
  # 初始化 OpenAI 客户端
9
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
10
 
11
- # OCR识别
12
- def extract_text_from_image(uploaded_file):
13
- image = Image.open(uploaded_file).convert("RGB")
14
- text = pytesseract.image_to_string(image)
15
- return text
 
 
 
16
 
17
- # 邮件生成函数
18
  def generate_email(profile1_text, profile2_text):
19
  prompt = f"""
20
- 请根据以下两人的 LinkedIn Profile,识别他们的共同点,并撰写一封自然、有礼貌的 Coffee Chat 邀请邮件,避免模板式语言。
21
 
22
  📌 Profile 1:
23
  {profile1_text}
@@ -40,28 +46,29 @@ def generate_email(profile1_text, profile2_text):
40
 
41
  return response.choices[0].message.content.strip()
42
 
43
- # 主界面
44
  def main():
45
  st.set_page_config(page_title="AI Cold Email Generator", layout="centered")
46
  st.title("📬 AI Cold Email Generator")
47
- st.write("上传两张 LinkedIn 截图,AI 自动识别并生成 Coffee Chat 邀请邮件。")
48
 
49
  col1, col2 = st.columns(2)
 
50
  with col1:
51
- file1 = st.file_uploader("上传你的 LinkedIn 截图", type=["png", "jpg", "jpeg"], key="profile1")
52
  with col2:
53
- file2 = st.file_uploader("上传目标联系人的截图", type=["png", "jpg", "jpeg"], key="profile2")
54
 
55
  if st.button("🚀 生成邮件"):
56
- if file1 and file2:
57
- with st.spinner("正在识别和撰写邮件..."):
58
- profile1_text = extract_text_from_image(file1)
59
- profile2_text = extract_text_from_image(file2)
60
  email = generate_email(profile1_text, profile2_text)
61
- st.success("✅ 邮件生成成功")
62
- st.text_area("📨 邮件内容", email, height=300)
63
  else:
64
- st.warning("请上传两张截图")
65
 
66
  if __name__ == "__main__":
67
  main()
 
5
  from PIL import Image
6
  import pytesseract
7
 
8
+ # 手动指定 tesseract 路径(非常关键)
9
+ pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
10
+
11
  # 初始化 OpenAI 客户端
12
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
13
 
14
+ # 将多张图片 OCR 提取为一个字符串
15
+ def extract_text_from_images(uploaded_files):
16
+ all_text = ""
17
+ for uploaded_file in uploaded_files:
18
+ image = Image.open(uploaded_file).convert("RGB")
19
+ text = pytesseract.image_to_string(image)
20
+ all_text += text + "\n"
21
+ return all_text.strip()
22
 
23
+ # GPT 生成邮件
24
  def generate_email(profile1_text, profile2_text):
25
  prompt = f"""
26
+ 请根据以下两人的 LinkedIn Profile,提取共同点,并撰写一封自然、有礼貌、不模板化的 Coffee Chat 邀请邮件。
27
 
28
  📌 Profile 1:
29
  {profile1_text}
 
46
 
47
  return response.choices[0].message.content.strip()
48
 
49
+ # Streamlit 前端
50
  def main():
51
  st.set_page_config(page_title="AI Cold Email Generator", layout="centered")
52
  st.title("📬 AI Cold Email Generator")
53
+ st.write("上传两个人的 LinkedIn 截图(每人可多图),AI 将识别并生成 Coffee Chat 邮件")
54
 
55
  col1, col2 = st.columns(2)
56
+
57
  with col1:
58
+ files1 = st.file_uploader("你的 LinkedIn 截图(支持多张)", type=["png", "jpg", "jpeg"], accept_multiple_files=True, key="profile1")
59
  with col2:
60
+ files2 = st.file_uploader("目标联系人的截图(支持多张)", type=["png", "jpg", "jpeg"], accept_multiple_files=True, key="profile2")
61
 
62
  if st.button("🚀 生成邮件"):
63
+ if files1 and files2:
64
+ with st.spinner("正在识别图像并撰写邮件..."):
65
+ profile1_text = extract_text_from_images(files1)
66
+ profile2_text = extract_text_from_images(files2)
67
  email = generate_email(profile1_text, profile2_text)
68
+ st.success("✅ 邮件生成成功!")
69
+ st.text_area("📨 Coffee Chat 邮件", email, height=300)
70
  else:
71
+ st.warning("请上传两个 Profile 的图像(至少一张)")
72
 
73
  if __name__ == "__main__":
74
  main()