pinge commited on
Commit
fbe0e88
·
verified ·
1 Parent(s): 61bc2d4

Upload 3 files

Browse files
Files changed (3) hide show
  1. .env +1 -0
  2. app.py +100 -0
  3. requirements.txt +6 -3
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENROUTER_API_KEY=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
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import json
4
+ import os
5
+ from dotenv import load_dotenv
6
+ from langchain_community.document_loaders import WebBaseLoader
7
+
8
+ # Load environment variables from .env file
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 # Extract text content
22
+ except Exception as e:
23
+ st.error(f"Error loading URL: {e}")
24
+ return None
25
+
26
+ # Function to summarize text using Gemma 3 27B via OpenRouter API
27
+ def summarize_text(url):
28
+ """Summarize the content from the given URL using Gemma 3 27B via OpenRouter API"""
29
+ text = load_text(url)
30
+ if not text:
31
+ return None
32
+
33
+ # Define the prompt for summarization
34
+ summary_prompt = f"""
35
+ 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.
36
+
37
+ TEXT: {text}
38
+
39
+ SUMMARY:
40
+ """
41
+
42
+ try:
43
+ # Make API request to OpenRouter for summarization
44
+ response = requests.post(
45
+ url="https://openrouter.ai/api/v1/chat/completions",
46
+ headers={
47
+ "Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}",
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": "google/gemma-3-27b-it:free",
54
+ "messages": [
55
+ {
56
+ "role": "user",
57
+ "content": [
58
+ {
59
+ "type": "text",
60
+ "text": summary_prompt
61
+ }
62
+ ]
63
+ }
64
+ ]
65
+ })
66
+ )
67
+
68
+ # Check if the request was successful
69
+ if response.status_code == 200:
70
+ result = response.json()
71
+ summary = result['choices'][0]['message']['content']
72
+ return summary.strip()
73
+ else:
74
+ st.error(f"API Error: {response.status_code} - {response.text}")
75
+ return None
76
+
77
+ except Exception as e:
78
+ st.error(f"Error summarizing content: {e}")
79
+ return None
80
+
81
+ # Streamlit app interface
82
+ st.title("Summarizer AI")
83
+ st.markdown("Enter a URL to summarize the content concisely")
84
+
85
+ with st.form(key='summarizer_form'):
86
+ url = st.text_area(
87
+ label="Enter the URL of the article or blog post:",
88
+ max_chars=250,
89
+ placeholder="https://example.com/article"
90
+ )
91
+ submit_button = st.form_submit_button(label="Summarize")
92
+
93
+ if submit_button and url:
94
+ with st.spinner("Summarizing..."):
95
+ summary = summarize_text(url)
96
+ if summary:
97
+ st.subheader("Summary")
98
+ st.write(summary)
99
+ else:
100
+ st.error("Unable to generate summary. Please check the URL or try again.")
requirements.txt CHANGED
@@ -1,3 +1,6 @@
1
- altair
2
- pandas
3
- streamlit
 
 
 
 
1
+ openai
2
+ streamlit
3
+ requests
4
+ python-dotenv
5
+ langchain-community
6
+ beautifulsoup4