Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +17 -19
src/streamlit_app.py
CHANGED
|
@@ -5,27 +5,30 @@ import os
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from langchain_community.document_loaders import WebBaseLoader
|
| 7 |
|
| 8 |
-
# Load environment variables
|
| 9 |
load_dotenv()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Function to load the article/blog post from a URL
|
| 12 |
def load_text(url):
|
| 13 |
"""Load the article/blog post from a URL"""
|
| 14 |
try:
|
| 15 |
loader = WebBaseLoader(url)
|
| 16 |
loader.requests_kwargs = {
|
| 17 |
-
'verify': False,
|
| 18 |
'headers': {'User-Agent': os.getenv('USER_AGENT', 'SummarizerBot/1.0 (https://your-site.com)')}
|
| 19 |
}
|
| 20 |
docs = loader.load()
|
| 21 |
-
return docs[0].page_content if docs else None
|
| 22 |
except Exception as e:
|
| 23 |
st.error(f"Error loading URL: {e}")
|
| 24 |
return None
|
| 25 |
|
| 26 |
-
# Function to summarize text using
|
| 27 |
def summarize_text(url):
|
| 28 |
-
"""Summarize the content from the given URL using
|
| 29 |
text = load_text(url)
|
| 30 |
if not text:
|
| 31 |
return None
|
|
@@ -40,28 +43,23 @@ SUMMARY:
|
|
| 40 |
"""
|
| 41 |
|
| 42 |
try:
|
| 43 |
-
# Make API request to
|
| 44 |
response = requests.post(
|
| 45 |
-
url="https://
|
| 46 |
headers={
|
| 47 |
-
"Authorization": f"Bearer {os.getenv('
|
| 48 |
-
"Content-Type": "application/json"
|
| 49 |
-
"HTTP-Referer": os.getenv('SITE_URL', '<YOUR_SITE_URL>'), # Optional, defaults to placeholder
|
| 50 |
-
"X-Title": os.getenv('SITE_NAME', '<YOUR_SITE_NAME>'), # Optional, defaults to placeholder
|
| 51 |
},
|
| 52 |
data=json.dumps({
|
| 53 |
-
"model": "
|
| 54 |
"messages": [
|
| 55 |
{
|
| 56 |
"role": "user",
|
| 57 |
-
"content":
|
| 58 |
-
{
|
| 59 |
-
"type": "text",
|
| 60 |
-
"text": summary_prompt
|
| 61 |
-
}
|
| 62 |
-
]
|
| 63 |
}
|
| 64 |
-
]
|
|
|
|
|
|
|
| 65 |
})
|
| 66 |
)
|
| 67 |
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from langchain_community.document_loaders import WebBaseLoader
|
| 7 |
|
| 8 |
+
# Load environment variables
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
+
# Debug: Check if API key is loaded
|
| 12 |
+
if not os.getenv('GROQ_API_KEY'):
|
| 13 |
+
st.error("GROQ_API_KEY not found in environment variables. Please check your .env file.")
|
| 14 |
+
|
| 15 |
# Function to load the article/blog post from a URL
|
| 16 |
def load_text(url):
|
| 17 |
"""Load the article/blog post from a URL"""
|
| 18 |
try:
|
| 19 |
loader = WebBaseLoader(url)
|
| 20 |
loader.requests_kwargs = {
|
|
|
|
| 21 |
'headers': {'User-Agent': os.getenv('USER_AGENT', 'SummarizerBot/1.0 (https://your-site.com)')}
|
| 22 |
}
|
| 23 |
docs = loader.load()
|
| 24 |
+
return docs[0].page_content if docs else None
|
| 25 |
except Exception as e:
|
| 26 |
st.error(f"Error loading URL: {e}")
|
| 27 |
return None
|
| 28 |
|
| 29 |
+
# Function to summarize text using Llama 2 70B via Groq API
|
| 30 |
def summarize_text(url):
|
| 31 |
+
"""Summarize the content from the given URL using Llama 2 70B via Groq API"""
|
| 32 |
text = load_text(url)
|
| 33 |
if not text:
|
| 34 |
return None
|
|
|
|
| 43 |
"""
|
| 44 |
|
| 45 |
try:
|
| 46 |
+
# Make API request to Groq for summarization
|
| 47 |
response = requests.post(
|
| 48 |
+
url="https://api.groq.com/openai/v1/chat/completions",
|
| 49 |
headers={
|
| 50 |
+
"Authorization": f"Bearer {os.getenv('GROQ_API_KEY')}",
|
| 51 |
+
"Content-Type": "application/json"
|
|
|
|
|
|
|
| 52 |
},
|
| 53 |
data=json.dumps({
|
| 54 |
+
"model": "llama2-70b-4096",
|
| 55 |
"messages": [
|
| 56 |
{
|
| 57 |
"role": "user",
|
| 58 |
+
"content": summary_prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
}
|
| 60 |
+
],
|
| 61 |
+
"max_tokens": 500, # Limit output for concise summaries
|
| 62 |
+
"temperature": 0.7 # Balanced creativity for summarization
|
| 63 |
})
|
| 64 |
)
|
| 65 |
|