Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,356 @@
|
|
1 |
-
import
|
2 |
-
from
|
3 |
-
import
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
|
13 |
-
|
14 |
-
input_text = " ".join(chat_history)
|
15 |
-
input_ids = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors='pt')
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from flask_cors import CORS
|
3 |
+
import requests
|
4 |
+
from bs4 import BeautifulSoup
|
5 |
+
import os
|
6 |
|
7 |
+
app = Flask(__name__)
|
8 |
+
CORS(app)
|
9 |
|
10 |
+
# Gemini API key
|
11 |
+
GEMINI_API_KEY = "AIzaSyA7xnPR4Mv27-E-bBhiJmY4l4my_KlpuwY" # Replace with your real API key
|
12 |
+
if not GEMINI_API_KEY:
|
13 |
+
raise ValueError("GEMINI_API_KEY not set")
|
14 |
|
15 |
+
GEMINI_ENDPOINT = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={GEMINI_API_KEY}"
|
|
|
|
|
16 |
|
17 |
+
# Fixed URLs to scrape
|
18 |
+
FIXED_URLS = [
|
19 |
+
"https://www.bou.ac.bd/",
|
20 |
+
"https://bousst.edu.bd/"
|
21 |
+
]
|
22 |
|
23 |
+
def scrape_sites():
|
24 |
+
all_text = ""
|
25 |
+
headers = {
|
26 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0 Safari/537.36"
|
27 |
+
}
|
28 |
+
for url in FIXED_URLS:
|
29 |
+
try:
|
30 |
+
response = requests.get(url, headers=headers, timeout=10)
|
31 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
32 |
+
text = soup.get_text(separator=' ')
|
33 |
+
all_text += text + "\n\n"
|
34 |
+
except Exception as e:
|
35 |
+
print(f"Error scraping {url}: {e}")
|
36 |
+
return all_text[:3000] # limit to 3000 chars
|
37 |
|
38 |
+
def ask_gemini(context, question):
|
39 |
+
try:
|
40 |
+
prompt = f"Context: {context}\n\nQuestion: {question}"
|
41 |
+
payload = {
|
42 |
+
"contents": [
|
43 |
+
{
|
44 |
+
"parts": [
|
45 |
+
{"text": prompt}
|
46 |
+
]
|
47 |
+
}
|
48 |
+
]
|
49 |
+
}
|
50 |
|
51 |
+
response = requests.post(
|
52 |
+
GEMINI_ENDPOINT,
|
53 |
+
headers={"Content-Type": "application/json"},
|
54 |
+
json=payload,
|
55 |
+
timeout=15
|
56 |
+
)
|
57 |
+
response.raise_for_status()
|
58 |
+
data = response.json()
|
59 |
|
60 |
+
return data['candidates'][0]['content']['parts'][0]['text'].strip()
|
61 |
+
|
62 |
+
except Exception as e:
|
63 |
+
return f"Gemini API error: {str(e)}"
|
64 |
+
|
65 |
+
@app.route("/ask", methods=["POST"])
|
66 |
+
def ask():
|
67 |
+
data = request.get_json()
|
68 |
+
question = data.get("question", "").strip()
|
69 |
+
if not question:
|
70 |
+
return jsonify({"answer": "Please provide a question."})
|
71 |
+
|
72 |
+
context = scrape_sites()
|
73 |
+
answer = ask_gemini(context, question)
|
74 |
+
return jsonify({"answer": answer})
|
75 |
+
|
76 |
+
if __name__ == '__main__':
|
77 |
+
app.run(debug=True)
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
|
95 |
+
# from flask import Flask, request, jsonify
|
96 |
+
# from flask_cors import CORS
|
97 |
+
# import requests
|
98 |
+
# from bs4 import BeautifulSoup
|
99 |
+
# from openai import OpenAI # DeepSeek-compatible client
|
100 |
+
|
101 |
+
# app = Flask(__name__)
|
102 |
+
# CORS(app)
|
103 |
+
|
104 |
+
# # DeepSeek API key
|
105 |
+
# DEEPSEEK_API_KEY = "sk-9875cb19f5a54d49a59bc8db8cece52d" # Replace with your actual key
|
106 |
+
# if not DEEPSEEK_API_KEY:
|
107 |
+
# raise ValueError("DEEPSEEK_API_KEY not set")
|
108 |
+
|
109 |
+
# # Initialize DeepSeek-compatible client
|
110 |
+
# client = OpenAI(
|
111 |
+
# api_key=DEEPSEEK_API_KEY,
|
112 |
+
# base_url="https://api.deepseek.com"
|
113 |
+
# )
|
114 |
+
|
115 |
+
# # Fixed URLs to scrape
|
116 |
+
# FIXED_URLS = [
|
117 |
+
# "https://www.bou.ac.bd/",
|
118 |
+
# "https://bousst.edu.bd/"
|
119 |
+
# ]
|
120 |
+
|
121 |
+
# def scrape_sites():
|
122 |
+
# all_text = ""
|
123 |
+
# headers = {
|
124 |
+
# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0 Safari/537.36"
|
125 |
+
# }
|
126 |
+
# for url in FIXED_URLS:
|
127 |
+
# try:
|
128 |
+
# response = requests.get(url, headers=headers, timeout=10)
|
129 |
+
# soup = BeautifulSoup(response.text, 'html.parser')
|
130 |
+
# text = soup.get_text(separator=' ')
|
131 |
+
# all_text += text + "\n\n"
|
132 |
+
# except Exception as e:
|
133 |
+
# print(f"Error scraping {url}: {e}")
|
134 |
+
# return all_text[:3000] # limit to 3000 chars
|
135 |
+
|
136 |
+
|
137 |
+
# def ask_deepseek(context, question):
|
138 |
+
# try:
|
139 |
+
# messages = [
|
140 |
+
# {"role": "system", "content": "You are a helpful assistant that answers questions based on the given context."},
|
141 |
+
# {"role": "user", "content": f"Context: {context}\n\nQuestion: {question}"}
|
142 |
+
# ]
|
143 |
+
|
144 |
+
# response = client.chat.completions.create(
|
145 |
+
# model="deepseek-chat",
|
146 |
+
# messages=messages,
|
147 |
+
# max_tokens=300,
|
148 |
+
# temperature=0.7,
|
149 |
+
# )
|
150 |
+
|
151 |
+
# answer = response.choices[0].message.content.strip()
|
152 |
+
# return answer
|
153 |
+
|
154 |
+
# except Exception as e:
|
155 |
+
# return f"DeepSeek API error: {str(e)}"
|
156 |
+
|
157 |
+
|
158 |
+
# @app.route("/ask", methods=["POST"])
|
159 |
+
# def ask():
|
160 |
+
# data = request.get_json()
|
161 |
+
# question = data.get("question", "").strip()
|
162 |
+
# if not question:
|
163 |
+
# return jsonify({"answer": "Please provide a question."})
|
164 |
+
|
165 |
+
# context = scrape_sites()
|
166 |
+
# answer = ask_deepseek(context, question)
|
167 |
+
# return jsonify({"answer": answer})
|
168 |
+
|
169 |
+
|
170 |
+
# if __name__ == '__main__':
|
171 |
+
# app.run(debug=True)
|
172 |
+
|
173 |
+
|
174 |
+
|
175 |
+
|
176 |
+
|
177 |
+
|
178 |
+
|
179 |
+
|
180 |
+
|
181 |
+
|
182 |
+
|
183 |
+
|
184 |
+
|
185 |
+
|
186 |
+
|
187 |
+
|
188 |
+
|
189 |
+
|
190 |
+
|
191 |
+
# # from flask import Flask, request, jsonify
|
192 |
+
# # from flask_cors import CORS
|
193 |
+
# # import requests
|
194 |
+
# # from bs4 import BeautifulSoup
|
195 |
+
# # import os
|
196 |
+
# # import openai
|
197 |
+
|
198 |
+
# # app = Flask(__name__)
|
199 |
+
# # CORS(app)
|
200 |
+
|
201 |
+
# # # OpenAI API key from environment variable
|
202 |
+
# # OPENAI_API_KEY="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"
|
203 |
+
# # if not OPENAI_API_KEY:
|
204 |
+
# # raise ValueError("OPENAI_API_KEY environment variable not set")
|
205 |
+
|
206 |
+
# # openai.api_key = OPENAI_API_KEY
|
207 |
+
|
208 |
+
# # # Fixed URLs to scrape
|
209 |
+
# # FIXED_URLS = [
|
210 |
+
# # "https://www.bou.ac.bd/",
|
211 |
+
# # "https://bousst.edu.bd/"
|
212 |
+
# # ]
|
213 |
+
|
214 |
+
# # def scrape_sites():
|
215 |
+
# # all_text = ""
|
216 |
+
# # headers = {
|
217 |
+
# # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0 Safari/537.36"
|
218 |
+
# # }
|
219 |
+
# # for url in FIXED_URLS:
|
220 |
+
# # try:
|
221 |
+
# # response = requests.get(url, headers=headers, timeout=10)
|
222 |
+
# # soup = BeautifulSoup(response.text, 'html.parser')
|
223 |
+
# # text = soup.get_text(separator=' ')
|
224 |
+
# # all_text += text + "\n\n"
|
225 |
+
# # except Exception as e:
|
226 |
+
# # print(f"Error scraping {url}: {e}")
|
227 |
+
# # return all_text[:3000] # limit to 3000 chars
|
228 |
+
|
229 |
+
|
230 |
+
# # import openai
|
231 |
+
|
232 |
+
# # openai.api_key = OPENAI_API_KEY
|
233 |
+
|
234 |
+
# # def ask_openai(context, question):
|
235 |
+
# # try:
|
236 |
+
# # messages = [
|
237 |
+
# # {"role": "system", "content": "You are a helpful assistant that answers questions based on the given context."},
|
238 |
+
# # {"role": "user", "content": f"Context: {context}\n\nQuestion: {question}"}
|
239 |
+
# # ]
|
240 |
+
|
241 |
+
# # response = openai.chat.completions.create(
|
242 |
+
# # model="gpt-3.5-turbo",
|
243 |
+
# # messages=messages,
|
244 |
+
# # max_tokens=300,
|
245 |
+
# # temperature=0.7,
|
246 |
+
# # )
|
247 |
+
|
248 |
+
# # answer = response.choices[0].message.content.strip()
|
249 |
+
# # return answer
|
250 |
+
|
251 |
+
# # except Exception as e:
|
252 |
+
# # return f"OpenAI API error: {str(e)}"
|
253 |
+
|
254 |
+
|
255 |
+
# # @app.route("/ask", methods=["POST"])
|
256 |
+
# # def ask():
|
257 |
+
# # data = request.get_json()
|
258 |
+
# # question = data.get("question", "").strip()
|
259 |
+
# # if not question:
|
260 |
+
# # return jsonify({"answer": "Please provide a question."})
|
261 |
+
|
262 |
+
# # context = scrape_sites()
|
263 |
+
# # answer = ask_openai(context, question)
|
264 |
+
# # return jsonify({"answer": answer})
|
265 |
+
|
266 |
+
# # if __name__ == '__main__':
|
267 |
+
# # app.run(debug=True)
|
268 |
+
|
269 |
+
|
270 |
+
|
271 |
+
|
272 |
+
|
273 |
+
|
274 |
+
|
275 |
+
|
276 |
+
|
277 |
+
|
278 |
+
|
279 |
+
# # # from flask import Flask, request, jsonify
|
280 |
+
# # # from flask_cors import CORS
|
281 |
+
# # # import requests
|
282 |
+
# # # from bs4 import BeautifulSoup
|
283 |
+
# # # import os
|
284 |
+
|
285 |
+
# # # app = Flask(__name__)
|
286 |
+
# # # CORS(app)
|
287 |
+
|
288 |
+
# # # # Hugging Face API setup
|
289 |
+
# # # HUGGINGFACE_API_URL = "https://api-inference.huggingface.co/models/deepseek-ai/deepseek-llm-7b-chat"
|
290 |
+
# # # HUGGINGFACE_TOKEN = os.getenv("token_hf")
|
291 |
+
|
292 |
+
# # # if not HUGGINGFACE_TOKEN:
|
293 |
+
# # # raise ValueError("HUGGINGFACE_TOKEN environment variable not set")
|
294 |
+
|
295 |
+
# # # HEADERS = {
|
296 |
+
# # # "Authorization": f"Bearer {HUGGINGFACE_TOKEN}"
|
297 |
+
# # # }
|
298 |
+
|
299 |
+
# # # # Fixed URLs to scrape
|
300 |
+
# # # FIXED_URLS = [
|
301 |
+
# # # "https://www.bou.ac.bd/",
|
302 |
+
# # # "https://bousst.edu.bd/"
|
303 |
+
# # # ]
|
304 |
+
|
305 |
+
# # # # Scrape and extract text from the target websites
|
306 |
+
# # # def scrape_sites():
|
307 |
+
# # # all_text = ""
|
308 |
+
# # # headers = {
|
309 |
+
# # # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/91.0 Safari/537.36"
|
310 |
+
# # # }
|
311 |
+
|
312 |
+
# # # for url in FIXED_URLS:
|
313 |
+
# # # try:
|
314 |
+
# # # response = requests.get(url, headers=headers, timeout=10)
|
315 |
+
# # # soup = BeautifulSoup(response.text, 'html.parser')
|
316 |
+
# # # text = soup.get_text(separator=' ')
|
317 |
+
# # # all_text += text + "\n\n"
|
318 |
+
# # # except Exception as e:
|
319 |
+
# # # print(f"Error scraping {url}: {e}")
|
320 |
+
|
321 |
+
# # # return all_text[:3000] # Limit to 3000 characters for performance
|
322 |
+
|
323 |
+
# # # # Send context + question to Hugging Face API
|
324 |
+
# # # def ask_deepseek(context, question):
|
325 |
+
# # # payload = {
|
326 |
+
# # # "inputs": f"Context: {context}\n\nQuestion: {question}",
|
327 |
+
# # # "parameters": {"max_new_tokens": 300}
|
328 |
+
# # # }
|
329 |
+
|
330 |
+
# # # try:
|
331 |
+
# # # res = requests.post(HUGGINGFACE_API_URL, headers=HEADERS, json=payload)
|
332 |
+
# # # result = res.json()
|
333 |
+
# # # print("Hugging Face response:", result)
|
334 |
+
|
335 |
+
# # # if isinstance(result, list) and "generated_text" in result[0]:
|
336 |
+
# # # return result[0]["generated_text"].split("Question:")[-1].strip()
|
337 |
+
# # # else:
|
338 |
+
# # # return f"DeepSeek error: {result.get('error', 'Unknown error')}"
|
339 |
+
# # # except Exception as e:
|
340 |
+
# # # return f"Error contacting DeepSeek API: {e}"
|
341 |
+
|
342 |
+
# # # # API endpoint
|
343 |
+
# # # @app.route("/ask", methods=["POST"])
|
344 |
+
# # # def ask():
|
345 |
+
# # # data = request.get_json()
|
346 |
+
# # # question = data.get("question", "").strip()
|
347 |
+
|
348 |
+
# # # if not question:
|
349 |
+
# # # return jsonify({"answer": "Please provide a question."})
|
350 |
+
|
351 |
+
# # # context = scrape_sites()
|
352 |
+
# # # answer = ask_deepseek(context, question)
|
353 |
+
# # # return jsonify({"answer": answer})
|
354 |
+
|
355 |
+
# # # if __name__ == '__main__':
|
356 |
+
# # # app.run(debug=True)
|