Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import requests
|
4 |
+
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 |
+
|
12 |
+
if not OPENAI_API_KEY or not RAINDROP_TOKEN:
|
13 |
+
raise EnvironmentError(
|
14 |
+
"Missing required environment variables. Please ensure 'openaikey' and 'raindroptoken' are set."
|
15 |
+
)
|
16 |
+
|
17 |
+
class RaindropSearchBot:
|
18 |
+
def __init__(self):
|
19 |
+
self.openai_api_key = OPENAI_API_KEY
|
20 |
+
self.raindrop_api_token = RAINDROP_TOKEN
|
21 |
+
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 |
+
The query should be specific and relevant for searching bookmarked links.
|
28 |
+
|
29 |
+
User Request: {user_request}
|
30 |
+
|
31 |
+
Format the search query with relevant keywords and operators if needed.
|
32 |
+
"""
|
33 |
+
|
34 |
+
response = openai.ChatCompletion.create(
|
35 |
+
model="gpt-3.5-turbo",
|
36 |
+
messages=[
|
37 |
+
{"role": "system", "content": "You are a search query optimization assistant."},
|
38 |
+
{"role": "user", "content": prompt}
|
39 |
+
]
|
40 |
+
)
|
41 |
+
|
42 |
+
return response.choices[0].message.content.strip()
|
43 |
+
|
44 |
+
def search_raindrop(self, search_query: str) -> List[Dict]:
|
45 |
+
"""Search Raindrop.io with the generated query."""
|
46 |
+
headers = {
|
47 |
+
"Authorization": f"Bearer {self.raindrop_api_token}",
|
48 |
+
"Content-Type": "application/json"
|
49 |
+
}
|
50 |
+
|
51 |
+
params = {
|
52 |
+
"search": search_query,
|
53 |
+
"page": 0,
|
54 |
+
"perpage": 10
|
55 |
+
}
|
56 |
+
|
57 |
+
response = requests.get(
|
58 |
+
"https://api.raindrop.io/rest/v1/raindrops/0",
|
59 |
+
headers=headers,
|
60 |
+
params=params
|
61 |
+
)
|
62 |
+
|
63 |
+
if response.status_code == 200:
|
64 |
+
return response.json().get("items", [])
|
65 |
+
else:
|
66 |
+
return []
|
67 |
+
|
68 |
+
def format_results(self, results: List[Dict]) -> str:
|
69 |
+
"""Format the search results into a readable string."""
|
70 |
+
if not results:
|
71 |
+
return "No results found."
|
72 |
+
|
73 |
+
formatted_output = "π Search Results:\n\n"
|
74 |
+
for idx, item in enumerate(results, 1):
|
75 |
+
formatted_output += f"{idx}. {item.get('title', 'No Title')}\n"
|
76 |
+
formatted_output += f" Link: {item.get('link', 'No Link')}\n"
|
77 |
+
if item.get('tags'):
|
78 |
+
formatted_output += f" Tags: {', '.join(item['tags'])}\n"
|
79 |
+
formatted_output += "\n"
|
80 |
+
|
81 |
+
return formatted_output
|
82 |
+
|
83 |
+
def process_request(self, user_request: str) -> str:
|
84 |
+
"""Process the user request and return formatted results."""
|
85 |
+
try:
|
86 |
+
# Generate optimized search query
|
87 |
+
search_query = self.generate_search_query(user_request)
|
88 |
+
|
89 |
+
# Search Raindrop.io
|
90 |
+
results = self.search_raindrop(search_query)
|
91 |
+
|
92 |
+
# Format and return results
|
93 |
+
return self.format_results(results)
|
94 |
+
|
95 |
+
except Exception as e:
|
96 |
+
return f"An error occurred: {str(e)}"
|
97 |
+
|
98 |
+
# Initialize the bot
|
99 |
+
bot = RaindropSearchBot()
|
100 |
+
|
101 |
+
# Create Gradio interface
|
102 |
+
def chatbot_interface(user_input: str) -> str:
|
103 |
+
return bot.process_request(user_input)
|
104 |
+
|
105 |
+
# Define and launch the Gradio interface
|
106 |
+
with gr.Blocks(title="Raindrop.io Link Search Assistant", theme=gr.themes.Soft()) as demo:
|
107 |
+
gr.Markdown("""
|
108 |
+
# π Raindrop.io Link Search Assistant
|
109 |
+
Enter your search request in natural language, and I'll find relevant bookmarked links from your Raindrop.io collection.
|
110 |
+
""")
|
111 |
+
|
112 |
+
with gr.Row():
|
113 |
+
input_text = gr.Textbox(
|
114 |
+
label="What would you like to search for?",
|
115 |
+
placeholder="Ex: Give me the list of all the links related to blockchain in Hong Kong",
|
116 |
+
lines=2
|
117 |
+
)
|
118 |
+
|
119 |
+
with gr.Row():
|
120 |
+
search_button = gr.Button("π Search", variant="primary")
|
121 |
+
|
122 |
+
with gr.Row():
|
123 |
+
output_text = gr.Textbox(
|
124 |
+
label="Search Results",
|
125 |
+
lines=10,
|
126 |
+
readonly=True
|
127 |
+
)
|
128 |
+
|
129 |
+
search_button.click(
|
130 |
+
fn=chatbot_interface,
|
131 |
+
inputs=input_text,
|
132 |
+
outputs=output_text
|
133 |
+
)
|
134 |
+
|
135 |
+
gr.Markdown("""
|
136 |
+
### How to use:
|
137 |
+
1. Enter your search request in natural language
|
138 |
+
2. Click the Search button
|
139 |
+
3. View the results from your Raindrop.io bookmarks
|
140 |
+
|
141 |
+
The assistant will convert your request into an optimized search query and fetch relevant results.
|
142 |
+
""")
|
143 |
+
|
144 |
+
# Launch the interface
|
145 |
+
if __name__ == "__main__":
|
146 |
+
demo.launch(share=True)
|