Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,103 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
-
environment variables
|
4 |
-
os.environ = "HUGGINGFACEHUB_API_TOKEN"
|
5 |
-
|
6 |
-
"""
|
7 |
-
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
|
8 |
-
"""
|
9 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
messages = [{"role": "system", "content": system_message}]
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
if val[1]:
|
26 |
-
messages.append({"role": "assistant", "content": val[1]})
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
):
|
39 |
-
token = message.choices[0].delta.content
|
40 |
|
41 |
-
|
42 |
-
|
|
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
53 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
54 |
-
gr.Slider(
|
55 |
-
minimum=0.1,
|
56 |
-
maximum=1.0,
|
57 |
-
value=0.95,
|
58 |
-
step=0.05,
|
59 |
-
label="Top-p (nucleus sampling)",
|
60 |
-
),
|
61 |
-
],
|
62 |
)
|
63 |
|
64 |
-
|
65 |
if __name__ == "__main__":
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
# app.py
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
+
import requests
|
6 |
+
import importlib.util
|
7 |
+
import os
|
8 |
+
import hashlib
|
9 |
+
import json
|
10 |
+
from datetime import datetime
|
11 |
|
12 |
+
# Configuration
|
13 |
+
CONFIG = {
|
14 |
+
"model_name": "deepseek-ai/deepseek-coder-1.3b-instruct",
|
15 |
+
"update_url": "https://raw.githubusercontent.com/[YOUR_USERNAME]/deepseek-updates/main/",
|
16 |
+
"manifest_url": "https://raw.githubusercontent.com/[YOUR_USERNAME]/deepseek-updates/main/manifest.json",
|
17 |
+
"allowed_modules": ["response_handler", "updater"],
|
18 |
+
"update_frequency": 6 # hours
|
19 |
+
}
|
|
|
20 |
|
21 |
+
# Load model and tokenizer
|
22 |
+
tokenizer = AutoTokenizer.from_pretrained(CONFIG["model_name"])
|
23 |
+
model = AutoModelForCausalLM.from_pretrained(CONFIG["model_name"])
|
|
|
|
|
24 |
|
25 |
+
# Security verification
|
26 |
+
def verify_file_integrity(content, expected_hash):
|
27 |
+
sha256 = hashlib.sha256()
|
28 |
+
sha256.update(content.encode('utf-8'))
|
29 |
+
return sha256.hexdigest() == expected_hash
|
30 |
|
31 |
+
# Update mechanism
|
32 |
+
def check_for_updates():
|
33 |
+
try:
|
34 |
+
# Get update manifest
|
35 |
+
response = requests.get(CONFIG["manifest_url"])
|
36 |
+
manifest = response.json()
|
37 |
+
|
38 |
+
# Check last update time
|
39 |
+
last_update_path = "last_update.txt"
|
40 |
+
if os.path.exists(last_update_path):
|
41 |
+
with open(last_update_path, 'r') as f:
|
42 |
+
last_update = datetime.fromisoformat(f.read().strip())
|
43 |
+
time_since_update = (datetime.utcnow() - last_update).total_seconds() / 3600
|
44 |
+
if time_since_update < CONFIG["update_frequency"]:
|
45 |
+
return "Too soon for update check"
|
46 |
+
|
47 |
+
# Process updates
|
48 |
+
updates_applied = []
|
49 |
+
for module_name in CONFIG["allowed_modules"]:
|
50 |
+
if module_name in manifest["modules"]:
|
51 |
+
module_info = manifest["modules"][module_name]
|
52 |
+
file_path = f"{module_name}.py"
|
53 |
+
|
54 |
+
# Download and verify update
|
55 |
+
response = requests.get(CONFIG["update_url"] + file_path)
|
56 |
+
if response.status_code == 200:
|
57 |
+
content = response.text
|
58 |
+
if verify_file_integrity(content, module_info["sha256"]):
|
59 |
+
# Save new version
|
60 |
+
with open(file_path, 'w') as f:
|
61 |
+
f.write(content)
|
62 |
+
updates_applied.append(module_name)
|
63 |
+
|
64 |
+
# Update timestamp
|
65 |
+
with open(last_update_path, 'w') as f:
|
66 |
+
f.write(datetime.utcnow().isoformat())
|
67 |
+
|
68 |
+
return f"Updates applied to: {', '.join(updates_applied)}" if updates_applied else "No updates available"
|
69 |
+
|
70 |
+
except Exception as e:
|
71 |
+
return f"Update failed: {str(e)}"
|
72 |
|
73 |
+
# Dynamic module loader
|
74 |
+
def load_module(module_name):
|
75 |
+
spec = importlib.util.spec_from_file_location(module_name, f"{module_name}.py")
|
76 |
+
module = importlib.util.module_from_spec(spec)
|
77 |
+
spec.loader.exec_module(module)
|
78 |
+
return module
|
|
|
|
|
79 |
|
80 |
+
# Load core modules
|
81 |
+
response_handler = load_module("response_handler")
|
82 |
+
updater = load_module("updater")
|
83 |
|
84 |
+
# Main processing function
|
85 |
+
def process_query(prompt):
|
86 |
+
# Check for update command
|
87 |
+
if "/update" in prompt:
|
88 |
+
return check_for_updates()
|
89 |
+
|
90 |
+
# Normal processing
|
91 |
+
return response_handler.generate_response(prompt, tokenizer, model)
|
92 |
|
93 |
+
# Create Gradio interface
|
94 |
+
interface = gr.Interface(
|
95 |
+
fn=process_query,
|
96 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter your query..."),
|
97 |
+
outputs="text",
|
98 |
+
title="Self-Updating DeepSeek AI",
|
99 |
+
description="This AI can update its own code. Type '/update' to check for improvements."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
)
|
101 |
|
|
|
102 |
if __name__ == "__main__":
|
103 |
+
interface.launch(server_port=7860, share=True)
|