Spaces:
Runtime error
Runtime error
File size: 2,469 Bytes
7e6554b |
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 |
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
class APIService:
"""Basisklasse für alle API-Dienste."""
def __init__(self, api_key):
self.api_key = api_key
def fetch(self, *args, **kwargs):
raise NotImplementedError("Diese Methode muss von Unterklassen implementiert werden.")
class SynonymAPI(APIService):
"""API-Dienst für Synonyme."""
def fetch(self, word):
url = f"https://lingua-robot.p.rapidapi.com/language/v1/synonyms?word={word}"
headers = {
'x-rapidapi-key': self.api_key,
'x-rapidapi-host': "lingua-robot.p.rapidapi.com"
}
response = requests.get(url, headers=headers)
return response.json()['entries'][0]['synonyms'] if response.status_code == 200 else []
class GrammarAPI(APIService):
"""API-Dienst für Grammatik."""
def fetch(self, text):
response = requests.post(self.api_key, data={'text': text, 'language': 'en'})
return response.json()['matches'] if response.status_code == 200 else []
class APIManager:
"""Verwalten der APIs (Synonym und Grammatik)."""
def __init__(self):
self.synonym_api = None
self.grammar_api = None
def set_synonym_api(self, api):
self.synonym_api = api
def set_grammar_api(self, api):
self.grammar_api = api
def fetch_synonyms(self, word):
return self.synonym_api.fetch(word) if self.synonym_api else "Keine Synonym-API konfiguriert."
def fetch_grammar(self, text):
return self.grammar_api.fetch(text) if self.grammar_api else "Keine Grammatik-API konfiguriert."
# Instanziierung
api_manager = APIManager()
# API-Routen
@app.route('/configure', methods=['POST'])
def configure_apis():
data = request.json
synonym_api_key = data.get('synonym_api_key')
grammar_api_key = data.get('grammar_api_key')
api_manager.set_synonym_api(SynonymAPI(synonym_api_key))
api_manager.set_grammar_api(GrammarAPI(grammar_api_key))
return jsonify({"status": "APIs erfolgreich konfiguriert."})
@app.route('/analyze-text', methods=['POST'])
def analyze_text():
data = request.json
text = data.get('text')
word = text.split()[0]
synonyms = api_manager.fetch_synonyms(word)
grammar = api_manager.fetch_grammar(text)
return jsonify({"synonyms": synonyms, "grammar_suggestions": grammar})
# Start des Servers
if __name__ == '__main__':
app.run(debug=True) |