Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +88 -0
- requirements.txt +14 -0
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from langchain_openai import ChatOpenAI
|
| 5 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 6 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
+
from langchain.document_loaders import WikipediaLoader # Import for loading Wikipedia articles
|
| 8 |
+
|
| 9 |
+
# Load environment variables from a .env file
|
| 10 |
+
load_dotenv() # Adjust the path if necessary
|
| 11 |
+
|
| 12 |
+
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 13 |
+
|
| 14 |
+
# Create a prompt template
|
| 15 |
+
prompt = ChatPromptTemplate.from_messages(
|
| 16 |
+
[
|
| 17 |
+
('system', "You are a helpful assistant. Please respond to the question asked based mostly on the context provided. Context: {context}. If not present in context, say not present in the given context"),
|
| 18 |
+
("user", "Question: {question}")
|
| 19 |
+
]
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
st.title("Question and Answering")
|
| 23 |
+
|
| 24 |
+
# Initialize session state variables if not present
|
| 25 |
+
if "input_type" not in st.session_state:
|
| 26 |
+
st.session_state.input_type = "Direct Text Input"
|
| 27 |
+
if "input_text" not in st.session_state:
|
| 28 |
+
st.session_state.input_text = ""
|
| 29 |
+
if "context_text" not in st.session_state:
|
| 30 |
+
st.session_state.context_text = ""
|
| 31 |
+
|
| 32 |
+
# Function to reset session state when switching input type
|
| 33 |
+
def reset_inputs():
|
| 34 |
+
st.session_state.input_text = ""
|
| 35 |
+
st.session_state.context_text = ""
|
| 36 |
+
|
| 37 |
+
# Sidebar for selecting input type with reset callback
|
| 38 |
+
input_type = st.sidebar.selectbox(
|
| 39 |
+
'Select Input Type',
|
| 40 |
+
('Direct Text Input', 'Wikipedia Topic'),
|
| 41 |
+
key="input_type",
|
| 42 |
+
on_change=reset_inputs
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Handle input based on the selected type
|
| 46 |
+
if input_type == 'Direct Text Input':
|
| 47 |
+
# Direct Text Input Mode
|
| 48 |
+
context_text = st.text_area("Enter your context here:", key="direct_context")
|
| 49 |
+
question = st.text_input(
|
| 50 |
+
"What question do you have in mind?",
|
| 51 |
+
value=st.session_state.input_text,
|
| 52 |
+
key="direct_question"
|
| 53 |
+
)
|
| 54 |
+
st.session_state.input_text = question
|
| 55 |
+
st.session_state.context_text = context_text
|
| 56 |
+
else:
|
| 57 |
+
# Wikipedia Topic Mode
|
| 58 |
+
topic = st.text_input("Enter Wikipedia Topic:", key="wiki_topic")
|
| 59 |
+
if topic:
|
| 60 |
+
# Load the Wikipedia article
|
| 61 |
+
full_content = WikipediaLoader(query=topic, load_max_docs=2).load()
|
| 62 |
+
|
| 63 |
+
# Limit the content size (e.g., first 1000 characters)
|
| 64 |
+
max_length = 1000
|
| 65 |
+
st.session_state.context_text = full_content[:max_length] # Truncate to max_length
|
| 66 |
+
if len(full_content) > max_length:
|
| 67 |
+
st.session_state.context_text += "..." # Indicate that the text has been truncated
|
| 68 |
+
|
| 69 |
+
# Ensure the question input starts empty
|
| 70 |
+
question = st.text_input(
|
| 71 |
+
"What question do you have in mind?",
|
| 72 |
+
value=st.session_state.input_text,
|
| 73 |
+
key="wiki_question"
|
| 74 |
+
)
|
| 75 |
+
st.session_state.input_text = question
|
| 76 |
+
|
| 77 |
+
# Initialize the OpenAI model
|
| 78 |
+
llm = ChatOpenAI(model="gpt-3.5-turbo") # Use OpenAI model
|
| 79 |
+
output_parser = StrOutputParser()
|
| 80 |
+
chain = prompt | llm | output_parser
|
| 81 |
+
|
| 82 |
+
if st.session_state.input_text:
|
| 83 |
+
# Use context_text if provided, otherwise use the loaded Wikipedia content
|
| 84 |
+
response = chain.invoke({
|
| 85 |
+
"question": st.session_state.input_text,
|
| 86 |
+
"context": st.session_state.context_text
|
| 87 |
+
})
|
| 88 |
+
st.write(response)
|
requirements.txt
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
python-dotenv
|
| 3 |
+
langchain-community
|
| 4 |
+
bs4 #beautifulSoup
|
| 5 |
+
Wikipedia
|
| 6 |
+
langchain-text-splitters
|
| 7 |
+
langchain-openai
|
| 8 |
+
chromadb
|
| 9 |
+
sentence-transformers
|
| 10 |
+
langchain_huggingface
|
| 11 |
+
faiss-cpu
|
| 12 |
+
langchain_chroma
|
| 13 |
+
beautifulsoup4
|
| 14 |
+
streamlit
|