|
|
from flask import Flask, render_template, request, jsonify |
|
|
import requests |
|
|
import os |
|
|
|
|
|
from flask_cors import CORS |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
CORS(app) |
|
|
|
|
|
HF_MODEL = "google/gemma-2b" |
|
|
HF_API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}" |
|
|
|
|
|
|
|
|
HF_API_KEY = os.getenv("HF_API_KEY") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def chat_with_gemma(prompt): |
|
|
if not HF_API_KEY: |
|
|
return "Lỗi: API Key không tồn tại. Hãy thêm vào Hugging Face Secrets." |
|
|
|
|
|
response = requests.post( |
|
|
HF_API_URL, |
|
|
headers={"Authorization": f"Bearer {HF_API_KEY}"}, |
|
|
json={"inputs": prompt} |
|
|
) |
|
|
|
|
|
if response.status_code == 200: |
|
|
return response.json()[0].get('generated_text', "Không có phản hồi.") |
|
|
return f"Lỗi: {response.status_code} - {response.text}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/') |
|
|
def index(): |
|
|
return jsonify({'FROM': 'Gemma-2B', 'MESSAGE': bot_reply}) |
|
|
|
|
|
if __name__ == '__main__': |
|
|
app.run(host="0.0.0.0", port=7860) |
|
|
|