Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,97 @@
|
|
1 |
-
import
|
2 |
-
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
def
|
11 |
-
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
import zipfile
|
5 |
+
import requests
|
6 |
|
7 |
+
app = Flask(__name__)
|
|
|
|
|
|
|
8 |
|
9 |
+
class APIService:
|
10 |
+
""" Basisklasse für alle API-Dienste. """
|
11 |
+
def __init__(self, api_key):
|
12 |
+
self.api_key = api_key
|
13 |
|
14 |
+
def fetch(self, *args, **kwargs):
|
15 |
+
raise NotImplementedError("Diese Methode muss von Unterklassen implementiert werden.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
class SynonymAPI(APIService):
|
18 |
+
""" API-Dienst für Synonyme. """
|
19 |
+
def fetch(self, word):
|
20 |
+
url = f"https://lingua-robot.p.rapidapi.com/language/v1/synonyms?word={word}"
|
21 |
+
headers = {
|
22 |
+
'x-rapidapi-key': self.api_key,
|
23 |
+
'x-rapidapi-host': "lingua-robot.p.rapidapi.com"
|
24 |
+
}
|
25 |
+
response = requests.get(url, headers=headers)
|
26 |
+
return response.json()['entries'][0]['synonyms'] if response.status_code == 200 else []
|
27 |
|
28 |
+
class AliasManager:
|
29 |
+
""" Verwaltung von Aliasen. """
|
30 |
+
aliases = []
|
31 |
|
32 |
+
@classmethod
|
33 |
+
def add_alias(cls, alias_name, command, description):
|
34 |
+
cls.aliases.append({
|
35 |
+
"alias_name": alias_name,
|
36 |
+
"command": command,
|
37 |
+
"description": description
|
38 |
+
})
|
39 |
|
40 |
+
@classmethod
|
41 |
+
def list_aliases(cls):
|
42 |
+
return cls.aliases
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
class BackupManager:
|
45 |
+
""" Backup-Funktionalitäten """
|
46 |
+
@staticmethod
|
47 |
+
def create_backup():
|
48 |
+
backup_filename = "project_backup.zip"
|
49 |
+
with zipfile.ZipFile(backup_filename, 'w') as zipf:
|
50 |
+
for root, dirs, files in os.walk("."):
|
51 |
+
for file in files:
|
52 |
+
if file not in ["project_backup.zip"]:
|
53 |
+
zipf.write(os.path.join(root, file))
|
54 |
+
return backup_filename
|
55 |
|
56 |
+
# API-Routen
|
57 |
+
@app.route('/aliases', methods=['GET', 'POST'])
|
58 |
+
def manage_aliases():
|
59 |
+
if request.method == 'POST':
|
60 |
+
data = request.json
|
61 |
+
alias_name = data.get('alias_name')
|
62 |
+
command = data.get('command')
|
63 |
+
description = data.get('description')
|
64 |
+
AliasManager.add_alias(alias_name, command, description)
|
65 |
+
return jsonify({"status": "Alias erfolgreich erstellt", "alias_name": alias_name})
|
66 |
+
else:
|
67 |
+
return jsonify(AliasManager.list_aliases())
|
68 |
|
69 |
+
@app.route('/create-script', methods=['POST'])
|
70 |
+
def create_script():
|
71 |
+
data = request.json
|
72 |
+
script_name = data.get('script_name')
|
73 |
+
content = data.get('content')
|
74 |
+
description = data.get('description')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
with open(f"my_scripts/{script_name}.py", "w") as script_file:
|
77 |
+
script_file.write(content)
|
78 |
|
79 |
+
return jsonify({"status": "Skript erfolgreich erstellt", "script_name": script_name})
|
80 |
+
|
81 |
+
@app.route('/create-backup', methods=['POST'])
|
82 |
+
def create_backup():
|
83 |
+
backup_filename = BackupManager.create_backup()
|
84 |
+
return jsonify({"status": "Backup erfolgreich erstellt", "backup_filename": backup_filename})
|
85 |
+
|
86 |
+
@app.route('/tasks', methods=['GET'])
|
87 |
+
def get_tasks():
|
88 |
+
if os.path.exists("tasks.json"):
|
89 |
+
with open("tasks.json", "r") as tasks_file:
|
90 |
+
tasks = json.load(tasks_file)
|
91 |
+
else:
|
92 |
+
tasks = []
|
93 |
+
return jsonify(tasks)
|
94 |
+
|
95 |
+
# Start des Servers
|
96 |
+
if __name__ == '__main__':
|
97 |
+
app.run(debug=True)
|