Spaces:
Configuration error
Configuration error
Delete app.py
Browse files
app.py
DELETED
@@ -1,64 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from langchain.document_loaders.base import Document, BaseLoader
|
3 |
-
from langchain.indexes import VectorstoreIndexCreator
|
4 |
-
from apify_client import ApifyClient
|
5 |
-
import os
|
6 |
-
|
7 |
-
# Update with your OpenAI API key
|
8 |
-
os.environ["OPENAI_API_KEY"] = "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"
|
9 |
-
|
10 |
-
# Page Function to extract website content
|
11 |
-
page_function_code = """
|
12 |
-
function pageFunction(context) {
|
13 |
-
const $ = context.jQuery;
|
14 |
-
const data = {
|
15 |
-
title: $('title').text(),
|
16 |
-
content: $('body').text(),
|
17 |
-
url: context.request.url
|
18 |
-
};
|
19 |
-
return data;
|
20 |
-
}
|
21 |
-
"""
|
22 |
-
|
23 |
-
# Function to fetch website content using the updated actor
|
24 |
-
def fetch_website_content(website_url):
|
25 |
-
apify_client = ApifyClient("apify_api_uz0y556N4IG2aLcESj67kmnGSUpHF12XAkLp")
|
26 |
-
run_input = {
|
27 |
-
"startUrls": [{"url": website_url}],
|
28 |
-
"pageFunction": page_function_code
|
29 |
-
}
|
30 |
-
run = apify_client.actor("moJRLRc85AitArpNN").call(run_input=run_input)
|
31 |
-
items = list(apify_client.dataset(run["defaultDatasetId"]).iterate_items())
|
32 |
-
return items if items else None
|
33 |
-
|
34 |
-
# Custom loader for our documents
|
35 |
-
class CustomLoader(BaseLoader):
|
36 |
-
def __init__(self, documents):
|
37 |
-
self.documents = documents
|
38 |
-
|
39 |
-
def load(self):
|
40 |
-
return self.documents
|
41 |
-
|
42 |
-
# Fetch content
|
43 |
-
content = fetch_website_content("https://python.langchain.com/en/latest/")
|
44 |
-
documents = [Document(page_content=item["content"] or "", metadata={"source": item.get("url", "Unknown URL")}) for item in content]
|
45 |
-
|
46 |
-
# Use custom loader
|
47 |
-
loader = CustomLoader(documents)
|
48 |
-
index = VectorstoreIndexCreator().from_loaders([loader])
|
49 |
-
|
50 |
-
# Function for the Gradio UI
|
51 |
-
def ask_langchain(question):
|
52 |
-
result = index.query_with_sources(question)
|
53 |
-
answer = result["answer"]
|
54 |
-
sources = ", ".join(result["sources"])
|
55 |
-
return f"{answer}\n\nSources: {sources}"
|
56 |
-
|
57 |
-
# Gradio interface
|
58 |
-
iface = gr.Interface(fn=ask_langchain,
|
59 |
-
inputs="text",
|
60 |
-
outputs="text",
|
61 |
-
live=True,
|
62 |
-
title="LangChain Query",
|
63 |
-
description="Ask a question about LangChain based on the indexed content.")
|
64 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|