nelsonjq commited on
Commit
c270c31
verified
1 Parent(s): 4827ea7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -27
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
- # Try to fetch metadata directly from the URL
148
- raw_markdown = fetch_metadata(url)
149
-
150
- # If raw_markdown is a fallback message, try DuckDuckGo search
151
- if "Failed to fetch data" in raw_markdown or "No search results" in raw_markdown:
152
- raw_markdown = duckduckgo_search_fallback(url)
153
-
154
- # Use the DuckDuckGo chat to format the output, but fallback to raw_markdown if unsuccessful
155
- try:
156
- 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.
157
- 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.
158
- Please answer only with one line of the markdown output.
159
- 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)
160
- 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)
161
-
162
- Input:
163
- {raw_markdown}
164
- """, model='claude-3-haiku')
165
- except Exception as e:
166
- ddgschat = raw_markdown # If there's an error with the chat API, use raw_markdown
167
-
168
- # Trim the result using the chat output or fallback to raw_markdown
169
- clean_markdown = trimming_chat_answer(ddgschat)
170
- if clean_markdown == "No match found":
171
- clean_markdown = raw_markdown
172
-
173
- results.append(clean_markdown)
 
 
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