Spaces:
Sleeping
Sleeping
Sahil commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,6 +17,16 @@ TRAINING_DATASET = "Sahil5112/ContinuumGPT" # Main training dataset for Continu
|
|
| 17 |
CONVERSATION_BUFFER = []
|
| 18 |
MAX_BUFFER_SIZE = 10 # Save to HF after 10 training examples
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def load_training_dataset():
|
| 21 |
"""Load existing training data from HuggingFace"""
|
| 22 |
try:
|
|
@@ -54,10 +64,95 @@ def save_to_training_dataset(training_examples):
|
|
| 54 |
print(f"β Error saving to dataset: {e}")
|
| 55 |
return False
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
@app.route("/")
|
| 58 |
def index():
|
| 59 |
return send_from_directory(".", "index.html")
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
@app.route("/api/train", methods=["POST"])
|
| 62 |
def train_model():
|
| 63 |
"""Process AI model response and save as training data"""
|
|
@@ -149,14 +244,14 @@ def flush_buffer():
|
|
| 149 |
return jsonify({"error": "Failed to flush buffer"}), 500
|
| 150 |
|
| 151 |
if __name__ == "__main__":
|
| 152 |
-
port = int(os.getenv("PORT",
|
| 153 |
|
| 154 |
print("π Starting ContinuumLearner Training Server...")
|
| 155 |
print(f"π Training Dataset: {TRAINING_DATASET}")
|
| 156 |
print(f"π Dataset URL: https://huggingface.co/datasets/{TRAINING_DATASET}")
|
| 157 |
print("")
|
| 158 |
print("π€ Training Mode: Model Copy Learning")
|
| 159 |
-
print(" - AI models respond to prompts")
|
| 160 |
print(" - Responses are saved as training data")
|
| 161 |
print(" - ContinuumGPT learns from these patterns")
|
| 162 |
print(" - NO user data is stored")
|
|
@@ -168,5 +263,7 @@ if __name__ == "__main__":
|
|
| 168 |
print(f"π Current dataset size: {len(training_data)} training examples")
|
| 169 |
else:
|
| 170 |
print("β οΈ HuggingFace Integration Disabled - Add HF_TOKEN to enable")
|
|
|
|
|
|
|
| 171 |
|
| 172 |
app.run(host="0.0.0.0", port=port, debug=False, threaded=True)
|
|
|
|
| 17 |
CONVERSATION_BUFFER = []
|
| 18 |
MAX_BUFFER_SIZE = 10 # Save to HF after 10 training examples
|
| 19 |
|
| 20 |
+
# Model mapping for HuggingFace Inference API
|
| 21 |
+
MODEL_MAPPING = {
|
| 22 |
+
"puter:gpt-5-nano": "meta-llama/Llama-3.2-3B-Instruct",
|
| 23 |
+
"puter:claude-sonnet-4": "meta-llama/Llama-3.2-3B-Instruct",
|
| 24 |
+
"puter:google/gemini-2.5-flash": "meta-llama/Llama-3.2-3B-Instruct",
|
| 25 |
+
"puter:meta-llama/llama-4-scout": "meta-llama/Llama-3.2-3B-Instruct",
|
| 26 |
+
"puter:deepseek-chat": "meta-llama/Llama-3.2-3B-Instruct",
|
| 27 |
+
"puter:liquid/lfm-7b": "meta-llama/Llama-3.2-3B-Instruct"
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
def load_training_dataset():
|
| 31 |
"""Load existing training data from HuggingFace"""
|
| 32 |
try:
|
|
|
|
| 64 |
print(f"β Error saving to dataset: {e}")
|
| 65 |
return False
|
| 66 |
|
| 67 |
+
def call_huggingface_model(model_name, prompt):
|
| 68 |
+
"""Call HuggingFace Inference API - Returns dict with success/error info"""
|
| 69 |
+
if not HF_TOKEN:
|
| 70 |
+
return {
|
| 71 |
+
"success": False,
|
| 72 |
+
"error": "HF_TOKEN not set. Please add your HuggingFace token to enable AI model training.",
|
| 73 |
+
"response": None
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
hf_model = MODEL_MAPPING.get(model_name, "meta-llama/Llama-3.2-3B-Instruct")
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 80 |
+
api_url = f"https://api-inference.huggingface.co/models/{hf_model}"
|
| 81 |
+
|
| 82 |
+
payload = {
|
| 83 |
+
"inputs": prompt,
|
| 84 |
+
"parameters": {
|
| 85 |
+
"max_new_tokens": 512,
|
| 86 |
+
"temperature": 0.7,
|
| 87 |
+
"top_p": 0.95,
|
| 88 |
+
"return_full_text": False
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
response = requests.post(api_url, headers=headers, json=payload, timeout=30)
|
| 93 |
+
|
| 94 |
+
if response.status_code == 200:
|
| 95 |
+
result = response.json()
|
| 96 |
+
if isinstance(result, list) and len(result) > 0:
|
| 97 |
+
generated_text = result[0].get("generated_text", "")
|
| 98 |
+
return {
|
| 99 |
+
"success": True,
|
| 100 |
+
"error": None,
|
| 101 |
+
"response": generated_text
|
| 102 |
+
}
|
| 103 |
+
return {
|
| 104 |
+
"success": False,
|
| 105 |
+
"error": f"Unexpected response format: {str(result)}",
|
| 106 |
+
"response": None
|
| 107 |
+
}
|
| 108 |
+
elif response.status_code == 503:
|
| 109 |
+
return {
|
| 110 |
+
"success": False,
|
| 111 |
+
"error": f"Model {hf_model} is loading. Please try again in a few seconds.",
|
| 112 |
+
"response": None
|
| 113 |
+
}
|
| 114 |
+
else:
|
| 115 |
+
return {
|
| 116 |
+
"success": False,
|
| 117 |
+
"error": f"API Error: {response.status_code} - {response.text[:200]}",
|
| 118 |
+
"response": None
|
| 119 |
+
}
|
| 120 |
+
except Exception as e:
|
| 121 |
+
return {
|
| 122 |
+
"success": False,
|
| 123 |
+
"error": f"Error calling model: {str(e)}",
|
| 124 |
+
"response": None
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
@app.route("/")
|
| 128 |
def index():
|
| 129 |
return send_from_directory(".", "index.html")
|
| 130 |
|
| 131 |
+
@app.route("/api/generate", methods=["POST"])
|
| 132 |
+
def generate_response():
|
| 133 |
+
"""Generate AI response using HuggingFace models"""
|
| 134 |
+
data = request.get_json()
|
| 135 |
+
prompt = data.get("prompt", "").strip()
|
| 136 |
+
model = data.get("model", "puter:gpt-5-nano")
|
| 137 |
+
|
| 138 |
+
if not prompt:
|
| 139 |
+
return jsonify({"success": False, "error": "Missing prompt"}), 400
|
| 140 |
+
|
| 141 |
+
result = call_huggingface_model(model, prompt)
|
| 142 |
+
|
| 143 |
+
if result["success"]:
|
| 144 |
+
return jsonify({
|
| 145 |
+
"success": True,
|
| 146 |
+
"response": result["response"],
|
| 147 |
+
"model": model
|
| 148 |
+
})
|
| 149 |
+
else:
|
| 150 |
+
return jsonify({
|
| 151 |
+
"success": False,
|
| 152 |
+
"error": result["error"],
|
| 153 |
+
"model": model
|
| 154 |
+
})
|
| 155 |
+
|
| 156 |
@app.route("/api/train", methods=["POST"])
|
| 157 |
def train_model():
|
| 158 |
"""Process AI model response and save as training data"""
|
|
|
|
| 244 |
return jsonify({"error": "Failed to flush buffer"}), 500
|
| 245 |
|
| 246 |
if __name__ == "__main__":
|
| 247 |
+
port = int(os.getenv("PORT", 5000))
|
| 248 |
|
| 249 |
print("π Starting ContinuumLearner Training Server...")
|
| 250 |
print(f"π Training Dataset: {TRAINING_DATASET}")
|
| 251 |
print(f"π Dataset URL: https://huggingface.co/datasets/{TRAINING_DATASET}")
|
| 252 |
print("")
|
| 253 |
print("π€ Training Mode: Model Copy Learning")
|
| 254 |
+
print(" - AI models respond to prompts via HuggingFace API")
|
| 255 |
print(" - Responses are saved as training data")
|
| 256 |
print(" - ContinuumGPT learns from these patterns")
|
| 257 |
print(" - NO user data is stored")
|
|
|
|
| 263 |
print(f"π Current dataset size: {len(training_data)} training examples")
|
| 264 |
else:
|
| 265 |
print("β οΈ HuggingFace Integration Disabled - Add HF_TOKEN to enable")
|
| 266 |
+
print(" - You can still use the app, but responses will show warnings")
|
| 267 |
+
print(" - Training data won't be saved to HuggingFace")
|
| 268 |
|
| 269 |
app.run(host="0.0.0.0", port=port, debug=False, threaded=True)
|