Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,37 @@
|
|
1 |
-
|
2 |
-
from langchain.
|
3 |
-
from
|
4 |
-
|
5 |
-
|
6 |
-
StableDiffusionPromptGeneratorTool,
|
7 |
-
TextToVideoTool,
|
8 |
-
)
|
9 |
|
10 |
-
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
]
|
20 |
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
)
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langchain.document_loaders.base import Document
|
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 |
+
# Function to fetch website content using the updated actor
|
11 |
+
def fetch_website_content(website_url):
|
12 |
+
apify_client = ApifyClient("apify_api_uz0y556N4IG2aLcESj67kmnGSUpHF12XAkLp")
|
13 |
+
run_input = {"startUrls": [{"url": website_url}]}
|
14 |
+
run = apify_client.actor("moJRLRc85AitArpNN").call(run_input=run_input)
|
15 |
+
items = list(apify_client.dataset(run["defaultDatasetId"]).iterate_items())
|
16 |
+
return items if items else None
|
|
|
17 |
|
18 |
+
# Fetch and index website content
|
19 |
+
content = fetch_website_content("https://python.langchain.com/en/latest/")
|
20 |
+
documents = [Document(page_content=item["text"] or "", metadata={"source": item["url"]}) for item in content]
|
21 |
+
index = VectorstoreIndexCreator().from_loaders([documents])
|
22 |
|
23 |
+
# Function for the Gradio UI
|
24 |
+
def ask_langchain(question):
|
25 |
+
result = index.query_with_sources(question)
|
26 |
+
answer = result["answer"]
|
27 |
+
sources = ", ".join(result["sources"])
|
28 |
+
return f"{answer}\n\nSources: {sources}"
|
29 |
+
|
30 |
+
# Gradio interface
|
31 |
+
iface = gr.Interface(fn=ask_langchain,
|
32 |
+
inputs="text",
|
33 |
+
outputs="text",
|
34 |
+
live=True,
|
35 |
+
title="LangChain Query",
|
36 |
+
description="Ask a question about LangChain based on the indexed content.")
|
37 |
+
iface.launch()
|