Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import pytesseract
|
| 5 |
+
import openai
|
| 6 |
+
from openai import OpenAI
|
| 7 |
+
import subprocess
|
| 8 |
+
|
| 9 |
+
# ========== ✅ 设置 GPT Key ==========
|
| 10 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# ========== ✅ 页面设置 ==========
|
| 14 |
+
st.set_page_config(page_title="AI Cold Email Generator", layout="centered")
|
| 15 |
+
st.title("📬 AI Cold Email Generator")
|
| 16 |
+
st.write("上传两位 LinkedIn 截图(每人可多张),系统将识别并生成 Coffee Chat 邀请邮件")
|
| 17 |
+
|
| 18 |
+
# ========== ✅ 函数区 ==========
|
| 19 |
+
|
| 20 |
+
def extract_text_from_images(uploaded_files):
|
| 21 |
+
"""从多张图片中提取文本"""
|
| 22 |
+
all_text = ""
|
| 23 |
+
for uploaded_file in uploaded_files:
|
| 24 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 25 |
+
text = pytesseract.image_to_string(image)
|
| 26 |
+
all_text += text + "\n"
|
| 27 |
+
return all_text.strip()
|
| 28 |
+
|
| 29 |
+
def generate_email(profile1_text, profile2_text):
|
| 30 |
+
"""调用 GPT 生成邮件"""
|
| 31 |
+
prompt = f"""
|
| 32 |
+
根据以下两人的 LinkedIn Profile,提取共同点,并生成一封自然、有温度的 Coffee Chat 邀请邮件(避免模板化):
|
| 33 |
+
|
| 34 |
+
📌 Profile 1:
|
| 35 |
+
{profile1_text}
|
| 36 |
+
|
| 37 |
+
📌 Profile 2:
|
| 38 |
+
{profile2_text}
|
| 39 |
+
|
| 40 |
+
✉️ 邮件内容如下:
|
| 41 |
+
"""
|
| 42 |
+
response = client.chat.completions.create(
|
| 43 |
+
model="gpt-4",
|
| 44 |
+
messages=[
|
| 45 |
+
{"role": "system", "content": "你是一位 AI Networking 邮件助手,擅长生成 Coffee Chat 邮件"},
|
| 46 |
+
{"role": "user", "content": prompt}
|
| 47 |
+
],
|
| 48 |
+
temperature=0.7,
|
| 49 |
+
max_tokens=600
|
| 50 |
+
)
|
| 51 |
+
return response.choices[0].message.content.strip()
|
| 52 |
+
|
| 53 |
+
def check_tesseract():
|
| 54 |
+
"""调试用:查看 OCR 是否安装成功"""
|
| 55 |
+
try:
|
| 56 |
+
version = subprocess.check_output(["tesseract", "--version"])
|
| 57 |
+
return version.decode()
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return str(e)
|
| 60 |
+
|
| 61 |
+
# ========== ✅ 页面上传区 ==========
|
| 62 |
+
|
| 63 |
+
col1, col2 = st.columns(2)
|
| 64 |
+
with col1:
|
| 65 |
+
files1 = st.file_uploader("上传你的 LinkedIn 截图(支持多张)", type=["png", "jpg", "jpeg"], accept_multiple_files=True, key="profile1")
|
| 66 |
+
with col2:
|
| 67 |
+
files2 = st.file_uploader("上传目标联系人的截图(支持多张)", type=["png", "jpg", "jpeg"], accept_multiple_files=True, key="profile2")
|
| 68 |
+
|
| 69 |
+
if st.button("🚀 生成邮件"):
|
| 70 |
+
if files1 and files2:
|
| 71 |
+
with st.spinner("正在识别图像并生成邮件..."):
|
| 72 |
+
profile1_text = extract_text_from_images(files1)
|
| 73 |
+
profile2_text = extract_text_from_images(files2)
|
| 74 |
+
email = generate_email(profile1_text, profile2_text)
|
| 75 |
+
st.success("✅ 邮件生成成功")
|
| 76 |
+
st.text_area("📨 Coffee Chat 邮件内容", email, height=300)
|
| 77 |
+
else:
|
| 78 |
+
st.warning("请上传双方的截图")
|
| 79 |
+
|
| 80 |
+
# ========== ✅ OCR 安装测试区 ==========
|
| 81 |
+
with st.expander("🛠️ 查看 Tesseract 安装状态(调试用)"):
|
| 82 |
+
result = check_tesseract()
|
| 83 |
+
if "tesseract" in result.lower():
|
| 84 |
+
st.success("✅ Tesseract 安装成功")
|
| 85 |
+
st.code(result, language="bash")
|
| 86 |
+
else:
|
| 87 |
+
st.error("❌ Tesseract 未安装")
|
| 88 |
+
st.text(result)
|