File size: 1,550 Bytes
236af6f
 
 
 
 
34893f5
236af6f
 
34893f5
236af6f
 
 
 
 
 
 
34893f5
236af6f
 
 
 
34893f5
236af6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from langchain.document_loaders.base import Document
from langchain.indexes import VectorstoreIndexCreator
from apify_client import ApifyClient
import os

# Update with your OpenAI API key
os.environ["OPENAI_API_KEY"] = "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"

# Function to fetch website content using the updated actor
def fetch_website_content(website_url):
    apify_client = ApifyClient("apify_api_uz0y556N4IG2aLcESj67kmnGSUpHF12XAkLp")
    run_input = {"startUrls": [{"url": website_url}]}
    run = apify_client.actor("moJRLRc85AitArpNN").call(run_input=run_input)
    items = list(apify_client.dataset(run["defaultDatasetId"]).iterate_items())
    return items if items else None

# Fetch and index website content
content = fetch_website_content("https://python.langchain.com/en/latest/")
documents = [Document(page_content=item["text"] or "", metadata={"source": item["url"]}) for item in content]
index = VectorstoreIndexCreator().from_loaders([documents])

# Function for the Gradio UI
def ask_langchain(question):
    result = index.query_with_sources(question)
    answer = result["answer"]
    sources = ", ".join(result["sources"])
    return f"{answer}\n\nSources: {sources}"

# Gradio interface
iface = gr.Interface(fn=ask_langchain, 
                     inputs="text", 
                     outputs="text",
                     live=True,
                     title="LangChain Query",
                     description="Ask a question about LangChain based on the indexed content.")
iface.launch()