Spaces:
Sleeping
Sleeping
Omar ID EL MOUMEN
commited on
Commit
·
26ddf5d
1
Parent(s):
ad66b01
Change response
Browse files- app.py +8 -21
- static/script.js +2 -2
app.py
CHANGED
|
@@ -105,17 +105,7 @@ async def extract_arxiv_pdf(document: DocumentID):
|
|
| 105 |
postprocess_text = remove_punctuations(postprocess_text)
|
| 106 |
postprocess_text = re.sub(r"\s+", " ", postprocess_text)
|
| 107 |
postprocess_text = postprocess_text.strip()
|
| 108 |
-
|
| 109 |
-
titles = doc.get_toc()
|
| 110 |
-
main_titles = []
|
| 111 |
-
if len(titles) <= 0:
|
| 112 |
-
main_titles = re.findall(regex_titles, postprocess_text, flags=re.MULTILINE)
|
| 113 |
-
main_titles = [(-1, t) for t in main_titles]
|
| 114 |
-
else:
|
| 115 |
-
for title in titles:
|
| 116 |
-
if title[0] == 1 or title[0] == 2:
|
| 117 |
-
main_titles.append((title[0], title[1]))
|
| 118 |
-
return {"pub_id": document.doc_id, "text": postprocess_text, "error": False} if len(main_titles) > 0 else {"pub_id": document.doc_id, "titles": "No titles found !", "text": postprocess_text, "error": False}
|
| 119 |
else:
|
| 120 |
print("ID: " + document.doc_id)
|
| 121 |
print("URL: " + f"http://arxiv.org/pdf/{document.doc_id}")
|
|
@@ -136,7 +126,7 @@ async def extract_pdf(pdf: WebPDF):
|
|
| 136 |
postprocess_text = remove_punctuations(postprocess_text)
|
| 137 |
postprocess_text = re.sub(r"\s+", " ", postprocess_text)
|
| 138 |
postprocess_text = postprocess_text.strip()
|
| 139 |
-
return {"error": False, "title": pdf_metadata.get("title", "").strip(), "text": postprocess_text}
|
| 140 |
else:
|
| 141 |
print("URL: " + pdf.url)
|
| 142 |
print("Status code: " + str(pdf_req.status_code))
|
|
@@ -350,18 +340,15 @@ research how new business models beyond ROI could benefit societal impact of 6G
|
|
| 350 |
response = requests.post(url, headers=headers, data=json.dumps(data), verify=False)
|
| 351 |
if response.status_code != 200:
|
| 352 |
print(f"Groq API error on post: {response.status_code}")
|
| 353 |
-
return
|
| 354 |
|
| 355 |
try:
|
| 356 |
response_data = response.json()
|
| 357 |
raw_content = response_data['choices'][0]['message']['content'].strip()
|
| 358 |
-
print(response_data)
|
| 359 |
-
print("Tokens utilisé (prompt): ", response_data['usage']['prompt_tokens'])
|
| 360 |
-
print("Tokens utilisé (total): ", response_data['usage']['total_tokens'])
|
| 361 |
return raw_content
|
| 362 |
except Exception as e:
|
| 363 |
print(f"Groq API error after post: {str(e)}")
|
| 364 |
-
return
|
| 365 |
|
| 366 |
class GroqRequest(BaseModel):
|
| 367 |
model: str
|
|
@@ -370,12 +357,12 @@ class GroqRequest(BaseModel):
|
|
| 370 |
@app.post("/search/plan")
|
| 371 |
async def get_research_plan(infos: GroqRequest, api_key: str = Header(None, alias="GROQ_TOKEN")):
|
| 372 |
if api_key is None:
|
| 373 |
-
return {"error": True, "message": "Missing API key"
|
| 374 |
plan = researcher(infos.model, infos.user, api_key)
|
| 375 |
-
if plan
|
| 376 |
-
return {"error": True, "
|
| 377 |
plan = json.loads(re.sub(r"\s+", " ", plan))
|
| 378 |
-
return {"error": False, "plan": plan}
|
| 379 |
|
| 380 |
@app.post("/search/plan/arxiv")
|
| 381 |
async def get_arxiv_research_plan(infos: GroqRequest, api_key: str = Header(None, alias="GROQ_TOKEN")):
|
|
|
|
| 105 |
postprocess_text = remove_punctuations(postprocess_text)
|
| 106 |
postprocess_text = re.sub(r"\s+", " ", postprocess_text)
|
| 107 |
postprocess_text = postprocess_text.strip()
|
| 108 |
+
return {"error": False, "message": {"pub_id": document.doc_id, "text": postprocess_text}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
else:
|
| 110 |
print("ID: " + document.doc_id)
|
| 111 |
print("URL: " + f"http://arxiv.org/pdf/{document.doc_id}")
|
|
|
|
| 126 |
postprocess_text = remove_punctuations(postprocess_text)
|
| 127 |
postprocess_text = re.sub(r"\s+", " ", postprocess_text)
|
| 128 |
postprocess_text = postprocess_text.strip()
|
| 129 |
+
return {"error": False, "message": {"title": pdf_metadata.get("title", "No title found").strip(), "text": postprocess_text}}
|
| 130 |
else:
|
| 131 |
print("URL: " + pdf.url)
|
| 132 |
print("Status code: " + str(pdf_req.status_code))
|
|
|
|
| 340 |
response = requests.post(url, headers=headers, data=json.dumps(data), verify=False)
|
| 341 |
if response.status_code != 200:
|
| 342 |
print(f"Groq API error on post: {response.status_code}")
|
| 343 |
+
return None
|
| 344 |
|
| 345 |
try:
|
| 346 |
response_data = response.json()
|
| 347 |
raw_content = response_data['choices'][0]['message']['content'].strip()
|
|
|
|
|
|
|
|
|
|
| 348 |
return raw_content
|
| 349 |
except Exception as e:
|
| 350 |
print(f"Groq API error after post: {str(e)}")
|
| 351 |
+
return None
|
| 352 |
|
| 353 |
class GroqRequest(BaseModel):
|
| 354 |
model: str
|
|
|
|
| 357 |
@app.post("/search/plan")
|
| 358 |
async def get_research_plan(infos: GroqRequest, api_key: str = Header(None, alias="GROQ_TOKEN")):
|
| 359 |
if api_key is None:
|
| 360 |
+
return {"error": True, "message": "Missing API key"}
|
| 361 |
plan = researcher(infos.model, infos.user, api_key)
|
| 362 |
+
if plan is None:
|
| 363 |
+
return {"error": True, "message": "Error while generating the research plan"}
|
| 364 |
plan = json.loads(re.sub(r"\s+", " ", plan))
|
| 365 |
+
return {"error": False, "message": {"plan": plan}}
|
| 366 |
|
| 367 |
@app.post("/search/plan/arxiv")
|
| 368 |
async def get_arxiv_research_plan(infos: GroqRequest, api_key: str = Header(None, alias="GROQ_TOKEN")):
|
static/script.js
CHANGED
|
@@ -50,7 +50,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
| 50 |
return;
|
| 51 |
}
|
| 52 |
|
| 53 |
-
displayPlan(data.plan);
|
| 54 |
} catch (error) {
|
| 55 |
console.error('Search error:', error);
|
| 56 |
resultsContainer.innerHTML = `<div class="error-message">Error performing generation: ${error.message}</div>`;
|
|
@@ -198,7 +198,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|
| 198 |
}
|
| 199 |
text = "";
|
| 200 |
|
| 201 |
-
popupText.textContent = data.text;
|
| 202 |
} catch (error) {
|
| 203 |
console.error('Search error:', error);
|
| 204 |
popupText.innerHTML = `<div class="error-message">Error performing search: ${error.message}</div>`;
|
|
|
|
| 50 |
return;
|
| 51 |
}
|
| 52 |
|
| 53 |
+
displayPlan(data.message.plan);
|
| 54 |
} catch (error) {
|
| 55 |
console.error('Search error:', error);
|
| 56 |
resultsContainer.innerHTML = `<div class="error-message">Error performing generation: ${error.message}</div>`;
|
|
|
|
| 198 |
}
|
| 199 |
text = "";
|
| 200 |
|
| 201 |
+
popupText.textContent = data.message.text;
|
| 202 |
} catch (error) {
|
| 203 |
console.error('Search error:', error);
|
| 204 |
popupText.innerHTML = `<div class="error-message">Error performing search: ${error.message}</div>`;
|