Spaces:
Runtime error
Runtime error
| 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 | |
| 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."}) | |
| 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) |