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)