Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| import json | |
| import os | |
| from dotenv import load_dotenv | |
| from langchain_community.document_loaders import WebBaseLoader | |
| # Load environment variables | |
| load_dotenv() | |
| # Get API key from .env | |
| api_key = os.getenv('GROQ_API_KEY') | |
| if not api_key: | |
| st.error("GROQ_API_KEY not found in .env file. Please add it to the .env file in your Hugging Face Space or local environment.") | |
| st.stop() | |
| # Function to load the article/blog post from a URL | |
| def load_text(url): | |
| """Load the article/blog post from a URL""" | |
| try: | |
| loader = WebBaseLoader(url) | |
| loader.requests_kwargs = { | |
| 'headers': {'User-Agent': 'SummarizerBot/1.0 (https://your-site.com)'} | |
| } | |
| docs = loader.load() | |
| return docs[0].page_content if docs else None | |
| except Exception as e: | |
| st.error(f"Error loading URL: {e}") | |
| return None | |
| # Function to summarize text using Llama 3 70B via Groq API | |
| def summarize_text(url): | |
| """Summarize the content from the given URL using Llama 3 70B via Groq API""" | |
| text = load_text(url) | |
| if not text: | |
| return None | |
| # Define the prompt for summarization | |
| summary_prompt = f""" | |
| You are an expert summarizer. Your task is to create a concise summary of the following text. The summary should be no more than 7-8 sentences long. | |
| TEXT: {text} | |
| SUMMARY: | |
| """ | |
| try: | |
| # Make API request to Groq for summarization | |
| response = requests.post( | |
| url="https://api.groq.com/openai/v1/chat/completions", | |
| headers={ | |
| "Authorization": f"Bearer {api_key}", | |
| "Content-Type": "application/json" | |
| }, | |
| data=json.dumps({ | |
| "model": "llama3-70b-8192", # Working Llama model on Groq | |
| "messages": [ | |
| { | |
| "role": "user", | |
| "content": summary_prompt | |
| } | |
| ], | |
| "max_tokens": 500, # Limit output for concise summaries | |
| "temperature": 0.7 # Balanced creativity for summarization | |
| }) | |
| ) | |
| # Check if the request was successful | |
| if response.status_code == 200: | |
| result = response.json() | |
| summary = result['choices'][0]['message']['content'] | |
| return summary.strip() | |
| else: | |
| st.error(f"API Error: {response.status_code} - {response.text}") | |
| return None | |
| except Exception as e: | |
| st.error(f"Error summarizing content: {e}") | |
| return None | |
| # Streamlit app interface | |
| st.title("Summarizer AI") | |
| st.markdown("Enter a URL to summarize the content concisely") | |
| with st.form(key='summarizer_form'): | |
| url = st.text_area( | |
| label="Enter the URL of the article or blog post:", | |
| max_chars=250, | |
| placeholder="https://example.com/article" | |
| ) | |
| submit_button = st.form_submit_button(label="Summarize") | |
| if submit_button and url: | |
| with st.spinner("Summarizing..."): | |
| summary = summarize_text(url) | |
| if summary: | |
| st.subheader("Summary") | |
| st.write(summary) | |
| else: | |
| st.error("Unable to generate summary. Please check the URL or try again.") |