Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,66 @@
|
|
| 1 |
-
#
|
| 2 |
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 5 |
-
from langchain.
|
| 6 |
-
from langchain.
|
| 7 |
|
| 8 |
-
# 1.
|
| 9 |
-
|
| 10 |
-
os.environ["TAVILY_API_KEY"] = "your-tavily-api-key"
|
| 11 |
|
| 12 |
-
# 2.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
llm = ChatGoogleGenerativeAI(
|
| 14 |
model="models/gemini-1.5-flash",
|
| 15 |
-
google_api_key=
|
| 16 |
)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
retriever=retriever,
|
| 25 |
-
return_source_documents=True
|
| 26 |
)
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
print("\n
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
print("\n
|
| 37 |
-
|
| 38 |
-
print("-", doc.metadata.get("source", "Unknown"))
|
| 39 |
|
| 40 |
-
#
|
| 41 |
if __name__ == "__main__":
|
| 42 |
-
url = input("Enter website URL: ")
|
| 43 |
-
question = input("
|
| 44 |
-
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
|
| 3 |
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from tavily import TavilyClient
|
| 6 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 7 |
+
from langchain.chains import LLMChain
|
| 8 |
+
from langchain.prompts import PromptTemplate
|
| 9 |
|
| 10 |
+
# 1. Load environment variables from .env file (if present)
|
| 11 |
+
load_dotenv()
|
|
|
|
| 12 |
|
| 13 |
+
# 2. Set API keys from environment
|
| 14 |
+
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 15 |
+
TAVILY_API_KEY = os.environ.get("TAVILY_API_KEY")
|
| 16 |
+
|
| 17 |
+
# 3. Initialize Gemini LLM
|
| 18 |
llm = ChatGoogleGenerativeAI(
|
| 19 |
model="models/gemini-1.5-flash",
|
| 20 |
+
google_api_key=GOOGLE_API_KEY
|
| 21 |
)
|
| 22 |
|
| 23 |
+
# 4. Tavily Client
|
| 24 |
+
tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
|
| 25 |
+
|
| 26 |
+
def extract_website_text(url):
|
| 27 |
+
result = tavily_client.extract(urls=url)
|
| 28 |
+
if result and "text" in result[0]:
|
| 29 |
+
return result[0]["text"]
|
| 30 |
+
return "Could not extract content from the URL."
|
| 31 |
+
|
| 32 |
+
# 5. Prompt Template
|
| 33 |
+
prompt = PromptTemplate(
|
| 34 |
+
input_variables=["website_content", "question"],
|
| 35 |
+
template="""
|
| 36 |
+
You are an intelligent assistant. Based on the following website content:
|
| 37 |
+
|
| 38 |
+
{website_content}
|
| 39 |
|
| 40 |
+
Answer the following question:
|
| 41 |
+
{question}
|
| 42 |
+
"""
|
|
|
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# 6. LLM QA Chain
|
| 46 |
+
qa_chain = LLMChain(llm=llm, prompt=prompt)
|
| 47 |
+
|
| 48 |
+
# 7. Main Chat Function
|
| 49 |
+
def ask_from_website(url, question):
|
| 50 |
+
print(f"\n🔗 Extracting content from: {url}")
|
| 51 |
+
website_text = extract_website_text(url)
|
| 52 |
|
| 53 |
+
print(f"\n💬 Asking: {question}")
|
| 54 |
+
response = qa_chain.invoke({
|
| 55 |
+
"website_content": website_text,
|
| 56 |
+
"question": question
|
| 57 |
+
})
|
| 58 |
|
| 59 |
+
print("\n✅ Answer:")
|
| 60 |
+
print(response["text"])
|
|
|
|
| 61 |
|
| 62 |
+
# 8. Run
|
| 63 |
if __name__ == "__main__":
|
| 64 |
+
url = input("Enter the website URL: ")
|
| 65 |
+
question = input("What do you want to ask about this website? ")
|
| 66 |
+
ask_from_website(url, question)
|