my-diary / backend /app.py
Ping9gu's picture
Initial commit
8b7b432
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
from huggingface_hub import InferenceClient
import os
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
CORS(app, resources={
r"/*": {
"origins": ["http://your-frontend-domain"],
"methods": ["GET", "POST", "OPTIONS"],
"allow_headers": ["Content-Type", "Accept"]
}
})
# Hugging Face ν΄λΌμ΄μ–ΈνŠΈ μ΄ˆκΈ°ν™”
client = InferenceClient(
model=os.getenv("MODEL_ID"),
token=os.getenv("HUGGINGFACE_API_KEY")
)
# 더 큰 ν•œκ΅­μ–΄ λͺ¨λΈλ‘œ λ³€κ²½
model_name = "nlpai-lab/kullm-polyglot-5.8b-v2" # 더 λ‚˜μ€ ν’ˆμ§ˆμ˜ λͺ¨λΈ
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
device_map="auto",
torch_dtype=torch.float16,
low_cpu_mem_usage=True
)
# device_map="auto"λ₯Ό μ‚¬μš©ν•˜λ―€λ‘œ .to(device) 호좜 제거
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# model = model.to(device) # 이 쀄 제거
# 루트 경둜 ν•Έλ“€λŸ¬ μΆ”κ°€
@app.route('/')
def serve_frontend():
return send_from_directory('../frontend', 'index.html')
# 정적 파일 μ„œλΉ™μ„ μœ„ν•œ 라우트 μΆ”κ°€
@app.route('/<path:path>')
def serve_static(path):
return send_from_directory('../frontend', path)
# favicon.ico ν•Έλ“€λŸ¬ μΆ”κ°€
@app.route('/favicon.ico')
def favicon():
return '', 204 # No Content 응닡 λ°˜ν™˜
@app.route('/api/generate-diary', methods=['POST'])
def generate_diary():
try:
data = request.json
if not data or 'keywords' not in data:
return jsonify({"error": "ν‚€μ›Œλ“œκ°€ ν•„μš”ν•©λ‹ˆλ‹€"}), 400
keywords = data.get('keywords', '').strip()
if not keywords:
return jsonify({"error": "ν‚€μ›Œλ“œκ°€ λΉ„μ–΄μžˆμŠ΅λ‹ˆλ‹€"}), 400
prompt = f"""λ‹€μŒμ€ 였늘 μžˆμ—ˆλ˜ 일의 μš”μ•½μž…λ‹ˆλ‹€. 이것을 λ°”νƒ•μœΌλ‘œ μƒμƒν•˜κ³  감동적인 일기λ₯Ό μž‘μ„±ν•΄μ£Όμ„Έμš”.
[상세 μš”κ΅¬μ‚¬ν•­]
1. λ„μž…λΆ€:
- κ·Έλ‚ μ˜ λ‚ μ”¨λ‚˜ λΆ„μœ„κΈ°λ‘œ μ‹œμž‘
- 상황과 λ“±μž₯인물 μ†Œκ°œ
2. μ „κ°œ:
- ꡬ체적인 λŒ€ν™”μ™€ 행동 λ¬˜μ‚¬
- μ˜€κ°μ„ μ‚¬μš©ν•œ μž₯λ©΄ λ¬˜μ‚¬
- λ“±μž₯μΈλ¬Όλ“€μ˜ ν‘œμ •κ³Ό 감정 λ³€ν™”
3. 감정과 생각:
- λ‚΄λ©΄μ˜ 감정을 μ„¬μ„Έν•˜κ²Œ ν‘œν˜„
- 사건에 λŒ€ν•œ λ‚˜μ˜ 생각과 κΉ¨λ‹¬μŒ
- λ‹€λ₯Έ μ‚¬λžŒλ“€μ˜ 감정에 λŒ€ν•œ 곡감
4. 문체:
- 문어체와 ꡬ어체λ₯Ό 적절히 혼용
- λΉ„μœ μ™€ μ€μœ λ₯Ό ν™œμš©ν•œ ν‘œν˜„
- λ°˜λ³΅μ„ ν”Όν•˜κ³  λ‹€μ–‘ν•œ μ–΄νœ˜ μ‚¬μš©
5. 마무리:
- κ·Έλ‚ μ˜ κ²½ν—˜μ΄ μ£ΌλŠ” 의미
- μ•žμœΌλ‘œμ˜ κΈ°λŒ€λ‚˜ 닀짐
μš”μ•½:
{keywords}
===
였늘의 일기:
μ˜€λŠ˜μ€ """
# Hugging Face APIλ₯Ό ν†΅ν•œ ν…μŠ€νŠΈ 생성
parameters = {
"max_new_tokens": 768,
"temperature": 0.88,
"top_p": 0.95,
"repetition_penalty": 1.35,
"top_k": 50,
"do_sample": True,
"num_return_sequences": 1
}
response = client.text_generation(
prompt,
**parameters
)
if not response:
return jsonify({"error": "일기 생성에 μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€"}), 500
# ν”„λ‘¬ν”„νŠΈ μ œκ±°ν•˜κ³  μƒμ„±λœ ν…μŠ€νŠΈλ§Œ λ°˜ν™˜
diary_content = response.split("μ˜€λŠ˜μ€ ")[-1].strip()
diary_content = "μ˜€λŠ˜μ€ " + diary_content
return jsonify({"diary": diary_content})
except Exception as e:
print(f"Error generating diary: {str(e)}")
return jsonify({"error": f"일기 생성 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"}), 500
if __name__ == '__main__':
app.run(debug=True)