Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,6 @@ import json
|
|
5 |
import os
|
6 |
from typing import Dict, List
|
7 |
|
8 |
-
# Get API keys from environment variables
|
9 |
OPENAI_API_KEY = os.getenv('openaikey')
|
10 |
RAINDROP_TOKEN = os.getenv('raindroptoken')
|
11 |
|
@@ -21,14 +20,14 @@ class RaindropSearchBot:
|
|
21 |
self.client = OpenAI(api_key=self.openai_api_key)
|
22 |
|
23 |
def generate_search_query(self, user_request: str) -> str:
|
24 |
-
"""Convert user request to a tailored search query using OpenAI."""
|
25 |
prompt = f"""
|
26 |
Convert the following request into a focused search query for Raindrop.io.
|
27 |
-
|
|
|
28 |
|
29 |
User Request: {user_request}
|
30 |
|
31 |
-
Format the search query
|
32 |
"""
|
33 |
|
34 |
response = self.client.chat.completions.create(
|
@@ -38,20 +37,27 @@ class RaindropSearchBot:
|
|
38 |
max_tokens=1000
|
39 |
)
|
40 |
return response.choices[0].message.content
|
41 |
-
|
42 |
def search_raindrop(self, search_query: str) -> List[Dict]:
|
43 |
-
"""Search Raindrop.io with the generated query."""
|
44 |
headers = {
|
45 |
"Authorization": f"Bearer {self.raindrop_api_token}",
|
46 |
"Content-Type": "application/json"
|
47 |
}
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
params = {
|
50 |
"search": search_query,
|
51 |
"page": 0,
|
52 |
-
"perpage": 10
|
|
|
53 |
}
|
54 |
|
|
|
55 |
response = requests.get(
|
56 |
"https://api.raindrop.io/rest/v1/raindrops/0",
|
57 |
headers=headers,
|
@@ -59,12 +65,43 @@ class RaindropSearchBot:
|
|
59 |
)
|
60 |
|
61 |
if response.status_code == 200:
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
def format_results(self, results: List[Dict]) -> str:
|
67 |
-
"""Format the search results into a readable string."""
|
68 |
if not results:
|
69 |
return "No results found."
|
70 |
|
@@ -72,24 +109,27 @@ class RaindropSearchBot:
|
|
72 |
for idx, item in enumerate(results, 1):
|
73 |
formatted_output += f"{idx}. {item.get('title', 'No Title')}\n"
|
74 |
formatted_output += f" Link: {item.get('link', 'No Link')}\n"
|
|
|
|
|
75 |
if item.get('tags'):
|
76 |
formatted_output += f" Tags: {', '.join(item['tags'])}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
formatted_output += "\n"
|
78 |
|
79 |
return formatted_output
|
80 |
|
81 |
def process_request(self, user_request: str) -> str:
|
82 |
-
"""Process the user request and return formatted results."""
|
83 |
try:
|
84 |
-
# Generate optimized search query
|
85 |
search_query = self.generate_search_query(user_request)
|
86 |
-
|
87 |
-
# Search Raindrop.io
|
88 |
results = self.search_raindrop(search_query)
|
89 |
-
|
90 |
-
# Format and return results
|
91 |
return self.format_results(results)
|
92 |
-
|
93 |
except Exception as e:
|
94 |
return f"An error occurred: {str(e)}"
|
95 |
|
@@ -104,7 +144,7 @@ def chatbot_interface(user_input: str) -> str:
|
|
104 |
with gr.Blocks(title="Raindrop.io Link Search Assistant", theme=gr.themes.Soft()) as demo:
|
105 |
gr.Markdown("""
|
106 |
# π Raindrop.io Link Search Assistant
|
107 |
-
Enter your search request in natural language, and I'll find relevant bookmarked links from your Raindrop.io collection.
|
108 |
""")
|
109 |
|
110 |
with gr.Row():
|
@@ -120,7 +160,7 @@ with gr.Blocks(title="Raindrop.io Link Search Assistant", theme=gr.themes.Soft()
|
|
120 |
with gr.Row():
|
121 |
output_text = gr.Textbox(
|
122 |
label="Search Results",
|
123 |
-
lines=
|
124 |
interactive=False
|
125 |
)
|
126 |
|
@@ -134,11 +174,14 @@ with gr.Blocks(title="Raindrop.io Link Search Assistant", theme=gr.themes.Soft()
|
|
134 |
### How to use:
|
135 |
1. Enter your search request in natural language
|
136 |
2. Click the Search button
|
137 |
-
3. View the results from your Raindrop.io bookmarks
|
138 |
|
139 |
-
The assistant will
|
|
|
|
|
|
|
|
|
140 |
""")
|
141 |
|
142 |
-
# Launch the interface
|
143 |
if __name__ == "__main__":
|
144 |
-
demo.launch(share=True)
|
|
|
5 |
import os
|
6 |
from typing import Dict, List
|
7 |
|
|
|
8 |
OPENAI_API_KEY = os.getenv('openaikey')
|
9 |
RAINDROP_TOKEN = os.getenv('raindroptoken')
|
10 |
|
|
|
20 |
self.client = OpenAI(api_key=self.openai_api_key)
|
21 |
|
22 |
def generate_search_query(self, user_request: str) -> str:
|
|
|
23 |
prompt = f"""
|
24 |
Convert the following request into a focused search query for Raindrop.io.
|
25 |
+
Extract key concepts and create a search query that will work well with the Raindrop API.
|
26 |
+
Include relevant operators and consider both exact matches and related terms.
|
27 |
|
28 |
User Request: {user_request}
|
29 |
|
30 |
+
Format the search query for maximum relevance and recall.
|
31 |
"""
|
32 |
|
33 |
response = self.client.chat.completions.create(
|
|
|
37 |
max_tokens=1000
|
38 |
)
|
39 |
return response.choices[0].message.content
|
40 |
+
|
41 |
def search_raindrop(self, search_query: str) -> List[Dict]:
|
|
|
42 |
headers = {
|
43 |
"Authorization": f"Bearer {self.raindrop_api_token}",
|
44 |
"Content-Type": "application/json"
|
45 |
}
|
46 |
|
47 |
+
# Get all collections first
|
48 |
+
collections_response = requests.get(
|
49 |
+
"https://api.raindrop.io/rest/v1/collections",
|
50 |
+
headers=headers
|
51 |
+
)
|
52 |
+
|
53 |
params = {
|
54 |
"search": search_query,
|
55 |
"page": 0,
|
56 |
+
"perpage": 50, # Increased from 10 to 50
|
57 |
+
"sort": "-created" # Sort by newest first
|
58 |
}
|
59 |
|
60 |
+
# Search in all collections (0)
|
61 |
response = requests.get(
|
62 |
"https://api.raindrop.io/rest/v1/raindrops/0",
|
63 |
headers=headers,
|
|
|
65 |
)
|
66 |
|
67 |
if response.status_code == 200:
|
68 |
+
items = response.json().get("items", [])
|
69 |
+
# Get filters to enhance search context
|
70 |
+
filters_response = requests.get(
|
71 |
+
"https://api.raindrop.io/rest/v1/filters/0",
|
72 |
+
headers=headers,
|
73 |
+
params={"search": search_query}
|
74 |
+
)
|
75 |
+
return items
|
76 |
+
return []
|
77 |
+
|
78 |
+
def generate_summary(self, item: Dict) -> str:
|
79 |
+
"""Generate a summary for a single bookmark using GPT-4."""
|
80 |
+
content = f"""
|
81 |
+
Title: {item.get('title', 'No Title')}
|
82 |
+
Description: {item.get('excerpt', '')}
|
83 |
+
Tags: {', '.join(item.get('tags', []))}
|
84 |
+
Type: {item.get('type', 'unknown')}
|
85 |
+
"""
|
86 |
+
|
87 |
+
prompt = f"""
|
88 |
+
Please provide a brief, informative summary of this bookmarked content:
|
89 |
+
{content}
|
90 |
+
Keep the summary concise but include key points and relevance.
|
91 |
+
"""
|
92 |
+
|
93 |
+
try:
|
94 |
+
response = self.client.chat.completions.create(
|
95 |
+
model="gpt-4o-mini",
|
96 |
+
messages=[{"role": "user", "content": prompt}],
|
97 |
+
temperature=0.3,
|
98 |
+
max_tokens=150
|
99 |
+
)
|
100 |
+
return response.choices[0].message.content
|
101 |
+
except Exception as e:
|
102 |
+
return "Summary generation failed"
|
103 |
+
|
104 |
def format_results(self, results: List[Dict]) -> str:
|
|
|
105 |
if not results:
|
106 |
return "No results found."
|
107 |
|
|
|
109 |
for idx, item in enumerate(results, 1):
|
110 |
formatted_output += f"{idx}. {item.get('title', 'No Title')}\n"
|
111 |
formatted_output += f" Link: {item.get('link', 'No Link')}\n"
|
112 |
+
|
113 |
+
# Add tags if present
|
114 |
if item.get('tags'):
|
115 |
formatted_output += f" Tags: {', '.join(item['tags'])}\n"
|
116 |
+
|
117 |
+
# Add type and created date
|
118 |
+
formatted_output += f" Type: {item.get('type', 'unknown')} | Created: {item.get('created', 'unknown')}\n"
|
119 |
+
|
120 |
+
# Add summary
|
121 |
+
summary = self.generate_summary(item)
|
122 |
+
formatted_output += f" Summary: {summary}\n"
|
123 |
+
|
124 |
formatted_output += "\n"
|
125 |
|
126 |
return formatted_output
|
127 |
|
128 |
def process_request(self, user_request: str) -> str:
|
|
|
129 |
try:
|
|
|
130 |
search_query = self.generate_search_query(user_request)
|
|
|
|
|
131 |
results = self.search_raindrop(search_query)
|
|
|
|
|
132 |
return self.format_results(results)
|
|
|
133 |
except Exception as e:
|
134 |
return f"An error occurred: {str(e)}"
|
135 |
|
|
|
144 |
with gr.Blocks(title="Raindrop.io Link Search Assistant", theme=gr.themes.Soft()) as demo:
|
145 |
gr.Markdown("""
|
146 |
# π Raindrop.io Link Search Assistant
|
147 |
+
Enter your search request in natural language, and I'll find and summarize relevant bookmarked links from your Raindrop.io collection.
|
148 |
""")
|
149 |
|
150 |
with gr.Row():
|
|
|
160 |
with gr.Row():
|
161 |
output_text = gr.Textbox(
|
162 |
label="Search Results",
|
163 |
+
lines=15,
|
164 |
interactive=False
|
165 |
)
|
166 |
|
|
|
174 |
### How to use:
|
175 |
1. Enter your search request in natural language
|
176 |
2. Click the Search button
|
177 |
+
3. View the results and summaries from your Raindrop.io bookmarks
|
178 |
|
179 |
+
The assistant will:
|
180 |
+
- Convert your request into an optimized search query
|
181 |
+
- Search across all your collections
|
182 |
+
- Generate summaries for each result
|
183 |
+
- Present the findings with relevant metadata
|
184 |
""")
|
185 |
|
|
|
186 |
if __name__ == "__main__":
|
187 |
+
demo.launch(share=True)
|