Update app.py
Browse files
app.py
CHANGED
@@ -144,33 +144,35 @@ def trimming_chat_answer(text):
|
|
144 |
def process_urls(urls):
|
145 |
results = []
|
146 |
for url in urls:
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
raw_markdown
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
clean_markdown =
|
172 |
-
|
173 |
-
|
|
|
|
|
174 |
|
175 |
return results
|
176 |
|
|
|
144 |
def process_urls(urls):
|
145 |
results = []
|
146 |
for url in urls:
|
147 |
+
url = url.strip() # Remove leading and trailing whitespace
|
148 |
+
if url: # Proceed only if the URL is not empty
|
149 |
+
# Try to fetch metadata directly from the URL
|
150 |
+
raw_markdown = fetch_metadata(url)
|
151 |
+
|
152 |
+
# If raw_markdown is a fallback message, try DuckDuckGo search
|
153 |
+
if "Failed to fetch data" in raw_markdown or "No search results" in raw_markdown:
|
154 |
+
raw_markdown = duckduckgo_search_fallback(url)
|
155 |
+
|
156 |
+
# Use the DuckDuckGo chat to format the output, but fallback to raw_markdown if unsuccessful
|
157 |
+
try:
|
158 |
+
ddgschat = DDGS().chat(f"""Please rewrite the following markdown string so the news headline is capitalized as a sentence, only proper nouns or names should have capital initials. Also correct capitalization of the source name if necessary.
|
159 |
+
Then, please check if the source name (surrounded by round parenthesis) is repeated inside the headline (surrounded by square brackets); if it is repeated, please remove the source name mention from the headline keeping the URL and the source name in parentheses outside the headline.
|
160 |
+
Please answer only with one line of the markdown output.
|
161 |
+
Example input = [Montana Is a Frontier for Deep Carbon Storage, Mr. Ant贸nio Guterres from the United Nations Claims for Urgent Action - Inside Climate News](https://insideclimatenews.org/news/18072024/montana-deep-carbon-storage-controversies/) (Inside Climate News)
|
162 |
+
Example output = [Montana is a frontier for deep carbon storage, Mr. Ant贸nio Guterres from the United Nations claims for urgent action](https://insideclimatenews.org/news/18072024/montana-deep-carbon-storage-controversies/) (Inside Climate News)
|
163 |
+
|
164 |
+
Input:
|
165 |
+
{raw_markdown}
|
166 |
+
""", model='claude-3-haiku')
|
167 |
+
except Exception as e:
|
168 |
+
ddgschat = raw_markdown # If there's an error with the chat API, use raw_markdown
|
169 |
+
|
170 |
+
# Trim the result using the chat output or fallback to raw_markdown
|
171 |
+
clean_markdown = trimming_chat_answer(ddgschat)
|
172 |
+
if clean_markdown == "No match found":
|
173 |
+
clean_markdown = raw_markdown
|
174 |
+
|
175 |
+
results.append(clean_markdown)
|
176 |
|
177 |
return results
|
178 |
|