antfraia commited on
Commit
236af6f
·
1 Parent(s): 69cd53b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -27
app.py CHANGED
@@ -1,31 +1,37 @@
1
- from langchain.agents import initialize_agent
2
- from langchain.llms import OpenAI
3
- from gradio_tools.tools import (
4
- StableDiffusionTool,
5
- ImageCaptioningTool,
6
- StableDiffusionPromptGeneratorTool,
7
- TextToVideoTool,
8
- )
9
 
10
- from langchain.memory import ConversationBufferMemory
 
11
 
12
- llm = OpenAI(temperature=0)
13
- memory = ConversationBufferMemory(memory_key="chat_history")
14
- tools = [
15
- StableDiffusionTool().langchain,
16
- ImageCaptioningTool().langchain,
17
- StableDiffusionPromptGeneratorTool().langchain,
18
- TextToVideoTool().langchain,
19
- ]
20
 
 
 
 
 
21
 
22
- agent = initialize_agent(
23
- tools, llm, memory=memory, agent="conversational-react-description", verbose=True
24
- )
25
- output = agent.run(
26
- input=(
27
- "Please create a photo of a dog riding a skateboard "
28
- "but improve my prompt prior to using an image generator."
29
- "Please caption the generated image and create a video for it using the improved prompt."
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()