Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import pymongo
|
| 3 |
+
import requests
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
# MongoDB bağlantısı
|
| 9 |
+
mongo_client = pymongo.MongoClient('mongodb://localhost:27017/')
|
| 10 |
+
db = mongo_client['EgitimDatabase']
|
| 11 |
+
collection = db['test']
|
| 12 |
+
|
| 13 |
+
# Model API Endpoint
|
| 14 |
+
MODEL_API_URL = "https://api.example.com/model" # Hugging Face API URL veya başka bir model URL'si
|
| 15 |
+
MODEL_API_KEY = "YOUR_API_KEY" # API anahtarınız
|
| 16 |
+
|
| 17 |
+
def get_model_prediction(title, keywords, subheadings):
|
| 18 |
+
headers = {
|
| 19 |
+
'Authorization': f'Bearer {MODEL_API_KEY}',
|
| 20 |
+
'Content-Type': 'application/json'
|
| 21 |
+
}
|
| 22 |
+
payload = {
|
| 23 |
+
'title': title,
|
| 24 |
+
'keywords': keywords,
|
| 25 |
+
'subheadings': subheadings
|
| 26 |
+
}
|
| 27 |
+
response = requests.post(MODEL_API_URL, headers=headers, json=payload)
|
| 28 |
+
return response.json()
|
| 29 |
+
|
| 30 |
+
@app.route('/predict', methods=['POST'])
|
| 31 |
+
def predict():
|
| 32 |
+
data = request.json
|
| 33 |
+
title = data.get('title')
|
| 34 |
+
keywords = data.get('keywords')
|
| 35 |
+
subheadings = data.get('subheadings')
|
| 36 |
+
|
| 37 |
+
# MongoDB'den ilgili verileri çekme
|
| 38 |
+
query = {
|
| 39 |
+
'title': title,
|
| 40 |
+
'keywords': {'$in': keywords.split(',')},
|
| 41 |
+
'subheadings': {'$in': subheadings.split(',')}
|
| 42 |
+
}
|
| 43 |
+
documents = list(collection.find(query))
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
predictions = []
|
| 47 |
+
for doc in documents:
|
| 48 |
+
result = get_model_prediction(doc['title'], doc['keywords'], doc['subheadings'])
|
| 49 |
+
predictions.append(result)
|
| 50 |
+
|
| 51 |
+
# Sonuçları döndürme
|
| 52 |
+
response = {
|
| 53 |
+
'title': title,
|
| 54 |
+
'keywords': keywords,
|
| 55 |
+
'subheadings': subheadings,
|
| 56 |
+
'predictions': predictions
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
return jsonify(response)
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
app.run(host='0.0.0.0', port=8080)
|
| 63 |
+
|