starkbyte45896 commited on
Commit
c41b2ce
Β·
verified Β·
1 Parent(s): ef9f319

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -21
app.py CHANGED
@@ -1,34 +1,34 @@
1
  from flask import Flask, request, jsonify
2
  from pymongo import MongoClient
 
3
  import requests
4
  from bs4 import BeautifulSoup
5
- import os
6
 
7
  app = Flask(__name__)
8
 
9
- # MongoDB Connection
10
- MONGO_URI = os.getenv("MONGO_URI", "mongodb+srv://<username>:<password>@cluster0.mongodb.net/?retryWrites=true&w=majority")
11
  client = MongoClient(MONGO_URI)
12
- db = client["ravi_db"]
13
- qna_collection = db["qna"]
14
 
15
- # πŸ“Œ Add a new QnA pair
16
  @app.route('/add_qna', methods=['POST'])
17
  def add_qna():
18
  data = request.json
19
- question = data.get("question").strip().lower()
20
  answer = data.get("answer")
21
 
22
  if not question or not answer:
23
  return jsonify({"error": "Both question and answer are required"}), 400
24
 
25
- if qna_collection.find_one({"question": question}):
26
  return jsonify({"error": "Question already exists"}), 409
27
 
28
- qna_collection.insert_one({"question": question, "answer": answer})
29
  return jsonify({"message": "QnA added successfully!"}), 201
30
 
31
- # πŸ“Œ Get answer for a question
32
  @app.route('/ask', methods=['POST'])
33
  def ask():
34
  data = request.json
@@ -37,45 +37,68 @@ def ask():
37
  if not question:
38
  return jsonify({"error": "Question is required"}), 400
39
 
40
- result = qna_collection.find_one({"question": question})
41
-
42
  if result:
43
- return jsonify({"answer": result["answer"]})
44
  else:
45
  return jsonify({"answer": web_scrape_answer(question)}) # Fetch from web
46
 
47
- # πŸ“Œ List all stored QnA
48
  @app.route('/list_qna', methods=['GET'])
49
  def list_qna():
50
- data = list(qna_collection.find({}, {"_id": 0, "question": 1, "answer": 1}))
51
- return jsonify({"qna_list": data})
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- # πŸ“Œ Delete a QnA pair
 
 
 
 
 
54
  @app.route('/delete_qna', methods=['DELETE'])
55
  def delete_qna():
56
  data = request.json
57
- question = data.get("question").strip().lower()
58
 
59
  if not question:
60
  return jsonify({"error": "Question is required"}), 400
61
 
62
- qna_collection.delete_one({"question": question})
 
 
 
 
63
  return jsonify({"message": "QnA deleted successfully!"})
64
 
65
  # πŸ“Œ Web Scraping Function (GeeksforGeeks)
66
  def web_scrape_answer(question):
67
  search_query = question.replace(" ", "+")
68
  url = f"https://www.geeksforgeeks.org/?s={search_query}"
 
69
  headers = {"User-Agent": "Mozilla/5.0"}
70
-
71
  response = requests.get(url, headers=headers)
 
72
  if response.status_code == 200:
73
  soup = BeautifulSoup(response.text, "html.parser")
74
  first_result = soup.find("div", class_="entry-content")
 
75
  if first_result:
76
  return first_result.text.strip()
77
 
78
  return "I couldn't find an answer. Try rephrasing the question."
79
 
80
  if __name__ == '__main__':
81
- app.run(host="0.0.0.0", port=7860)
 
1
  from flask import Flask, request, jsonify
2
  from pymongo import MongoClient
3
+ import os
4
  import requests
5
  from bs4 import BeautifulSoup
 
6
 
7
  app = Flask(__name__)
8
 
9
+ # Load MongoDB URI from Hugging Face Secrets
10
+ MONGO_URI = os.getenv("MONGO_URI")
11
  client = MongoClient(MONGO_URI)
12
+ db = client["Ravi"]
13
+ collection = db["qna"]
14
 
15
+ # πŸ“Œ Add a Question-Answer Pair
16
  @app.route('/add_qna', methods=['POST'])
17
  def add_qna():
18
  data = request.json
19
+ question = data.get("question")
20
  answer = data.get("answer")
21
 
22
  if not question or not answer:
23
  return jsonify({"error": "Both question and answer are required"}), 400
24
 
25
+ if collection.find_one({"question": question.lower()}):
26
  return jsonify({"error": "Question already exists"}), 409
27
 
28
+ collection.insert_one({"question": question.lower(), "answer": answer})
29
  return jsonify({"message": "QnA added successfully!"}), 201
30
 
31
+ # πŸ“Œ Ask a Question (Retrieve Answer)
32
  @app.route('/ask', methods=['POST'])
33
  def ask():
34
  data = request.json
 
37
  if not question:
38
  return jsonify({"error": "Question is required"}), 400
39
 
40
+ result = collection.find_one({"question": question})
41
+
42
  if result:
43
+ return jsonify({"answer": result["answer"]}) # Return stored answer
44
  else:
45
  return jsonify({"answer": web_scrape_answer(question)}) # Fetch from web
46
 
47
+ # πŸ“Œ List All Stored Questions & Answers
48
  @app.route('/list_qna', methods=['GET'])
49
  def list_qna():
50
+ qna_list = list(collection.find({}, {"_id": 0})) # Exclude MongoDB `_id`
51
+ return jsonify({"qna_list": qna_list})
52
+
53
+ # πŸ“Œ Update an Existing QnA
54
+ @app.route('/update_qna', methods=['PUT'])
55
+ def update_qna():
56
+ data = request.json
57
+ question = data.get("question")
58
+ new_answer = data.get("answer")
59
+
60
+ if not question or not new_answer:
61
+ return jsonify({"error": "Both question and new answer are required"}), 400
62
+
63
+ result = collection.update_one({"question": question.lower()}, {"$set": {"answer": new_answer}})
64
 
65
+ if result.matched_count == 0:
66
+ return jsonify({"error": "Question not found"}), 404
67
+
68
+ return jsonify({"message": "QnA updated successfully!"})
69
+
70
+ # πŸ“Œ Delete a QnA Entry
71
  @app.route('/delete_qna', methods=['DELETE'])
72
  def delete_qna():
73
  data = request.json
74
+ question = data.get("question")
75
 
76
  if not question:
77
  return jsonify({"error": "Question is required"}), 400
78
 
79
+ result = collection.delete_one({"question": question.lower()})
80
+
81
+ if result.deleted_count == 0:
82
+ return jsonify({"error": "Question not found"}), 404
83
+
84
  return jsonify({"message": "QnA deleted successfully!"})
85
 
86
  # πŸ“Œ Web Scraping Function (GeeksforGeeks)
87
  def web_scrape_answer(question):
88
  search_query = question.replace(" ", "+")
89
  url = f"https://www.geeksforgeeks.org/?s={search_query}"
90
+
91
  headers = {"User-Agent": "Mozilla/5.0"}
 
92
  response = requests.get(url, headers=headers)
93
+
94
  if response.status_code == 200:
95
  soup = BeautifulSoup(response.text, "html.parser")
96
  first_result = soup.find("div", class_="entry-content")
97
+
98
  if first_result:
99
  return first_result.text.strip()
100
 
101
  return "I couldn't find an answer. Try rephrasing the question."
102
 
103
  if __name__ == '__main__':
104
+ app.run(host="0.0.0.0", port=7860, debug=True)