Spaces:
Sleeping
Sleeping
iclalcetin
commited on
Commit
•
9fe34ae
1
Parent(s):
d565ffd
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
load_dotenv()
|
3 |
+
import streamlit as st
|
4 |
+
from langchain.text_splitter import CharacterTextSplitter
|
5 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
6 |
+
from langchain.vectorstores import FAISS
|
7 |
+
from langchain.chains.question_answering import load_qa_chain
|
8 |
+
from langchain.llms import OpenAI
|
9 |
+
import os
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
def main():
|
14 |
+
load_dotenv()
|
15 |
+
st.set_page_config(page_title="Chat Text")
|
16 |
+
st.header("Chat Text 💬")
|
17 |
+
|
18 |
+
# Dosyayı oku
|
19 |
+
file_path = 'shipping.txt'
|
20 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
21 |
+
text = file.read()
|
22 |
+
|
23 |
+
# Metni parçalara ayır
|
24 |
+
char_text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000,
|
25 |
+
chunk_overlap=200, length_function=len)
|
26 |
+
text_chunks = char_text_splitter.split_text(text)
|
27 |
+
|
28 |
+
embeddings = OpenAIEmbeddings(
|
29 |
+
openai_api_key=os.getenv('OPENAI_API_KEY')
|
30 |
+
)
|
31 |
+
docsearch = FAISS.from_texts(text_chunks, embeddings)
|
32 |
+
llm = OpenAI()
|
33 |
+
chain = load_qa_chain(llm, chain_type="stuff")
|
34 |
+
|
35 |
+
# Kullanıcıdan soru al
|
36 |
+
query = st.text_input("Type your question:")
|
37 |
+
if query:
|
38 |
+
docs = docsearch.similarity_search(query)
|
39 |
+
response = chain.run(input_documents=docs, question=query)
|
40 |
+
|
41 |
+
st.write(response)
|
42 |
+
|
43 |
+
if __name__ == '__main__':
|
44 |
+
main()
|