Spaces:
Build error
Build error
Commit ·
4599cdf
1
Parent(s): aad227c
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,77 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
|
| 3 |
-
from langchain.agents import AgentExecutor
|
| 4 |
-
from langchain.llms import OpenAI
|
| 5 |
-
from langchain.memory import ConversationBufferMemory
|
| 6 |
-
from langchain.agents.agent_types import ChatAgent
|
| 7 |
-
from langchain.data_ops import load_text_file, summarize, question_answering, extract_keywords, search
|
| 8 |
-
|
| 9 |
-
# Initialize OpenAI LLM
|
| 10 |
-
llm = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) # Access API key from environment variable
|
| 11 |
-
|
| 12 |
-
# Define prompts
|
| 13 |
-
chat_prompt = """
|
| 14 |
-
Hi there! I'm your Langchain assistant. Ask me anything about Langchain or upload a document for me to analyze.
|
| 15 |
-
"""
|
| 16 |
-
doc_prompt = """
|
| 17 |
-
What would you like me to do with this document? (e.g., summarize, answer questions, extract keywords, search documentation for relevant information)
|
| 18 |
-
"""
|
| 19 |
-
|
| 20 |
-
# Initialize agent
|
| 21 |
-
agent = ChatAgent(
|
| 22 |
-
llm=llm,
|
| 23 |
-
prompt=chat_prompt,
|
| 24 |
-
memory=ConversationBufferMemory(),
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
# Initialize AgentExecutor
|
| 28 |
-
agent_executor = AgentExecutor(agent=agent)
|
| 29 |
-
|
| 30 |
-
# Streamlit app
|
| 31 |
-
st.title("Langchain Chat & Document Analysis")
|
| 32 |
-
|
| 33 |
-
# Upload document
|
| 34 |
-
uploaded_file = st.file_uploader("Upload a document (txt, pdf, doc, docx):")
|
| 35 |
-
|
| 36 |
-
# Input field for user query
|
| 37 |
-
user_query = st.text_area("Ask me anything:")
|
| 38 |
-
|
| 39 |
-
# Choose document or chat mode
|
| 40 |
-
is_doc_mode = uploaded_file is not None
|
| 41 |
-
|
| 42 |
-
# Execute agent based on mode
|
| 43 |
-
if is_doc_mode:
|
| 44 |
-
# Load document text
|
| 45 |
-
doc_text = load_text_file(uploaded_file)
|
| 46 |
-
|
| 47 |
-
# Update chat prompt with document information
|
| 48 |
-
agent.update_prompt(doc_prompt, {"doc_text": doc_text})
|
| 49 |
-
|
| 50 |
-
# Get user instructions for document processing
|
| 51 |
-
doc_action = st.text_input("Instructions for document processing:")
|
| 52 |
-
|
| 53 |
-
# Analyze document based on user instructions
|
| 54 |
-
if "summarize" in doc_action.lower():
|
| 55 |
-
summary = summarize(doc_text)
|
| 56 |
-
st.write(f"Document summary: {summary}")
|
| 57 |
-
elif "answer question" in doc_action.lower():
|
| 58 |
-
answer = question_answering(doc_text, doc_action)
|
| 59 |
-
st.write(f"Answer to your question: {answer}")
|
| 60 |
-
elif "extract keywords" in doc_action.lower():
|
| 61 |
-
keywords = extract_keywords(doc_text)
|
| 62 |
-
st.write(f"Keywords extracted: {keywords}")
|
| 63 |
-
elif "search documentation" in doc_action.lower():
|
| 64 |
-
search_results = search(doc_action, sources=["documentation"])
|
| 65 |
-
st.write(f"Documentation search results: {search_results}")
|
| 66 |
-
else:
|
| 67 |
-
st.write("Please provide specific instructions for document processing.")
|
| 68 |
-
|
| 69 |
-
# Update chat prompt with document removed
|
| 70 |
-
agent.update_prompt(chat_prompt)
|
| 71 |
-
else:
|
| 72 |
-
# Execute agent with user query
|
| 73 |
-
response = agent_executor.ainvoke({"input": user_query})
|
| 74 |
-
|
| 75 |
-
# Display response
|
| 76 |
-
st.write(response["output"])
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|