Spaces:
Runtime error
Runtime error
Create backend.py
Browse files- backend.py +73 -0
backend.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
import requests
|
3 |
+
|
4 |
+
app = Flask(__name__)
|
5 |
+
|
6 |
+
class APIService:
|
7 |
+
"""Basisklasse für alle API-Dienste."""
|
8 |
+
def __init__(self, api_key):
|
9 |
+
self.api_key = api_key
|
10 |
+
|
11 |
+
def fetch(self, *args, **kwargs):
|
12 |
+
raise NotImplementedError("Diese Methode muss von Unterklassen implementiert werden.")
|
13 |
+
|
14 |
+
class SynonymAPI(APIService):
|
15 |
+
"""API-Dienst für Synonyme."""
|
16 |
+
def fetch(self, word):
|
17 |
+
url = f"https://lingua-robot.p.rapidapi.com/language/v1/synonyms?word={word}"
|
18 |
+
headers = {
|
19 |
+
'x-rapidapi-key': self.api_key,
|
20 |
+
'x-rapidapi-host': "lingua-robot.p.rapidapi.com"
|
21 |
+
}
|
22 |
+
response = requests.get(url, headers=headers)
|
23 |
+
return response.json()['entries'][0]['synonyms'] if response.status_code == 200 else []
|
24 |
+
|
25 |
+
class GrammarAPI(APIService):
|
26 |
+
"""API-Dienst für Grammatik."""
|
27 |
+
def fetch(self, text):
|
28 |
+
response = requests.post(self.api_key, data={'text': text, 'language': 'en'})
|
29 |
+
return response.json()['matches'] if response.status_code == 200 else []
|
30 |
+
|
31 |
+
class APIManager:
|
32 |
+
"""Verwalten der APIs (Synonym und Grammatik)."""
|
33 |
+
def __init__(self):
|
34 |
+
self.synonym_api = None
|
35 |
+
self.grammar_api = None
|
36 |
+
|
37 |
+
def set_synonym_api(self, api):
|
38 |
+
self.synonym_api = api
|
39 |
+
|
40 |
+
def set_grammar_api(self, api):
|
41 |
+
self.grammar_api = api
|
42 |
+
|
43 |
+
def fetch_synonyms(self, word):
|
44 |
+
return self.synonym_api.fetch(word) if self.synonym_api else "Keine Synonym-API konfiguriert."
|
45 |
+
|
46 |
+
def fetch_grammar(self, text):
|
47 |
+
return self.grammar_api.fetch(text) if self.grammar_api else "Keine Grammatik-API konfiguriert."
|
48 |
+
|
49 |
+
# Instanziierung
|
50 |
+
api_manager = APIManager()
|
51 |
+
|
52 |
+
# API-Routen
|
53 |
+
@app.route('/configure', methods=['POST'])
|
54 |
+
def configure_apis():
|
55 |
+
data = request.json
|
56 |
+
synonym_api_key = data.get('synonym_api_key')
|
57 |
+
grammar_api_key = data.get('grammar_api_key')
|
58 |
+
api_manager.set_synonym_api(SynonymAPI(synonym_api_key))
|
59 |
+
api_manager.set_grammar_api(GrammarAPI(grammar_api_key))
|
60 |
+
return jsonify({"status": "APIs erfolgreich konfiguriert."})
|
61 |
+
|
62 |
+
@app.route('/analyze-text', methods=['POST'])
|
63 |
+
def analyze_text():
|
64 |
+
data = request.json
|
65 |
+
text = data.get('text')
|
66 |
+
word = text.split()[0]
|
67 |
+
synonyms = api_manager.fetch_synonyms(word)
|
68 |
+
grammar = api_manager.fetch_grammar(text)
|
69 |
+
return jsonify({"synonyms": synonyms, "grammar_suggestions": grammar})
|
70 |
+
|
71 |
+
# Start des Servers
|
72 |
+
if __name__ == '__main__':
|
73 |
+
app.run(debug=True)
|