-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathapp.py
133 lines (115 loc) · 5.09 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import logging
from flask import Flask, request, jsonify
from flask_cors import CORS
from beyondllm import source,retrieve,generator
from utils.mapping import url_map
from beyondllm.llms import GeminiModel
from beyondllm.embeddings import HuggingFaceEmbeddings
from utils.keywordExtractor import keyword_from_search_sentence
from utils.rankBlogs import rank_documents
from dotenv import load_dotenv # type: ignore
import pymongo as pym
load_dotenv()
import os
# configure logging and save the logs in a file within the logs directory
logging.basicConfig(filename='logs/app.log', level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# get all the env variables
MONGO_URL = os.getenv("MONGO_URL")
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
# connect to the mongodb database
client = pym.MongoClient(MONGO_URL)
db = client["newsgpt"]
blog_collection = db["articles"]
user_collection = db["users"]
# define the app
app = Flask(__name__)
CORS(app)
@app.route('/', methods=['GET'])
def home():
return "Welcome to the NewsGPT API!"
@app.route('/recommend', methods=['GET'])
def recommend_endpoint():
'''
calls the rank_documents function with the search keywords as an empty list and the documents and returns the ranked documents.
'''
try:
# identifiers for the database and the collection
documents = list(blog_collection.find({}))
users = list(user_collection.find({}))
search_keywords = []
# displaying all documents while sorting based on the search keywords.
ranked_documents = rank_documents(search_keywords=search_keywords,
documents=documents,
users=users,
search_filter=False)
ranked_list = [{'title': doc[0], 'user': doc[1], 'slug': doc[2]} for doc in ranked_documents]
return jsonify({"ranked_documents": ranked_list})
except Exception as e:
logging.error(f"Error in /recommend endpoint: {e}")
return jsonify({"error": str(e)})
@app.route('/rank', methods=['POST'])
def rank_endpoint():
'''
calls the rank_documents function with the search keywords from the data and the documents and returns the ranked documents.
'''
try:
data = request.json
documents = list(blog_collection.find({}))
users = list(user_collection.find({}))
search_keywords = data.get('search_keywords')
# displaying all documents while sorting based on the search keywords.
ranked_documents = rank_documents(search_keywords=search_keywords,
documents=documents,
users=users,
search_filter=True)
ranked_list = [{'title': doc[0], 'user': doc[1], 'slug': doc[2], 'image': doc[3]} for doc in ranked_documents]
return jsonify({"ranked_documents": ranked_list})
except Exception as e:
logging.error(f"Error in /rank endpoint: {e}")
return jsonify({"error": str(e)})
@app.route('/keyword_extractor', methods=['POST'])
def yake_endpoint():
'''
takes data in the form of a dictionary with the key 'text' and returns the top 10 keywords from the text.
'''
try:
data = request.json
text = data.get('body')
keywords = keyword_from_search_sentence(text)
return jsonify({"keywords": keywords})
except Exception as e:
logging.error(f"Error in /keyword_extractor endpoint: {e}")
return jsonify({"error": str(e)})
@app.route('/ai', methods=['POST'])
def ai_endpoint():
'''
takes data in the form of a dictionary with the keys 'path', 'type', and 'question' and returns the response from the enterprise RAG model.
'''
try:
data_post = request.json
path = url_map[data_post.get('path')] # https://highonbugs.sbk2k1.in/sows
# print(path)
dtype = data_post.get('type') # "url", "youtube", etc.
question = data_post.get('question') # What is the best way to learn python?
data = source.fit(path=path,
dtype=dtype,
chunk_size=1024)
embed_model = HuggingFaceEmbeddings(model_name="BAAI/bge-small-en-v1.5")
retriever = retrieve.auto_retriever(data=data,
embed_model=embed_model,
type="normal",
top_k=3)
llm = GeminiModel(model_name="gemini-pro",
google_api_key = GOOGLE_API_KEY)
pipeline = generator.Generate(
question=question,
system_prompt = "you are a smart AI chatbot, answer the question asked by the user",
llm = llm,
retriever=retriever)
response = pipeline.call()
return jsonify({"response": response})
except Exception as e:
logging.error(f"Error in /ai endpoint: {e}")
return jsonify({"error": str(e)})
if __name__ == '__main__':
app.run(debug=True)