Spaces:
Runtime error
Runtime error
Download app.py from Kapitest/ChatbotAyu: direct link, hf CLI and curl.
- Browser
- Download file 4.33 kB
-
https://huggingface.co/spaces/Kapitest/ChatbotAyu/resolve/main/app.py
- Command line
-
hf download hf://spaces/Kapitest/ChatbotAyu/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Kapitest/ChatbotAyu/resolve/main/app.py
4.33 kB
| import os | |
| import gradio as gr | |
| import openai | |
| from langchain.embeddings.openai import OpenAIEmbeddings | |
| from langchain.vectorstores import Chroma | |
| from langchain.text_splitter import TokenTextSplitter | |
| from langchain.llms import OpenAI | |
| from langchain.chains import ChatVectorDBChain | |
| from langchain.chains import ConversationalRetrievalChain | |
| from langchain.document_loaders import UnstructuredWordDocumentLoader, TextLoader | |
| from langchain.document_loaders.csv_loader import CSVLoader | |
| from langchain.prompts.prompt import PromptTemplate | |
| os.environ["OPENAI_API_KEY"] = os.getenv("secret1") | |
| openai.api_key = os.getenv("secret1") | |
| def get_document(): | |
| loader1= CSVLoader(file_path='AllProductsTest2.csv') | |
| data1 = loader1.load() | |
| loader2= CSVLoader(file_path='BlogLinks.csv') | |
| data2 = loader2.load() | |
| return data1+data2 | |
| my_data = get_document() | |
| words=["hi", "hii", "hiii", "hey", "heyy", "heyyy", "hello", "helloo", "hellooo", "g morning", "gm", | |
| "gmorning", "good morning", "goodmorning", "morning","mornin", "good day", "good afternoon", | |
| "good evening", "greetings", "greeting", | |
| "good to see you", "its good seeing you", "how are you", "how're you", | |
| "how are you doing", "how ya doin'", "how ya doin", "how is everything", | |
| "how is everything going", "how's everything going", "how is you", "how's you", | |
| "how are things", "how're things", "how is it going", "how's it going", "how's it goin'", | |
| "how's it goin", "how is life been treating you", "how's life been treating you", | |
| "how have you been", "how've you been", "what is up", "what's up", "what is cracking", | |
| "what's cracking", "what is good", "what's good", "what is happening", "what's happening", | |
| "what is new", "what's new", "what is neww", "gโday", "howdy", | |
| "who are you", "who you", "namaste","who r u"] | |
| text_splitter = TokenTextSplitter(chunk_size=500, chunk_overlap=50) | |
| my_doc = text_splitter.split_documents(my_data) | |
| embeddings = OpenAIEmbeddings() | |
| vectordb = Chroma.from_documents(my_doc, embeddings) | |
| greet="""Hello there! ๐ I'm Dr. Aayush, your friendly health partner with wisdom of ayurveda from Kapiva. ๐ Don't hesitate to reach out with any health questions or worries you might have. Whether it's a bothersome symptom, a curiosity about staying healthy, or just a chat about well-being, I'm here to provide you with a caring and understanding ear, infused with the goodness of Ayurveda. ๐ผ Your health is important, and with Kapiva, I'm here to support you every step of the way. ๐ช๐ฅ๐ | |
| """ | |
| template = open("instruct.txt").read() | |
| QA_PROMPT = PromptTemplate(template=template, input_variables=["question", "context"]) | |
| def generate_response(query,chat_history): | |
| if query: | |
| llm = OpenAI(temperature=0.7 , model_name="gpt-3.5-turbo") | |
| my_qa = ChatVectorDBChain.from_llm(llm, vectordb, qa_prompt=QA_PROMPT, return_source_documents=True) | |
| result = my_qa({"question": query, "chat_history": chat_history}) | |
| return result["answer"] | |
| def my_chatbot(input, history): | |
| history = history or [] | |
| my_history = list(sum(history, ())) | |
| my_history.append(input) | |
| my_input = ' '.join(my_history) | |
| lowin=input.lower().strip( ) | |
| output=[] | |
| for el in words: | |
| if lowin==el: | |
| output=greet | |
| if len(history)>0: | |
| output=output+"\n\nPlease type 'clear' to start a new conversation" | |
| break | |
| print(output) | |
| #Clear history | |
| if lowin=='clear': | |
| history=[] | |
| output='Conversation cleared, please state your new query ' | |
| history.append((' ', output)) | |
| history=[] | |
| if len((output))==0: | |
| output = generate_response(input,history) | |
| output=output+"\n\nPlease type 'clear' to start a new conversation" | |
| print(len(history)) | |
| history.append((input, output)) | |
| if len(history)>15: | |
| del history[:5] | |
| return history, history | |
| with gr.Blocks() as demo: | |
| gr.Markdown("""<h1><center>Ayurveda Chatbot Test</center></h1>""") | |
| chatbot = gr.Chatbot() | |
| state = gr.State() | |
| text = gr.Textbox(placeholder="Enter Query") | |
| text.submit(my_chatbot, inputs=[text, state], outputs=[chatbot, state]) | |
| text.submit(lambda x: gr.update(value=''), [],[text]) | |
| demo.launch() |