|
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"] |
|
} |
|
}) |
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
@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) |
|
|
|
|
|
@app.route('/favicon.ico') |
|
def favicon(): |
|
return '', 204 |
|
|
|
@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} |
|
|
|
=== |
|
μ€λμ μΌκΈ°: |
|
μ€λμ """ |
|
|
|
|
|
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) |
|
|