Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| from transformers import pipeline | |
| import requests | |
| from bs4 import BeautifulSoup | |
| # Initialize a text generation pipeline | |
| generator = pipeline('text-generation', model='dbmdz/german-gpt2') | |
| # Define a function to fetch trending news related to a specific niche | |
| import streamlit as st | |
| from transformers import pipeline | |
| import requests | |
| from bs4 import BeautifulSoup | |
| # Initialize a text generation pipeline | |
| generator = pipeline('text-generation', model='dbmdz/german-gpt2') | |
| def fetch_trending_news(niche): | |
| url = f"https://www.google.com/search?q={niche}+news&tbs=qdr:d" | |
| headers = { | |
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"} | |
| try: | |
| response = requests.get(url, headers=headers) | |
| if response.status_code == 200: | |
| soup = BeautifulSoup(response.content, "html.parser") | |
| # Adjusted to use more generic selectors that might be more stable | |
| news_items = soup.find_all("div", class_="ZINbbc xpd O9g5cc uUPGi") | |
| if not news_items: | |
| print("No news items found, check your selectors.") | |
| return [] | |
| trending_news = [item.find("div", class_="BNeawe vvjwJb AP7Wnd").text for item in news_items[:5]] | |
| return trending_news | |
| else: | |
| print(f"Failed to fetch news, status code: {response.status_code}") | |
| return [] | |
| except Exception as e: | |
| print(f"Error fetching news: {e}") | |
| return [] | |
| # Define the pages | |
| def page_trending_niche(): | |
| # Using st.columns to create a two-column layout | |
| col1, col2 = st.columns([3, 1]) | |
| with col1: | |
| st.title("What is trending in my niche?") | |
| with col2: | |
| st.image('Robot.png', use_column_width=True) | |
| niche = st.text_input('Enter your niche', 'German clinics') | |
| if st.button('Fetch Trending News'): | |
| st.write(f"Trending news in {niche}:") | |
| trending_news = fetch_trending_news(niche) | |
| print("Trending news:", trending_news) # Debug print | |
| for idx, news_item in enumerate(trending_news, start=1): | |
| st.write(f"{idx}. {news_item}") | |
| # Define the pages | |
| def page_social_media_generator(): | |
| # Using st.columns to create a two-column layout | |
| col1, col2 = st.columns([3, 1]) | |
| with col1: | |
| st.title("German Medical Content Manager") | |
| with col2: | |
| st.image('Content_Creation_Pic.png', use_column_width=True) | |
| input_topic = st.text_input('Enter a medical topic', 'Type 1 Diabetes') | |
| st.write(f"Creating social media content for: {input_topic}") | |
| def generate_content(topic): | |
| generated_text = generator(f"Letzte Nachrichten über {topic}:", max_length=50, num_return_sequences=1) | |
| return generated_text[0]['generated_text'] | |
| if st.button('Generate Social Media Post'): | |
| with st.spinner('Generating...'): | |
| post_content = generate_content(input_topic) | |
| st.success('Generated Content:') | |
| st.write(post_content) | |
| st.write('Generated social media posts will appear here after clicking the "Generate" button.') | |
| def page_test(): | |
| st.title('Test Page') | |
| st.write('This is a test page with a test name.') | |
| # Setup the sidebar with page selection | |
| st.sidebar.title("Anne's Current Projects :star2:") | |
| page = st.sidebar.selectbox( | |
| 'What project do you like to see first?', | |
| ('trending_niche', 'Social Media Content Generator', 'Test Page')) | |
| # Display the selected page | |
| if page == 'trending_niche': | |
| page_trending_niche() | |
| elif page == 'Social Media Content Generator': | |
| page_social_media_generator() | |
| elif page == 'Test Page': | |
| page_test() | |