Spaces:
Runtime error
Runtime error
File size: 9,766 Bytes
e8ab43d b26e611 e8ab43d b26e611 e8ab43d b26e611 e8ab43d b26e611 e8ab43d b26e611 e8ab43d b26e611 e8ab43d b26e611 e8ab43d bbb8c1f e8ab43d |
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
from flask import Flask, request, jsonify
from flask_cors import CORS
import requests
from bs4 import BeautifulSoup
import os
app = Flask(__name__)
CORS(app)
# Gemini API key
GEMINI_API_KEY = "AIzaSyA7xnPR4Mv27-E-bBhiJmY4l4my_KlpuwY" # Replace with your real API key
if not GEMINI_API_KEY:
raise ValueError("GEMINI_API_KEY not set")
GEMINI_ENDPOINT = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={GEMINI_API_KEY}"
# Fixed URLs to scrape
FIXED_URLS = [
"https://www.bou.ac.bd/",
"https://bousst.edu.bd/"
]
def scrape_sites():
all_text = ""
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0 Safari/537.36"
}
for url in FIXED_URLS:
try:
response = requests.get(url, headers=headers, timeout=10)
soup = BeautifulSoup(response.text, 'html.parser')
text = soup.get_text(separator=' ')
all_text += text + "\n\n"
except Exception as e:
print(f"Error scraping {url}: {e}")
return all_text[:3000] # limit to 3000 chars
def ask_gemini(context, question):
try:
prompt = f"Context: {context}\n\nQuestion: {question}"
payload = {
"contents": [
{
"parts": [
{"text": prompt}
]
}
]
}
response = requests.post(
GEMINI_ENDPOINT,
headers={"Content-Type": "application/json"},
json=payload,
timeout=15
)
response.raise_for_status()
data = response.json()
return data['candidates'][0]['content']['parts'][0]['text'].strip()
except Exception as e:
return f"Gemini API error: {str(e)}"
@app.route("/ask", methods=["POST"])
def ask():
data = request.get_json()
question = data.get("question", "").strip()
if not question:
return jsonify({"answer": "Please provide a question."})
context = scrape_sites()
answer = ask_gemini(context, question)
return jsonify({"answer": answer})
if __name__ == '__main__':
app.run(debug=True)
# from flask import Flask, request, jsonify
# from flask_cors import CORS
# import requests
# from bs4 import BeautifulSoup
# from openai import OpenAI # DeepSeek-compatible client
# app = Flask(__name__)
# CORS(app)
# # DeepSeek API key
# DEEPSEEK_API_KEY = "sk-9875cb19f5a54d49a59bc8db8cece52d" # Replace with your actual key
# if not DEEPSEEK_API_KEY:
# raise ValueError("DEEPSEEK_API_KEY not set")
# # Initialize DeepSeek-compatible client
# client = OpenAI(
# api_key=DEEPSEEK_API_KEY,
# base_url="https://api.deepseek.com"
# )
# # Fixed URLs to scrape
# FIXED_URLS = [
# "https://www.bou.ac.bd/",
# "https://bousst.edu.bd/"
# ]
# def scrape_sites():
# all_text = ""
# headers = {
# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0 Safari/537.36"
# }
# for url in FIXED_URLS:
# try:
# response = requests.get(url, headers=headers, timeout=10)
# soup = BeautifulSoup(response.text, 'html.parser')
# text = soup.get_text(separator=' ')
# all_text += text + "\n\n"
# except Exception as e:
# print(f"Error scraping {url}: {e}")
# return all_text[:3000] # limit to 3000 chars
# def ask_deepseek(context, question):
# try:
# messages = [
# {"role": "system", "content": "You are a helpful assistant that answers questions based on the given context."},
# {"role": "user", "content": f"Context: {context}\n\nQuestion: {question}"}
# ]
# response = client.chat.completions.create(
# model="deepseek-chat",
# messages=messages,
# max_tokens=300,
# temperature=0.7,
# )
# answer = response.choices[0].message.content.strip()
# return answer
# except Exception as e:
# return f"DeepSeek API error: {str(e)}"
# @app.route("/ask", methods=["POST"])
# def ask():
# data = request.get_json()
# question = data.get("question", "").strip()
# if not question:
# return jsonify({"answer": "Please provide a question."})
# context = scrape_sites()
# answer = ask_deepseek(context, question)
# return jsonify({"answer": answer})
# if __name__ == '__main__':
# app.run(debug=True)
# # from flask import Flask, request, jsonify
# # from flask_cors import CORS
# # import requests
# # from bs4 import BeautifulSoup
# # import os
# # import openai
# # app = Flask(__name__)
# # CORS(app)
# # # OpenAI API key from environment variable
# # OPENAI_API_KEY="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"
# # if not OPENAI_API_KEY:
# # raise ValueError("OPENAI_API_KEY environment variable not set")
# # openai.api_key = OPENAI_API_KEY
# # # Fixed URLs to scrape
# # FIXED_URLS = [
# # "https://www.bou.ac.bd/",
# # "https://bousst.edu.bd/"
# # ]
# # def scrape_sites():
# # all_text = ""
# # headers = {
# # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0 Safari/537.36"
# # }
# # for url in FIXED_URLS:
# # try:
# # response = requests.get(url, headers=headers, timeout=10)
# # soup = BeautifulSoup(response.text, 'html.parser')
# # text = soup.get_text(separator=' ')
# # all_text += text + "\n\n"
# # except Exception as e:
# # print(f"Error scraping {url}: {e}")
# # return all_text[:3000] # limit to 3000 chars
# # import openai
# # openai.api_key = OPENAI_API_KEY
# # def ask_openai(context, question):
# # try:
# # messages = [
# # {"role": "system", "content": "You are a helpful assistant that answers questions based on the given context."},
# # {"role": "user", "content": f"Context: {context}\n\nQuestion: {question}"}
# # ]
# # response = openai.chat.completions.create(
# # model="gpt-3.5-turbo",
# # messages=messages,
# # max_tokens=300,
# # temperature=0.7,
# # )
# # answer = response.choices[0].message.content.strip()
# # return answer
# # except Exception as e:
# # return f"OpenAI API error: {str(e)}"
# # @app.route("/ask", methods=["POST"])
# # def ask():
# # data = request.get_json()
# # question = data.get("question", "").strip()
# # if not question:
# # return jsonify({"answer": "Please provide a question."})
# # context = scrape_sites()
# # answer = ask_openai(context, question)
# # return jsonify({"answer": answer})
# # if __name__ == '__main__':
# # app.run(debug=True)
# # # from flask import Flask, request, jsonify
# # # from flask_cors import CORS
# # # import requests
# # # from bs4 import BeautifulSoup
# # # import os
# # # app = Flask(__name__)
# # # CORS(app)
# # # # Hugging Face API setup
# # # HUGGINGFACE_API_URL = "https://api-inference.huggingface.co/models/deepseek-ai/deepseek-llm-7b-chat"
# # # HUGGINGFACE_TOKEN = os.getenv("token_hf")
# # # if not HUGGINGFACE_TOKEN:
# # # raise ValueError("HUGGINGFACE_TOKEN environment variable not set")
# # # HEADERS = {
# # # "Authorization": f"Bearer {HUGGINGFACE_TOKEN}"
# # # }
# # # # Fixed URLs to scrape
# # # FIXED_URLS = [
# # # "https://www.bou.ac.bd/",
# # # "https://bousst.edu.bd/"
# # # ]
# # # # Scrape and extract text from the target websites
# # # def scrape_sites():
# # # all_text = ""
# # # headers = {
# # # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0 Safari/537.36"
# # # }
# # # for url in FIXED_URLS:
# # # try:
# # # response = requests.get(url, headers=headers, timeout=10)
# # # soup = BeautifulSoup(response.text, 'html.parser')
# # # text = soup.get_text(separator=' ')
# # # all_text += text + "\n\n"
# # # except Exception as e:
# # # print(f"Error scraping {url}: {e}")
# # # return all_text[:3000] # Limit to 3000 characters for performance
# # # # Send context + question to Hugging Face API
# # # def ask_deepseek(context, question):
# # # payload = {
# # # "inputs": f"Context: {context}\n\nQuestion: {question}",
# # # "parameters": {"max_new_tokens": 300}
# # # }
# # # try:
# # # res = requests.post(HUGGINGFACE_API_URL, headers=HEADERS, json=payload)
# # # result = res.json()
# # # print("Hugging Face response:", result)
# # # if isinstance(result, list) and "generated_text" in result[0]:
# # # return result[0]["generated_text"].split("Question:")[-1].strip()
# # # else:
# # # return f"DeepSeek error: {result.get('error', 'Unknown error')}"
# # # except Exception as e:
# # # return f"Error contacting DeepSeek API: {e}"
# # # # API endpoint
# # # @app.route("/ask", methods=["POST"])
# # # def ask():
# # # data = request.get_json()
# # # question = data.get("question", "").strip()
# # # if not question:
# # # return jsonify({"answer": "Please provide a question."})
# # # context = scrape_sites()
# # # answer = ask_deepseek(context, question)
# # # return jsonify({"answer": answer})
# # # if __name__ == '__main__':
# # # app.run(debug=True) |