cloudwp commited on
Commit
d161e3e
1 Parent(s): ac5c525

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -6
app.py CHANGED
@@ -1,8 +1,121 @@
1
- from flask import Flask
 
 
 
2
  app = Flask(__name__)
3
- @app.route("/")
4
- def home():
5
- return "Hello, Hugging Face Space!"
6
 
7
- if __name__ == "__main__":
8
- app.run(host="0.0.0.0", port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import requests
3
+ import json
4
+
5
  app = Flask(__name__)
 
 
 
6
 
7
+ # Laden der API-Keys aus einer Konfigurationsdatei
8
+ def load_api_keys():
9
+ try:
10
+ with open("config.json", "r") as file:
11
+ return json.load(file)
12
+ except FileNotFoundError:
13
+ return {"synonym_api_keys": {"primary": "", "secondary": ""}, "grammar_api_key": ""}
14
+
15
+ # Speichern der API-Keys
16
+ def save_api_keys(api_keys):
17
+ with open("config.json", "w") as file:
18
+ json.dump(api_keys, file)
19
+
20
+ # API-Dienst für Synonyme (mit Fallback)
21
+ class SynonymAPI:
22
+ def __init__(self, api_keys):
23
+ self.api_keys = api_keys
24
+
25
+ def fetch(self, word, language='en'):
26
+ try:
27
+ response = self._fetch_from_primary_api(word, language)
28
+ if response.status_code == 200:
29
+ return response.json()['entries'][0]['synonyms']
30
+ except Exception:
31
+ return self._fetch_from_secondary_api(word, language)
32
+
33
+ def _fetch_from_primary_api(self, word, language):
34
+ url = f"https://lingua-robot.p.rapidapi.com/language/v1/synonyms?word={word}&language={language}"
35
+ headers = {
36
+ 'x-rapidapi-key': self.api_keys['primary'],
37
+ 'x-rapidapi-host': "lingua-robot.p.rapidapi.com"
38
+ }
39
+ return requests.get(url, headers=headers)
40
+
41
+ def _fetch_from_secondary_api(self, word, language):
42
+ url = f"https://api.datamuse.com/words?rel_syn={word}&ml={language}"
43
+ return requests.get(url)
44
+
45
+ # API-Dienst für Grammatik
46
+ class GrammarAPI:
47
+ def __init__(self, api_key):
48
+ self.api_key = api_key
49
+
50
+ def fetch(self, text, language='en'):
51
+ response = requests.post(self.api_key, data={'text': text, 'language': language})
52
+ return response.json()['matches'] if response.status_code == 200 else []
53
+
54
+ # API Manager zum Verwalten der Dienste
55
+ class APIManager:
56
+ def __init__(self):
57
+ self.synonym_api = None
58
+ self.grammar_api = None
59
+
60
+ def configure_apis(self, synonym_api_keys, grammar_api_key):
61
+ self.synonym_api = SynonymAPI(synonym_api_keys)
62
+ self.grammar_api = GrammarAPI(grammar_api_key)
63
+
64
+ def fetch_synonyms(self, word, language='en'):
65
+ return self.synonym_api.fetch(word, language) if self.synonym_api else "Keine Synonym-API konfiguriert."
66
+
67
+ def fetch_grammar(self, text, language='en'):
68
+ return self.grammar_api.fetch(text, language) if self.grammar_api else "Keine Grammatik-API konfiguriert."
69
+
70
+ # Instanziierung des API-Managers
71
+ api_manager = APIManager()
72
+
73
+ # API-Konfiguration
74
+ @app.route('/configure', methods=['POST'])
75
+ def configure_apis():
76
+ data = request.json
77
+ api_keys = {
78
+ 'synonym_api_keys': {
79
+ 'primary': data.get('synonym_primary_api_key'),
80
+ 'secondary': data.get('synonym_secondary_api_key')
81
+ },
82
+ 'grammar_api_key': data.get('grammar_api_key')
83
+ }
84
+ save_api_keys(api_keys)
85
+ api_manager.configure_apis(api_keys['synonym_api_keys'], api_keys['grammar_api_key'])
86
+ return jsonify({"status": "APIs erfolgreich konfiguriert."})
87
+
88
+ # Textanalyse-API mit Zeichenanpassung
89
+ @app.route('/analyze-text', methods=['POST'])
90
+ def analyze_text():
91
+ data = request.json
92
+ text = data.get('text')
93
+ desired_length = data.get('desired_length', len(text))
94
+ language = data.get('language', 'en')
95
+
96
+ word = text.split()[0]
97
+ synonyms = api_manager.fetch_synonyms(word, language)
98
+ grammar = api_manager.fetch_grammar(text, language)
99
+
100
+ current_length = len(text)
101
+ length_difference = desired_length - current_length
102
+
103
+ response = {
104
+ "synonyms": synonyms,
105
+ "grammar_suggestions": grammar,
106
+ "current_length": current_length,
107
+ "desired_length": desired_length,
108
+ "length_difference": length_difference,
109
+ "adjustment_suggestions": []
110
+ }
111
+
112
+ if length_difference > 0:
113
+ response["adjustment_suggestions"].append(f"Fügen Sie {length_difference} Zeichen hinzu.")
114
+ elif length_difference < 0:
115
+ response["adjustment_suggestions"].append(f"Entfernen Sie {abs(length_difference)} Zeichen.")
116
+
117
+ return jsonify(response)
118
+
119
+ # Serverstart
120
+ if __name__ == '__main__':
121
+ app.run(debug=True)