Spaces:
Runtime error
Runtime error
File size: 4,181 Bytes
d161e3e 374b8bc d161e3e |
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
from flask import Flask, request, jsonify
import requests
import json
app = Flask(__name__)
# Laden der API-Keys aus einer Konfigurationsdatei
def load_api_keys():
try:
with open("config.json", "r") as file:
return json.load(file)
except FileNotFoundError:
return {"synonym_api_keys": {"primary": "", "secondary": ""}, "grammar_api_key": ""}
# Speichern der API-Keys
def save_api_keys(api_keys):
with open("config.json", "w") as file:
json.dump(api_keys, file)
# API-Dienst für Synonyme (mit Fallback)
class SynonymAPI:
def __init__(self, api_keys):
self.api_keys = api_keys
def fetch(self, word, language='en'):
try:
response = self._fetch_from_primary_api(word, language)
if response.status_code == 200:
return response.json()['entries'][0]['synonyms']
except Exception:
return self._fetch_from_secondary_api(word, language)
def _fetch_from_primary_api(self, word, language):
url = f"https://lingua-robot.p.rapidapi.com/language/v1/synonyms?word={word}&language={language}"
headers = {
'x-rapidapi-key': self.api_keys['primary'],
'x-rapidapi-host': "lingua-robot.p.rapidapi.com"
}
return requests.get(url, headers=headers)
def _fetch_from_secondary_api(self, word, language):
url = f"https://api.datamuse.com/words?rel_syn={word}&ml={language}"
return requests.get(url)
# API-Dienst für Grammatik
class GrammarAPI:
def __init__(self, api_key):
self.api_key = api_key
def fetch(self, text, language='en'):
response = requests.post(self.api_key, data={'text': text, 'language': language})
return response.json()['matches'] if response.status_code == 200 else []
# API Manager zum Verwalten der Dienste
class APIManager:
def __init__(self):
self.synonym_api = None
self.grammar_api = None
def configure_apis(self, synonym_api_keys, grammar_api_key):
self.synonym_api = SynonymAPI(synonym_api_keys)
self.grammar_api = GrammarAPI(grammar_api_key)
def fetch_synonyms(self, word, language='en'):
return self.synonym_api.fetch(word, language) if self.synonym_api else "Keine Synonym-API konfiguriert."
def fetch_grammar(self, text, language='en'):
return self.grammar_api.fetch(text, language) if self.grammar_api else "Keine Grammatik-API konfiguriert."
# Instanziierung des API-Managers
api_manager = APIManager()
# API-Konfiguration
@app.route('/configure', methods=['POST'])
def configure_apis():
data = request.json
api_keys = {
'synonym_api_keys': {
'primary': data.get('synonym_primary_api_key'),
'secondary': data.get('synonym_secondary_api_key')
},
'grammar_api_key': data.get('grammar_api_key')
}
save_api_keys(api_keys)
api_manager.configure_apis(api_keys['synonym_api_keys'], api_keys['grammar_api_key'])
return jsonify({"status": "APIs erfolgreich konfiguriert."})
# Textanalyse-API mit Zeichenanpassung
@app.route('/analyze-text', methods=['POST'])
def analyze_text():
data = request.json
text = data.get('text')
desired_length = data.get('desired_length', len(text))
language = data.get('language', 'en')
word = text.split()[0]
synonyms = api_manager.fetch_synonyms(word, language)
grammar = api_manager.fetch_grammar(text, language)
current_length = len(text)
length_difference = desired_length - current_length
response = {
"synonyms": synonyms,
"grammar_suggestions": grammar,
"current_length": current_length,
"desired_length": desired_length,
"length_difference": length_difference,
"adjustment_suggestions": []
}
if length_difference > 0:
response["adjustment_suggestions"].append(f"Fügen Sie {length_difference} Zeichen hinzu.")
elif length_difference < 0:
response["adjustment_suggestions"].append(f"Entfernen Sie {abs(length_difference)} Zeichen.")
return jsonify(response)
# Serverstart
if __name__ == '__main__':
app.run(debug=True) |