Update app.py
Browse files
app.py
CHANGED
@@ -480,7 +480,7 @@ def display_results(data):
|
|
480 |
st.warning("No data to display.")
|
481 |
return None
|
482 |
# Add the generate_literature_survey function below your other function definitions
|
483 |
-
def generate_literature_survey(papers, api_key="
|
484 |
"""
|
485 |
Generate a literature survey based on paper abstracts using Groq API with Llama-3.3-70B-Versatile
|
486 |
|
@@ -491,16 +491,19 @@ def generate_literature_survey(papers, api_key="gsk_G80LBPxmvDjQZ77zX0FIWGdyb3FY
|
|
491 |
Returns:
|
492 |
str: Generated literature survey
|
493 |
"""
|
|
|
494 |
if not papers or len(papers) == 0:
|
495 |
return "No papers found to generate a literature survey."
|
496 |
|
|
|
497 |
papers_with_abstracts = [p for p in papers if p.get("Abstract") and len(p.get("Abstract")) > 50]
|
498 |
|
499 |
if len(papers_with_abstracts) == 0:
|
500 |
return "Cannot generate a literature survey because none of the papers have substantial abstracts."
|
501 |
|
|
|
502 |
paper_info = []
|
503 |
-
for i, paper in enumerate(papers_with_abstracts[:10]):
|
504 |
paper_info.append(f"Paper {i+1}:\nTitle: {paper.get('Title', 'Unknown')}\nAuthors: {paper.get('Author(s)', 'Unknown')}\nYear: {paper.get('Published', 'Unknown')}\nAbstract: {paper.get('Abstract', 'No abstract available')}\n")
|
505 |
|
506 |
papers_text = "\n".join(paper_info)
|
@@ -518,14 +521,15 @@ Please organize the survey by themes rather than by individual papers, creating
|
|
518 |
Format your response with markdown headings for better readability.
|
519 |
"""
|
520 |
|
521 |
-
|
|
|
522 |
headers = {
|
523 |
"Authorization": f"Bearer {api_key}",
|
524 |
"Content-Type": "application/json"
|
525 |
}
|
526 |
|
527 |
data = {
|
528 |
-
"model": "llama-70b",
|
529 |
"messages": [
|
530 |
{"role": "system", "content": "You are an academic research assistant that creates comprehensive literature surveys."},
|
531 |
{"role": "user", "content": prompt}
|
@@ -536,11 +540,17 @@ Format your response with markdown headings for better readability.
|
|
536 |
|
537 |
try:
|
538 |
response = requests.post(url, headers=headers, data=json.dumps(data))
|
539 |
-
response.
|
|
|
|
|
540 |
result = response.json()
|
541 |
survey_text = result["choices"][0]["message"]["content"]
|
542 |
return survey_text
|
|
|
|
|
|
|
543 |
except Exception as e:
|
|
|
544 |
return f"Failed to generate literature survey due to an error: {str(e)}"
|
545 |
|
546 |
# Example usage
|
|
|
480 |
st.warning("No data to display.")
|
481 |
return None
|
482 |
# Add the generate_literature_survey function below your other function definitions
|
483 |
+
def generate_literature_survey(papers, api_key="YOUR_API_KEY"):
|
484 |
"""
|
485 |
Generate a literature survey based on paper abstracts using Groq API with Llama-3.3-70B-Versatile
|
486 |
|
|
|
491 |
Returns:
|
492 |
str: Generated literature survey
|
493 |
"""
|
494 |
+
# Check if we have papers with abstracts
|
495 |
if not papers or len(papers) == 0:
|
496 |
return "No papers found to generate a literature survey."
|
497 |
|
498 |
+
# Filter papers that have abstracts
|
499 |
papers_with_abstracts = [p for p in papers if p.get("Abstract") and len(p.get("Abstract")) > 50]
|
500 |
|
501 |
if len(papers_with_abstracts) == 0:
|
502 |
return "Cannot generate a literature survey because none of the papers have substantial abstracts."
|
503 |
|
504 |
+
# Construct the prompt for the LLM
|
505 |
paper_info = []
|
506 |
+
for i, paper in enumerate(papers_with_abstracts[:10]): # Limit to 10 papers to avoid token limits
|
507 |
paper_info.append(f"Paper {i+1}:\nTitle: {paper.get('Title', 'Unknown')}\nAuthors: {paper.get('Author(s)', 'Unknown')}\nYear: {paper.get('Published', 'Unknown')}\nAbstract: {paper.get('Abstract', 'No abstract available')}\n")
|
508 |
|
509 |
papers_text = "\n".join(paper_info)
|
|
|
521 |
Format your response with markdown headings for better readability.
|
522 |
"""
|
523 |
|
524 |
+
# Make the API request to Groq
|
525 |
+
url = "https://api.groq.com/openai/v1/completions" # Updated endpoint
|
526 |
headers = {
|
527 |
"Authorization": f"Bearer {api_key}",
|
528 |
"Content-Type": "application/json"
|
529 |
}
|
530 |
|
531 |
data = {
|
532 |
+
"model": "llama-3.3-70b-versatile",
|
533 |
"messages": [
|
534 |
{"role": "system", "content": "You are an academic research assistant that creates comprehensive literature surveys."},
|
535 |
{"role": "user", "content": prompt}
|
|
|
540 |
|
541 |
try:
|
542 |
response = requests.post(url, headers=headers, data=json.dumps(data))
|
543 |
+
print(f"Response Status Code: {response.status_code}") # Log status code
|
544 |
+
print(f"Response Body: {response.text}") # Log full response body
|
545 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
546 |
result = response.json()
|
547 |
survey_text = result["choices"][0]["message"]["content"]
|
548 |
return survey_text
|
549 |
+
except requests.exceptions.HTTPError as e:
|
550 |
+
print(f"HTTP Error: {e}")
|
551 |
+
return f"Failed to generate literature survey due to an HTTP error: {str(e)}"
|
552 |
except Exception as e:
|
553 |
+
print(f"Unexpected Error: {e}")
|
554 |
return f"Failed to generate literature survey due to an error: {str(e)}"
|
555 |
|
556 |
# Example usage
|