Spaces:
Sleeping
Sleeping
| import os | |
| import requests | |
| import pandas as pd | |
| from bs4 import BeautifulSoup | |
| import gradio as gr | |
| from dotenv import load_dotenv | |
| import os | |
| load_dotenv() | |
| bright_key = os.getenv("BRIGHTDATA_API_KEY") | |
| groq_key = os.getenv("GROQ_API_KEY") | |
| print(bright_key[:5]) | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| BRIGHTDATA_API_KEY = os.getenv("BRIGHTDATA_API_KEY") | |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") | |
| import requests | |
| target_url = "https://www.goodreads.com/list/show/1.Best_Books_Ever" | |
| brightdata_url = "https://api.brightdata.com/request" | |
| headers = { | |
| "Authorization": f"Bearer {BRIGHTDATA_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "url": target_url, | |
| "zone": "web_unlocker1", | |
| "format": "raw" | |
| } | |
| response = requests.post(brightdata_url, json=payload, headers=headers) | |
| html_content = response.text | |
| print(html_content[:1000]) | |
| from bs4 import BeautifulSoup | |
| soup = BeautifulSoup(html_content, "html.parser") | |
| books = [] | |
| rows = soup.select("tr[itemtype='http://schema.org/Book']") | |
| for row in rows: | |
| title = row.select_one("a.bookTitle span") | |
| author = row.select_one("a.authorName span") | |
| rating = row.select_one("span.minirating") | |
| if title and author and rating: | |
| books.append({ | |
| "title": title.text.strip(), | |
| "author": author.text.strip(), | |
| "rating": rating.text.strip() | |
| }) | |
| print(books[:3]) | |
| import pandas as pd | |
| df = pd.DataFrame(books) | |
| print(df.head()) | |
| books_context = "\n".join( | |
| [f"{i+1}. {b['title']} by {b['author']} - {b['rating']}" | |
| for i, b in enumerate(books)] | |
| ) | |
| system_prompt = f""" | |
| You are a book information assistant. | |
| Here is the scraped Goodreads book ranking data: | |
| {books_context} | |
| Answer user questions ONLY using this data. | |
| If information is not present, say you don't know. | |
| """ | |
| # --- Groq API safe call function --- | |
| def ask_groq(messages): | |
| url = "https://api.groq.com/openai/v1/chat/completions" | |
| headers = { | |
| "Authorization": f"Bearer {GROQ_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "model": "llama-3.1-8b-instant", # supported model | |
| "messages": messages | |
| } | |
| try: | |
| response = requests.post(url, headers=headers, json=payload) | |
| except Exception as e: | |
| return f"Error connecting to Groq API: {e}" | |
| if response.status_code != 200: | |
| return f"API error {response.status_code}: {response.text}" | |
| try: | |
| data = response.json() | |
| except Exception as e: | |
| return f"Error parsing JSON: {e}" | |
| if "choices" in data and len(data["choices"]) > 0: | |
| return data["choices"][0]["message"]["content"] | |
| else: | |
| return f"Unexpected API response: {data}" | |
| # --- Gradio chatbot function --- | |
| history = [] | |
| def chatbot(user_input, chat_history): | |
| if chat_history is None: | |
| chat_history = [] | |
| # Messages to send to Groq LLM | |
| messages_for_groq = [{"role": "system", "content": system_prompt}] | |
| # Add existing messages | |
| for msg in chat_history: | |
| messages_for_groq.append({"role": "user" if msg["role"]=="user" else "assistant", "content": msg["content"]}) | |
| # Add new user input | |
| messages_for_groq.append({"role": "user", "content": str(user_input)}) | |
| # Call Groq API | |
| response = ask_groq(messages_for_groq) | |
| # Append new messages to chat_history | |
| chat_history.append({"role": "user", "content": str(user_input)}) | |
| chat_history.append({"role": "assistant", "content": str(response)}) | |
| # Return two outputs: chat history (list of dicts) and empty string to clear textbox | |
| return chat_history, "" | |
| with gr.Blocks() as demo: | |
| chatbot_output = gr.Chatbot() # expects list of dicts with 'role' and 'content' | |
| user_input = gr.Textbox(placeholder="Ask about Goodreads books...") | |
| send_btn = gr.Button("Send") | |
| send_btn.click(chatbot, inputs=[user_input, chatbot_output], outputs=[chatbot_output, user_input]) | |
| if __name__ == "__main__": | |
| demo.launch(share=False) # or True if you want a public link |