Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,44 @@
|
|
| 1 |
import os
|
| 2 |
from flask import Flask, render_template, request, jsonify
|
| 3 |
-
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
-
from
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
-
CORS(app)
|
| 9 |
|
| 10 |
-
client = InferenceClient(
|
| 11 |
-
model="meta-llama/Llama-3.2-3B-Instruct",
|
| 12 |
-
token=os.getenv("HF_TOKEN")
|
| 13 |
-
)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
with DDGS() as ddgs:
|
| 19 |
-
results = [r['body'] for r in ddgs.text(query, max_results=3)]
|
| 20 |
-
return "\n".join(results)
|
| 21 |
-
except Exception:
|
| 22 |
-
return "No real-time data available at the moment."
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
@app.route('/ask', methods=['POST'])
|
| 29 |
def ask():
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
"""
|
| 45 |
-
|
| 46 |
-
# 3. Call Llama
|
| 47 |
-
response = ""
|
| 48 |
-
for message in client.chat_completion(
|
| 49 |
-
messages=[
|
| 50 |
-
{"role": "system", "content": system_prompt},
|
| 51 |
-
{"role": "user", "content": user_query}
|
| 52 |
-
],
|
| 53 |
-
max_tokens=800,
|
| 54 |
-
stream=True,
|
| 55 |
-
):
|
| 56 |
-
token = message.choices[0].delta.content
|
| 57 |
-
if token:
|
| 58 |
-
response += token
|
| 59 |
-
|
| 60 |
-
return jsonify({"answer": response})
|
| 61 |
-
|
| 62 |
-
except Exception as e:
|
| 63 |
-
return jsonify({"answer": f"System Error: {str(e)}"}), 500
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
from flask import Flask, render_template, request, jsonify
|
| 3 |
+
import chromadb
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
|
|
|
| 8 |
|
| 9 |
+
client = InferenceClient(model="meta-llama/Llama-3.2-3B-Instruct", token=os.getenv("HF_TOKEN"))
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 12 |
+
db_client = chromadb.PersistentClient(path="./my_vector_db")
|
| 13 |
+
collection = db_client.get_or_create_collection(name="research_docs")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def add_to_knowledge_base(text_list):
|
| 16 |
+
embeddings = embed_model.encode(text_list).tolist()
|
| 17 |
+
ids = [f"id_{i}" for i in range(len(text_list))]
|
| 18 |
+
collection.add(documents=text_list, embeddings=embeddings, ids=ids)
|
| 19 |
+
|
| 20 |
+
initial_data = [
|
| 21 |
+
"The 2026 World AI Summit took place in Tokyo, focusing on decentralized models.",
|
| 22 |
+
"Nexus AI Research OS was launched in January 2026 as a browser-based tool.",
|
| 23 |
+
]
|
| 24 |
+
add_to_knowledge_base(initial_data)
|
| 25 |
|
| 26 |
@app.route('/ask', methods=['POST'])
|
| 27 |
def ask():
|
| 28 |
+
user_query = request.get_json().get("query")
|
| 29 |
+
|
| 30 |
+
query_vec = embed_model.encode([user_query]).tolist()
|
| 31 |
+
results = collection.query(query_embeddings=query_vec, n_results=2)
|
| 32 |
+
retrieved_text = "\n".join(results['documents'][0])
|
| 33 |
+
|
| 34 |
+
prompt = f"Use this data to answer: {retrieved_text}\n\nQuestion: {user_query}"
|
| 35 |
+
|
| 36 |
+
response = client.chat_completion(
|
| 37 |
+
messages=[{"role": "user", "content": prompt}],
|
| 38 |
+
max_tokens=500
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
return jsonify({"answer": response.choices[0].message.content})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|
| 44 |
app.run(host="0.0.0.0", port=7860)
|