CoolFace
Apppublic

janavbhasin/internship_study_planner

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
app.py127 linesDownload Raw Back to root
1import requests2from bs4 import BeautifulSoup3import math4import streamlit as st5 6# Request API key from the user7api_key = st.text_input("Enter your Google API key:")8cse_id = '563a3fc5acac44233'  # Still using the predefined CSE ID9 10def google_search(search_term, api_key, cse_id, **kwargs):11    url = f"https://www.googleapis.com/customsearch/v1"12    params = {13        'q': search_term,14        'key': api_key,15        'cx': cse_id16    }17    params.update(kwargs)18    response = requests.get(url, params=params)19    20    if response.status_code == 200:21        results = response.json()22        return results.get('items', [])23    else:24        st.error("Error with Google API request")25        return []26 27def scrape_headings(url):28    try:29        response = requests.get(url)30        soup = BeautifulSoup(response.content, 'html.parser')31        headings = [heading.get_text(strip=True) for level in range(1, 4)32                    for heading in soup.find_all(f'h{level}')]33        return headings34    except requests.exceptions.RequestException as e:35        st.error(f"Error scraping {url}: {e}")36        return []37 38def find_subtopics_and_resources(topic):39    search_results = google_search(topic, api_key=api_key, cse_id=cse_id, num=10)40    subtopics = []41 42    for result in search_results:43        title = result.get('title')44        link = result.get('link')45        snippet = result.get('snippet')46        headings = scrape_headings(link)47 48        material = {49            'title': title,50            'link': link,51            'snippet': snippet,52            'headings': headings53        }54        subtopics.append(material)55 56    return subtopics57 58def find_resources(topic):59    resource_types = ["PDF", "article", "book"]60    resources = []61 62    for resource_type in resource_types:63        search_results = google_search(f"{topic} {resource_type}", api_key=api_key, cse_id=cse_id, num=5)64        for result in search_results:65            title = result.get('title')66            link = result.get('link')67            snippet = result.get('snippet')68            resource = {69                'title': title,70                'link': link,71                'snippet': snippet,72                'type': resource_type73            }74            resources.append(resource)75 76    return resources77 78def divide_subtopics_into_weeks(subtopics, num_weeks):79    total_items = len(subtopics)80    items_per_week = max(1, math.ceil(total_items / num_weeks))81 82    weeks = [subtopics[i:i + items_per_week] for i in range(0, total_items, items_per_week)]83    return weeks84 85def display_subtopics_and_resources(subtopics, resources, num_weeks):86    weeks = divide_subtopics_into_weeks(subtopics, num_weeks)87 88    for week_num, week in enumerate(weeks, 1):89        with st.expander(f"Week {week_num}"):90            st.write(f"**Week {week_num}**")91 92            for item in week:93                st.write(f"**Subtopic Title:** {item['title']}")94                st.write(f"**Snippet:** {item['snippet']}")95                st.write("**Headings:**")96                for heading in item['headings']:97                    st.write(f"  - {heading}")98 99                st.write("=" * 50)100 101    with st.expander("Resources"):102        st.write("**Resources:**")103        for resource in resources:104            st.write(f"**Type:** {resource['type']}")105            st.write(f"**Title:** {resource['title']}")106            st.write(f"**Link:** [Click here]({resource['link']})")107            st.write(f"**Snippet:** {resource['snippet']}")108            st.write("=" * 50)109 110def main():111    st.title("AI-Powered Subtopic and Resource Finder")112 113    topic = st.text_input("Enter the topic you want to find subtopics and resources for:")114    num_weeks = st.number_input("Enter the number of weeks to divide the subtopics into:", min_value=1, step=1)115 116    if api_key and st.button("Find Subtopics and Resources"):117        with st.spinner("Searching..."):118            subtopics = find_subtopics_and_resources(topic)119            resources = find_resources(topic)120            display_subtopics_and_resources(subtopics, resources, num_weeks)121            st.success("Subtopics and resources displayed successfully.")122    elif not api_key:123        st.warning("Please enter your Google API key.")124 125if __name__ == "__main__":126    main()127