Spaces:
Configuration error
Configuration error
File size: 2,327 Bytes
7bc1b72 719954c 7bc1b72 719954c 7bc1b72 719954c 7bc1b72 |
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 |
from flask import Flask, request, jsonify
import pymongo
import requests
import json
app = Flask(__name__)
# MongoDB bağlantısı
mongo_client = pymongo.MongoClient('mongodb://localhost:27017/')
db = mongo_client['EgitimDatabase']
collection = db['test']
# Model API Endpoint
MODEL_API_URL = "https://api.example.com/model" # Hugging Face API URL veya başka bir model URL'si
MODEL_API_KEY = "YOUR_API_KEY" # API anahtarınız
def get_model_prediction(title, keywords, subheadings):
headers = {
'Authorization': f'Bearer {MODEL_API_KEY}',
'Content-Type': 'application/json'
}
payload = {
'title': title,
'keywords': keywords,
'subheadings': subheadings
}
try:
response = requests.post(MODEL_API_URL, headers=headers, json=payload)
response.raise_for_status() # HTTP hatalarını yakalar
result = response.json()
return result
except requests.exceptions.HTTPError as http_err:
return {'error': f'HTTP error occurred: {http_err}'}
except Exception as err:
return {'error': f'Other error occurred: {err}'}
@app.route('/predict', methods=['POST'])
def predict():
data = request.json
title = data.get('title')
keywords = data.get('keywords')
subheadings = data.get('subheadings')
# Giriş verilerini doğrulama
if not title or not keywords or not subheadings:
return jsonify({'error': 'Title, keywords, and subheadings are required'}), 400
# MongoDB'den ilgili verileri çekme
query = {
'title': title,
'keywords': {'$in': keywords.split(',')},
'subheadings': {'$in': subheadings.split(',')}
}
try:
documents = list(collection.find(query))
except Exception as e:
return jsonify({'error': f'Error querying MongoDB: {e}'}), 500
# Model API'den tahmin alma
predictions = []
for doc in documents:
result = get_model_prediction(doc['title'], doc['keywords'], doc['subheadings'])
predictions.append(result)
# Sonuçları döndürme
response = {
'title': title,
'keywords': keywords,
'subheadings': subheadings,
'predictions': predictions
}
return jsonify(response)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)
|