Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,109 +8,6 @@ from langchain_chroma import Chroma
|
|
| 8 |
from langchain.chains import ConversationalRetrievalChain
|
| 9 |
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 10 |
|
| 11 |
-
# # Initialize embedding function here
|
| 12 |
-
# embedding_function = OpenAIEmbeddings()
|
| 13 |
-
|
| 14 |
-
# # Updated cbfs class with dynamic database selection
|
| 15 |
-
# class cbfs:
|
| 16 |
-
# def __init__(self, persist_directory):
|
| 17 |
-
# self.chat_history = []
|
| 18 |
-
# self.answer = ""
|
| 19 |
-
# self.db_query = ""
|
| 20 |
-
# self.db_response = []
|
| 21 |
-
# self.panels = []
|
| 22 |
-
# # Initialize Chroma and the ConversationalRetrievalChain with the chosen database
|
| 23 |
-
# db = Chroma(persist_directory=persist_directory, embedding_function=embedding_function)
|
| 24 |
-
# retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 3})
|
| 25 |
-
# self.qa = ConversationalRetrievalChain.from_llm(
|
| 26 |
-
# llm=ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0),
|
| 27 |
-
# retriever=retriever,
|
| 28 |
-
# return_source_documents=True,
|
| 29 |
-
# return_generated_question=True,
|
| 30 |
-
# )
|
| 31 |
-
|
| 32 |
-
# def convchain(self, query):
|
| 33 |
-
# if not query:
|
| 34 |
-
# return [("User", ""), ("ChatBot", "")]
|
| 35 |
-
# result = self.qa.invoke({"question": query, "chat_history": self.chat_history})
|
| 36 |
-
# self.chat_history.append((query, result["answer"]))
|
| 37 |
-
# self.db_query = result["generated_question"]
|
| 38 |
-
# self.db_response = result["source_documents"]
|
| 39 |
-
# self.answer = result['answer']
|
| 40 |
-
# self.panels.append(["User", query]) # Ensure this is a list of two strings
|
| 41 |
-
# self.panels.append(["ChatBot", self.answer]) # Ensure this is a list of two strings
|
| 42 |
-
# return self.panels
|
| 43 |
-
|
| 44 |
-
# def clr_history(self):
|
| 45 |
-
# self.chat_history = []
|
| 46 |
-
# self.panels = []
|
| 47 |
-
# return self.panels # Clear the chatbot display
|
| 48 |
-
|
| 49 |
-
# # Create Gradio interface functions
|
| 50 |
-
# def initialize_cbfs(db_choice):
|
| 51 |
-
# """Initialize cbfs object based on the database selection and clear history."""
|
| 52 |
-
# if db_choice == "Governance Documents":
|
| 53 |
-
# return cbfs(persist_directory='docs/chroma_eg/')
|
| 54 |
-
# elif db_choice == "Faculty Handbook":
|
| 55 |
-
# return cbfs(persist_directory='docs/chroma_hb/')
|
| 56 |
-
# else:
|
| 57 |
-
# return None
|
| 58 |
-
|
| 59 |
-
# def chat_history(query, db_choice, cb):
|
| 60 |
-
# """Handles chat submissions. Reminds the user to select a document if none is selected."""
|
| 61 |
-
# if db_choice is None:
|
| 62 |
-
# return [("ChatBot", "Please select a document from the dropdown menu before submitting your query.")], ""
|
| 63 |
-
# else:
|
| 64 |
-
# return cb.convchain(query), "" # Clear input box by returning empty string
|
| 65 |
-
|
| 66 |
-
# # def clear_history(cb):
|
| 67 |
-
# # cb.clr_history()
|
| 68 |
-
# # return [], ""
|
| 69 |
-
|
| 70 |
-
# def clear_history(cb):
|
| 71 |
-
# if cb is None: # Check if cbfs instance is None
|
| 72 |
-
# return [], "" # No error message, simply clear the UI components
|
| 73 |
-
# else:
|
| 74 |
-
# cb.clr_history()
|
| 75 |
-
# return [], ""
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
# # Create Gradio UI layout
|
| 79 |
-
# with gr.Blocks() as demo:
|
| 80 |
-
# gr.Markdown("# ISU-Economics Policy and Rules ChatBot")
|
| 81 |
-
|
| 82 |
-
# with gr.Row():
|
| 83 |
-
# db_choice = gr.Dropdown(["Governance Documents", "Faculty Handbook"], label="Select Document", scale=1)
|
| 84 |
-
# button_clearhistory = gr.Button("Clear History", scale=1)
|
| 85 |
-
|
| 86 |
-
# with gr.Row():
|
| 87 |
-
# inp = gr.Textbox(placeholder="Enter text here…", scale=8)
|
| 88 |
-
# button_submit = gr.Button("Submit", scale=1)
|
| 89 |
-
|
| 90 |
-
# output = gr.Chatbot()
|
| 91 |
-
|
| 92 |
-
# # Initialize cbfs instance
|
| 93 |
-
# cbfs_instance = gr.State(initialize_cbfs(db_choice.value))
|
| 94 |
-
|
| 95 |
-
# # Update cbfs_instance and clear chat history when the dropdown value changes
|
| 96 |
-
# def update_cbfs_and_clear_history(db_choice):
|
| 97 |
-
# new_cbfs = initialize_cbfs(db_choice)
|
| 98 |
-
# new_cbfs.clr_history()
|
| 99 |
-
# return new_cbfs, [], "" # Clear the chatbot display and input box
|
| 100 |
-
|
| 101 |
-
# db_choice.change(
|
| 102 |
-
# fn=update_cbfs_and_clear_history,
|
| 103 |
-
# inputs=db_choice,
|
| 104 |
-
# outputs=[cbfs_instance, output, inp]
|
| 105 |
-
# )
|
| 106 |
-
|
| 107 |
-
# # Define interactions
|
| 108 |
-
# inp.submit(fn=chat_history, inputs=[inp, db_choice, cbfs_instance], outputs=[output, inp])
|
| 109 |
-
# button_submit.click(fn=chat_history, inputs=[inp, db_choice, cbfs_instance], outputs=[output, inp])
|
| 110 |
-
# button_clearhistory.click(fn=clear_history, inputs=cbfs_instance, outputs=[output, inp])
|
| 111 |
-
|
| 112 |
-
# # Launch the Gradio app
|
| 113 |
-
# demo.launch()
|
| 114 |
|
| 115 |
import os
|
| 116 |
import openai
|
|
|
|
| 8 |
from langchain.chains import ConversationalRetrievalChain
|
| 9 |
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
import os
|
| 13 |
import openai
|