Spaces:
Build error
Build error
Upload noura.py
Browse files- noura_ai/noura.py +53 -0
noura_ai/noura.py
CHANGED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import threading
|
| 3 |
+
import requests
|
| 4 |
+
from bs4 import BeautifulSoup
|
| 5 |
+
from flask import Flask, jsonify
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
knowledge_base = []
|
| 10 |
+
|
| 11 |
+
learning_sources = [
|
| 12 |
+
"https://www.khanacademy.org",
|
| 13 |
+
"https://www.coursera.org",
|
| 14 |
+
"https://www.edx.org",
|
| 15 |
+
"https://www.ted.com/topics/learning",
|
| 16 |
+
"https://www.edlibre.com/websites-to-learn-something-new-every-day/"
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
def fetch_and_learn():
|
| 20 |
+
global knowledge_base
|
| 21 |
+
new_knowledge = []
|
| 22 |
+
for url in learning_sources:
|
| 23 |
+
try:
|
| 24 |
+
headers = {'User-Agent': 'Mozilla/5.0'}
|
| 25 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 26 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 27 |
+
text = soup.get_text()
|
| 28 |
+
snippet = text.strip().replace('\n', ' ')[:1000]
|
| 29 |
+
new_knowledge.append({"source": url, "content": snippet})
|
| 30 |
+
except Exception as e:
|
| 31 |
+
new_knowledge.append({"source": url, "content": f"Error: {e}"})
|
| 32 |
+
knowledge_base = new_knowledge
|
| 33 |
+
|
| 34 |
+
def auto_learn():
|
| 35 |
+
while True:
|
| 36 |
+
fetch_and_learn()
|
| 37 |
+
time.sleep(3600)
|
| 38 |
+
|
| 39 |
+
learning_thread = threading.Thread(target=auto_learn)
|
| 40 |
+
learning_thread.daemon = True
|
| 41 |
+
learning_thread.start()
|
| 42 |
+
|
| 43 |
+
@app.route('/noura/knowledge', methods=['GET'])
|
| 44 |
+
def get_knowledge():
|
| 45 |
+
return jsonify(knowledge_base)
|
| 46 |
+
|
| 47 |
+
@app.route('/')
|
| 48 |
+
def index():
|
| 49 |
+
return "نورا تعمل وتتعلم تلقائيًا من الإنترنت."
|
| 50 |
+
|
| 51 |
+
if __name__ == '__main__':
|
| 52 |
+
fetch_and_learn() # أول تعلم يدوي عند بدء التشغيل
|
| 53 |
+
app.run(host='0.0.0.0', port=3000)
|