File size: 3,105 Bytes
89fbbdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import streamlit as st
from PIL import Image
import pytesseract
import openai
from openai import OpenAI
import subprocess

# ========== ✅ 设置 GPT Key ==========
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))


# ========== ✅ 页面设置 ==========
st.set_page_config(page_title="AI Cold Email Generator", layout="centered")
st.title("📬 AI Cold Email Generator")
st.write("上传两位 LinkedIn 截图(每人可多张),系统将识别并生成 Coffee Chat 邀请邮件")

# ========== ✅ 函数区 ==========

def extract_text_from_images(uploaded_files):
    """从多张图片中提取文本"""
    all_text = ""
    for uploaded_file in uploaded_files:
        image = Image.open(uploaded_file).convert("RGB")
        text = pytesseract.image_to_string(image)
        all_text += text + "\n"
    return all_text.strip()

def generate_email(profile1_text, profile2_text):
    """调用 GPT 生成邮件"""
    prompt = f"""
    根据以下两人的 LinkedIn Profile,提取共同点,并生成一封自然、有温度的 Coffee Chat 邀请邮件(避免模板化):

    📌 Profile 1:
    {profile1_text}

    📌 Profile 2:
    {profile2_text}

    ✉️ 邮件内容如下:
    """
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "你是一位 AI Networking 邮件助手,擅长生成 Coffee Chat 邮件"},
            {"role": "user", "content": prompt}
        ],
        temperature=0.7,
        max_tokens=600
    )
    return response.choices[0].message.content.strip()

def check_tesseract():
    """调试用:查看 OCR 是否安装成功"""
    try:
        version = subprocess.check_output(["tesseract", "--version"])
        return version.decode()
    except Exception as e:
        return str(e)

# ========== ✅ 页面上传区 ==========

col1, col2 = st.columns(2)
with col1:
    files1 = st.file_uploader("上传你的 LinkedIn 截图(支持多张)", type=["png", "jpg", "jpeg"], accept_multiple_files=True, key="profile1")
with col2:
    files2 = st.file_uploader("上传目标联系人的截图(支持多张)", type=["png", "jpg", "jpeg"], accept_multiple_files=True, key="profile2")

if st.button("🚀 生成邮件"):
    if files1 and files2:
        with st.spinner("正在识别图像并生成邮件..."):
            profile1_text = extract_text_from_images(files1)
            profile2_text = extract_text_from_images(files2)
            email = generate_email(profile1_text, profile2_text)
            st.success("✅ 邮件生成成功")
            st.text_area("📨 Coffee Chat 邮件内容", email, height=300)
    else:
        st.warning("请上传双方的截图")

# ========== ✅ OCR 安装测试区 ==========
with st.expander("🛠️ 查看 Tesseract 安装状态(调试用)"):
    result = check_tesseract()
    if "tesseract" in result.lower():
        st.success("✅ Tesseract 安装成功")
        st.code(result, language="bash")
    else:
        st.error("❌ Tesseract 未安装")
        st.text(result)