Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- .env +3 -0
- app.py +186 -69
- requirements.txt +6 -0
.env
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
GROQ_API_KEY=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
|
| 2 |
+
BRIGHT_API_KEY=ded1bc00-4d84-4dee-b4d6-c896cadf2417
|
| 3 |
+
BRIGHT_ZONE=web_unlocker2
|
app.py
CHANGED
|
@@ -1,69 +1,186 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
from groq import Groq
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 11 |
+
BRIGHT_API_KEY = os.getenv("BRIGHT_API_KEY")
|
| 12 |
+
BRIGHT_ZONE = os.getenv("BRIGHT_ZONE")
|
| 13 |
+
|
| 14 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 15 |
+
|
| 16 |
+
# -------------------------------
|
| 17 |
+
# BRIGHT DATA WEB UNLOCKER
|
| 18 |
+
# -------------------------------
|
| 19 |
+
|
| 20 |
+
def brightdata_request(target_url):
|
| 21 |
+
try:
|
| 22 |
+
response = requests.post(
|
| 23 |
+
"https://api.brightdata.com/request",
|
| 24 |
+
headers={
|
| 25 |
+
"Content-Type": "application/json",
|
| 26 |
+
"Authorization": f"Bearer {BRIGHT_API_KEY}"
|
| 27 |
+
},
|
| 28 |
+
json={
|
| 29 |
+
"zone": BRIGHT_ZONE,
|
| 30 |
+
"url": target_url,
|
| 31 |
+
"format": "raw"
|
| 32 |
+
},
|
| 33 |
+
timeout=60
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
response.raise_for_status()
|
| 37 |
+
return response.text
|
| 38 |
+
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"ERROR: {str(e)}"
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# -------------------------------
|
| 44 |
+
# SCRAPER
|
| 45 |
+
# -------------------------------
|
| 46 |
+
|
| 47 |
+
def scrape_goodreads(url):
|
| 48 |
+
html = brightdata_request(url)
|
| 49 |
+
|
| 50 |
+
if html.startswith("ERROR"):
|
| 51 |
+
return [], html
|
| 52 |
+
|
| 53 |
+
soup = BeautifulSoup(html, "html.parser")
|
| 54 |
+
books = []
|
| 55 |
+
|
| 56 |
+
rows = soup.find_all("tr")
|
| 57 |
+
|
| 58 |
+
for row in rows:
|
| 59 |
+
title_tag = row.find("a", class_="bookTitle")
|
| 60 |
+
author_tag = row.find("a", class_="authorName")
|
| 61 |
+
rating_tag = row.find("span", class_="minirating")
|
| 62 |
+
|
| 63 |
+
if title_tag and author_tag and rating_tag:
|
| 64 |
+
books.append({
|
| 65 |
+
"title": title_tag.get_text(strip=True),
|
| 66 |
+
"author": author_tag.get_text(strip=True),
|
| 67 |
+
"rating": rating_tag.get_text(strip=True)
|
| 68 |
+
})
|
| 69 |
+
|
| 70 |
+
if not books:
|
| 71 |
+
return [], "No books found. Make sure the URL is a Goodreads list page."
|
| 72 |
+
|
| 73 |
+
return books[:10], None
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# -------------------------------
|
| 77 |
+
# AI Q&A
|
| 78 |
+
# -------------------------------
|
| 79 |
+
|
| 80 |
+
def qa_bot(url, question):
|
| 81 |
+
books, error = scrape_goodreads(url)
|
| 82 |
+
|
| 83 |
+
if error:
|
| 84 |
+
return f"β {error}"
|
| 85 |
+
|
| 86 |
+
context = "\n".join(
|
| 87 |
+
[f"{i+1}. {b['title']} by {b['author']} - {b['rating']}"
|
| 88 |
+
for i, b in enumerate(books)]
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
system_prompt = f"""
|
| 92 |
+
You are a helpful assistant.
|
| 93 |
+
|
| 94 |
+
Here is book data scraped from Goodreads:
|
| 95 |
+
|
| 96 |
+
{context}
|
| 97 |
+
|
| 98 |
+
Answer questions ONLY using this data.
|
| 99 |
+
If the answer is not present, say "The data does not contain that information."
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
try:
|
| 103 |
+
response = client.chat.completions.create(
|
| 104 |
+
model="llama-3.1-8b-instant",
|
| 105 |
+
messages=[
|
| 106 |
+
{"role": "system", "content": system_prompt},
|
| 107 |
+
{"role": "user", "content": question}
|
| 108 |
+
]
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
return response.choices[0].message.content
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
return f"β AI Error: {str(e)}"
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
# -------------------------------
|
| 118 |
+
# UI LOGIC FUNCTIONS
|
| 119 |
+
# -------------------------------
|
| 120 |
+
|
| 121 |
+
def scrape_and_display(url):
|
| 122 |
+
books, error = scrape_goodreads(url)
|
| 123 |
+
|
| 124 |
+
if error:
|
| 125 |
+
return gr.update(value=[]), error
|
| 126 |
+
|
| 127 |
+
table_data = [[b["title"], b["author"], b["rating"]] for b in books]
|
| 128 |
+
|
| 129 |
+
return table_data, "β
Successfully scraped top 10 books."
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
# -------------------------------
|
| 133 |
+
# GRADIO UI
|
| 134 |
+
# -------------------------------
|
| 135 |
+
|
| 136 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Goodreads AI Explorer") as demo:
|
| 137 |
+
|
| 138 |
+
gr.Markdown("""
|
| 139 |
+
# π Goodreads AI Explorer
|
| 140 |
+
Scrape book data from a Goodreads list and ask AI questions about it.
|
| 141 |
+
""")
|
| 142 |
+
|
| 143 |
+
with gr.Row():
|
| 144 |
+
with gr.Column(scale=2):
|
| 145 |
+
url_input = gr.Textbox(
|
| 146 |
+
label="π Goodreads URL",
|
| 147 |
+
placeholder="Paste a Goodreads list URL here..."
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
scrape_btn = gr.Button("π₯ Scrape Books", variant="primary")
|
| 151 |
+
|
| 152 |
+
status_output = gr.Markdown()
|
| 153 |
+
|
| 154 |
+
books_table = gr.Dataframe(
|
| 155 |
+
headers=["Title", "Author", "Rating"],
|
| 156 |
+
datatype=["str", "str", "str"],
|
| 157 |
+
interactive=False,
|
| 158 |
+
wrap=True
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
with gr.Column(scale=3):
|
| 162 |
+
question_input = gr.Textbox(
|
| 163 |
+
label="π¬ Ask a Question",
|
| 164 |
+
placeholder="Which book has the highest rating?"
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
ask_btn = gr.Button("π€ Ask AI", variant="secondary")
|
| 168 |
+
|
| 169 |
+
answer_output = gr.Markdown()
|
| 170 |
+
|
| 171 |
+
# Button Actions
|
| 172 |
+
|
| 173 |
+
scrape_btn.click(
|
| 174 |
+
fn=scrape_and_display,
|
| 175 |
+
inputs=url_input,
|
| 176 |
+
outputs=[books_table, status_output]
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
ask_btn.click(
|
| 180 |
+
fn=qa_bot,
|
| 181 |
+
inputs=[url_input, question_input],
|
| 182 |
+
outputs=answer_output
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
if __name__ == "__main__":
|
| 186 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|
| 3 |
+
beautifulsoup4
|
| 4 |
+
pandas
|
| 5 |
+
groq
|
| 6 |
+
python-dotenv
|