Spaces:
Runtime error
Runtime error
Delete gradio_pr.py
Browse files- gradio_pr.py +0 -89
gradio_pr.py
DELETED
@@ -1,89 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import pathlib
|
4 |
-
import random
|
5 |
-
#import torch
|
6 |
-
#import transformers
|
7 |
-
from langchain.document_loaders import TextLoader
|
8 |
-
from langchain.text_splitter import CharacterTextSplitter
|
9 |
-
from langchain.embeddings.openai import OpenAIEmbeddings
|
10 |
-
from langchain.vectorstores import FAISS
|
11 |
-
from langchain.prompts.chat import (
|
12 |
-
ChatPromptTemplate,
|
13 |
-
SystemMessagePromptTemplate,
|
14 |
-
HumanMessagePromptTemplate,
|
15 |
-
)
|
16 |
-
from langchain.chat_models import ChatOpenAI
|
17 |
-
from langchain.chains import RetrievalQAWithSourcesChain
|
18 |
-
|
19 |
-
os.environ["OPENAI_API_KEY"] = "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"
|
20 |
-
|
21 |
-
# Set the data store directory
|
22 |
-
DATA_STORE_DIR = "data_store"
|
23 |
-
|
24 |
-
if os.path.exists(DATA_STORE_DIR):
|
25 |
-
vector_store = FAISS.load_local(
|
26 |
-
DATA_STORE_DIR,
|
27 |
-
OpenAIEmbeddings()
|
28 |
-
)
|
29 |
-
else:
|
30 |
-
print(f"Missing files. Upload index.faiss and index.pkl files to {DATA_STORE_DIR} directory first")
|
31 |
-
|
32 |
-
system_template = """Use the following pieces of context to answer the user's question.
|
33 |
-
Take note of the sources and include them in the answer in the format: "SOURCES: source1", use "SOURCES" in capital letters regardless of the number of sources.
|
34 |
-
If you don't know the answer, just say "I don't know", don't try to make up an answer.
|
35 |
-
----------------
|
36 |
-
{summaries}"""
|
37 |
-
|
38 |
-
messages = [
|
39 |
-
SystemMessagePromptTemplate.from_template(system_template),
|
40 |
-
HumanMessagePromptTemplate.from_template("{question}")
|
41 |
-
]
|
42 |
-
prompt = ChatPromptTemplate.from_messages(messages)
|
43 |
-
|
44 |
-
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, max_tokens=256)
|
45 |
-
|
46 |
-
chain_type_kwargs = {"prompt": prompt}
|
47 |
-
chain = RetrievalQAWithSourcesChain.from_chain_type(
|
48 |
-
llm=llm,
|
49 |
-
chain_type="stuff",
|
50 |
-
retriever=vector_store.as_retriever(),
|
51 |
-
return_source_documents=True,
|
52 |
-
chain_type_kwargs=chain_type_kwargs
|
53 |
-
)
|
54 |
-
|
55 |
-
|
56 |
-
class Chatbot:
|
57 |
-
def __init__(self):
|
58 |
-
self.query = None
|
59 |
-
|
60 |
-
def chat(self, query):
|
61 |
-
self.query = query
|
62 |
-
result = chain(query)
|
63 |
-
return result['answer']
|
64 |
-
|
65 |
-
|
66 |
-
chatbot = Chatbot()
|
67 |
-
|
68 |
-
|
69 |
-
# Create a Gradio interface
|
70 |
-
def chat_interface(query):
|
71 |
-
response = chatbot.chat(query)
|
72 |
-
return response
|
73 |
-
|
74 |
-
|
75 |
-
# inputs = gr.inputs.Textbox(lines=2, placeholder="Enter your message here...")
|
76 |
-
# outputs = gr.outputs.Textbox()
|
77 |
-
|
78 |
-
# chat_interface = gr.ChatInterface(chat_interface, inputs=inputs, outputs=outputs)
|
79 |
-
#
|
80 |
-
# chat_interface.launch()
|
81 |
-
gr.ChatInterface(
|
82 |
-
chat_interface,
|
83 |
-
chatbot=gr.Chatbot(height=300),
|
84 |
-
textbox=gr.Textbox(placeholder="Ask me a yes or no question"),
|
85 |
-
description="Ask Yes Man any question",
|
86 |
-
theme="soft",
|
87 |
-
cache_examples=True,
|
88 |
-
clear_btn="Clear",
|
89 |
-
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|