Spaces:
Configuration error
Configuration error
| 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}'} | |
| 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) | |