File size: 4,027 Bytes
8b7b432 |
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
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)
|