File size: 4,278 Bytes
d712cef | 1 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | import requests
import google.generativeai as genai
import os
from dotenv import load_dotenv
# ββ Configuration ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# In a real app, load these from your .env file
ROOT_DIR = os.environ.get('WORKSPACE_ROOT', '.')
env_address = os.path.join(ROOT_DIR, 'backend/.env')
load_dotenv(dotenv_path=env_address)
# Now it will successfully find your keys!
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
JINA_API_KEY = os.environ.get("JINA_API_KEY")
genai.configure(api_key=GEMINI_API_KEY)
def scrape_website_to_markdown(url: str) -> str:
"""
Uses Jina Reader API to cleanly extract text from any URL.
It automatically strips out ads, navbars, and messy HTML.
"""
print(f"π΅οΈββοΈ Scraping {url}...")
jina_url = f"https://r.jina.ai/{url}"
headers = {
# This passes your API key securely to Jina's servers
"Authorization": f"Bearer {JINA_API_KEY}",
"X-Retain-Images": "none"
}
response = requests.get(jina_url, headers=headers)
if response.status_code == 200:
return response.text
else:
raise Exception(f"Failed to scrape website. Status code: {response.status_code}\nResponse: {response.text}")
def generate_interview_answer(company_text: str, company_website: str, max_chars: int = 10000) -> str:
"""
Feeds the scraped text into the LLM with a highly specific prompt.
Safely truncates the text to avoid API Quota limit errors.
"""
# π THE FIX: Chop off the excess text
if len(company_text) > max_chars:
print(f"βοΈ Truncating scraped text from {len(company_text)} to {max_chars} characters...")
company_text = company_text[:max_chars]
print("π§ Synthesizing data and drafting answer...")
# Initialize the model
model = genai.GenerativeModel('gemini-flash-lite-latest')
prompt = f"""
You are an intelligent, well-prepared job bot for {company_website}.
I am going to provide you with the scraped text from their official website.
Based ONLY on this text, I want you to answer the classic question:
"What things company working on and what is their mission? What is their focus and what is there vision?"
Guidelines for your answer:
1. Keep it conversational, confident, and professional (around 3-4 short paragraphs).
2. Identify their core product/service and who their target audience is.
3. Highlight their overarching mission or the main problem they are trying to solve.
4. Mention any recent milestones, unique features, or company values explicitly stated in the text.
5. Do not hallucinate external information. If a detail isn't in the text, don't invent it.
Here is the company website text:
-----------------------------------
{company_text}
"""
response = model.generate_content(prompt)
return response.text
# ββ Execution ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# if __name__ == "__main__":
# # Example Target
# target_url = "https://www.anthropic.com" # Try changing this to another company!
# company_name = "Anthropic"
# try:
# # Step 1: Get the data
# raw_markdown = scrape_website_to_markdown(target_url)
# # π NEW: Print the exact text pulled by the scraper
# print("\n" + "="*50)
# print("π RAW SCRAPED DATA (From Jina):")
# print("="*50 + "\n")
# print(raw_markdown)
# print("\n" + "="*50 + "\n")
# # Step 2: Generate the answer
# answer = generate_interview_answer(raw_markdown, company_name)
# # Step 3: Output
# print("\n==========================================")
# print(f"ποΈ INTERVIEW QUESTION: What do you know about {company_name}?")
# print("==========================================\n")
# print(answer)
# except Exception as e:
# print(f"β An error occurred: {e}") |