Spaces:
Configuration error

yonkasoft commited on
Commit
f53e415
·
verified ·
1 Parent(s): 5a85e93

Delete app2.py

Browse files
Files changed (1) hide show
  1. app2.py +0 -76
app2.py DELETED
@@ -1,76 +0,0 @@
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
- try:
28
- response = requests.post(MODEL_API_URL, headers=headers, json=payload)
29
- response.raise_for_status() # HTTP hatalarını yakalar
30
- result = response.json()
31
- return result
32
- except requests.exceptions.HTTPError as http_err:
33
- return {'error': f'HTTP error occurred: {http_err}'}
34
- except Exception as err:
35
- return {'error': f'Other error occurred: {err}'}
36
-
37
- @app.route('/predict', methods=['POST'])
38
- def predict():
39
- data = request.json
40
- title = data.get('title')
41
- keywords = data.get('keywords')
42
- subheadings = data.get('subheadings')
43
-
44
- # Giriş verilerini doğrulama
45
- if not title or not keywords or not subheadings:
46
- return jsonify({'error': 'Title, keywords, and subheadings are required'}), 400
47
-
48
- # MongoDB'den ilgili verileri çekme
49
- query = {
50
- 'title': title,
51
- 'keywords': {'$in': keywords.split(',')},
52
- 'subheadings': {'$in': subheadings.split(',')}
53
- }
54
- try:
55
- documents = list(collection.find(query))
56
- except Exception as e:
57
- return jsonify({'error': f'Error querying MongoDB: {e}'}), 500
58
-
59
- # Model API'den tahmin alma
60
- predictions = []
61
- for doc in documents:
62
- result = get_model_prediction(doc['title'], doc['keywords'], doc['subheadings'])
63
- predictions.append(result)
64
-
65
- # Sonuçları döndürme
66
- response = {
67
- 'title': title,
68
- 'keywords': keywords,
69
- 'subheadings': subheadings,
70
- 'predictions': predictions
71
- }
72
-
73
- return jsonify(response)
74
-
75
- if __name__ == "__main__":
76
- app.run(host='0.0.0.0', port=8080)