Spaces:
Sleeping
Sleeping
File size: 7,143 Bytes
ca4e4fd f7bf685 ca4e4fd f7bf685 ca4e4fd |
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 |
import os
import streamlit as st
from dotenv import load_dotenv
import socket
from groq import Groq
from scholarly import scholarly
from prompts import get_research_prompt, get_guidance_prompt, invalid_question_prompt
# Load environment variables from (.env) file
load_dotenv()
# Set up Groq client using st.secrets for secure API key handling
# api_key = st.secrets["GROQ_API_KEY"] # Haalt API key uit een toml file (hier niet van toepassing)
# groq_api_key = os.environ['GROQ_API_KEY'] # Dit is de methode om de GROQ API key op te halen in HF Spaces omgeving
groq_api_key = os.environ['GROQ_API_KEY']
# print("groq_api_key: ", groq_api_key)
if not groq_api_key:
st.error("GROQ_API_KEY not found. Please check your environment variables.")
else:
client = Groq(api_key=groq_api_key)
# Function to check internet connection
def is_connected():
try:
socket.create_connection(("www.google.com", 80))
return True
except OSError:
return False
# Streamlit UI Enhancements
st.set_page_config(page_title="AI Researcher Pro - Your Research Companion", layout="wide")
# Custom CSS for styling
st.markdown("""
<style>
body {
font-family: 'Arial', sans-serif;
}
.stButton button {
border-radius: 10px;
padding: 10px 20px;
}
.big-title {
font-size: 80px;
font-weight: bold;
color: #d84df1;
text-align: center;
background: linear-gradient(90deg, #4285f4, #ea4335, #fbbc05, #34a853);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.subtitle-style {
font-size: 18px;
text-align: center;
margin-bottom: 20px;
}
.tooltip {
color: gray;
font-style: italic;
font-size: 14px;
}
.sidebar-title {
font-size: 18px;
font-weight: bold;
color: #c24838;
margin-bottom: 10px;
}
.research-field {
font-weight: bold;
color: #c24838;
font-size: 14px;
}
</style>
""", unsafe_allow_html=True)
# Title and subtitle with custom styling
st.markdown("<h1 class='big-title'>AI Researcher Pro</h1>", unsafe_allow_html=True)
st.markdown("<p class='subtitle-style'>Your AI-powered research assistant for all your research needs!</p>", unsafe_allow_html=True)
# Adding a tooltip with enhanced styling
st.markdown("""
<p class='tooltip'>Tip: Ask specific research questions like "What are the latest trends in AI?" or "How to improve model accuracy in NLP?"</p>
""", unsafe_allow_html=True)
# Connectivity Check
if not is_connected():
st.error("β οΈ No internet connection. Please check your internet and try again.")
st.stop()
# Define fields and their respective sub-fields
fields_dict = {
"Computer Science": ["Artificial Intelligence", "Machine Learning", "Data Science", "Computer Vision", "Natural Language Processing"],
"Medical": ["Biotechnology", "Genetics", "Neuroscience", "Immunology", "Medical Imaging"],
"Physics": ["Quantum Mechanics", "Astrophysics", "Nuclear Physics", "Condensed Matter Physics", "Particle Physics"],
"Chemistry": ["Organic Chemistry", "Inorganic Chemistry", "Biochemistry", "Physical Chemistry", "Analytical Chemistry"],
"Engineering": ["Electrical Engineering", "Mechanical Engineering", "Civil Engineering", "Aerospace Engineering", "Biomedical Engineering"]
}
# Sidebar for selecting broad research fields
st.sidebar.markdown("<div class='sidebar-title'>Select a Broad Research Field</div>", unsafe_allow_html=True)
broad_field = st.sidebar.selectbox("Research Field", list(fields_dict.keys()))
# Show relevant sub-fields based on the broad field selected
st.sidebar.markdown(f"<div class='sidebar-title'>Select Sub-fields in {broad_field}</div>", unsafe_allow_html=True)
selected_subfields = []
for subfield in fields_dict[broad_field]:
if st.sidebar.checkbox(f"π {subfield}", key=subfield):
selected_subfields.append(subfield)
# User input in the main layout
st.markdown("<div class='research-field'>Research Question</div>", unsafe_allow_html=True)
research_question = st.text_input("", "", help="Enter a clear research-related question")
# Function to get research papers from Google Scholar
def get_research_papers_from_scholar(topic, fields):
papers = []
for field in fields:
search_query = scholarly.search_pubs(f"{topic} {field}")
for i in range(5): # Get top 5 results for each field
try:
paper = next(search_query)
title = paper['bib']['title']
abstract = paper.get('bib', {}).get('abstract', "No abstract available")
url = paper.get('pub_url', "No URL available")
papers.append({
"title": title,
"abstract": abstract,
"url": url,
"field": field
})
except StopIteration:
break
return papers
# Function to simulate AI researcher's response
def get_researcher_response(question, fields):
subfields = ", ".join(fields)
chat_completion = client.chat.completions.create(
messages=[get_research_prompt(question, subfields)],
model="llama3-groq-70b-8192-tool-use-preview",
)
answer = chat_completion.choices[0].message.content
if "non-research" in answer.lower():
return invalid_question_prompt(), None
guidance = get_guidance_prompt(question, subfields)
return answer, guidance
# Submit button with enhanced styling
if st.button("Submit"):
if research_question and selected_subfields:
with st.spinner(f"Researching your question in {', '.join(selected_subfields)}..."):
answer, guidance = get_researcher_response(research_question, selected_subfields)
if guidance:
# Use Tabs with icons for better organization
tab1, tab2 = st.tabs([f"π€ AI Response", f"π Suggested Papers"])
with tab1:
st.write("### Researcher's Answer:")
st.write(answer)
st.write("### Researcher's Guidance:")
st.write(guidance)
with tab2:
st.write("### Suggested Research Papers:")
scholar_papers = get_research_papers_from_scholar(research_question, selected_subfields)
if scholar_papers:
for i, paper in enumerate(scholar_papers):
with st.expander(f"π Paper {i+1}: {paper['title']} ({paper['field']})"):
st.write(f"**Abstract**: {paper['abstract']}")
st.write(f"[Read Full Paper]({paper['url']})")
else:
st.info("No papers found, try a different query.")
else:
st.warning(answer)
else:
st.warning("Please enter a research question and select at least one sub-field before submitting.")
|