Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011121314151617181920214 unmodified lines26272829303132332 unmodified lines363738394041424344454647484950515253545556575859606162636465666768697071import gradio as grfrom huggingface_hub import InferenceClient"""For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference"""client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")def respond(message,history: list[tuple[str, str]],system_message,max_tokens,temperature,top_p,):system_message = "I offer mock interviews, personalized feedback, and valuable insights into common interview questions and industry-specific tips. My goal is to boost your confidence, improve your communication skills, and equip you with the tools needed to impress potential employers."messages = [{"role": "system", "content": system_message}]for val in history:4 unmodified linesmessages.append({"role": "user", "content": message})response = ""for message in client.chat_completion(messages,max_tokens=max_tokens,2 unmodified linestop_p=top_p,):token = message.choices[0].delta.contentresponse += tokenyield response"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""demo = gr.ChatInterface(respond,additional_inputs=[gr.Textbox(value = "You are a good listener and concise and point-specific speaker. You advise relaxation exercises, suggest avoiding negative thoughts, and guide through steps to manage stress. Discuss what's on your mind, or ask me for a quick relaxation exercise.", label="System message"),gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),gr.Slider(minimum=0.1,maximum=1.0,value=0.95,step=0.05,label="Top-p (nucleus sampling)",),],examples = [["What are the most common mistakes candidates make during interviews, and how can I avoid them?"],["Do you have any tips for handling nerves or anxiety during interviews?"],["What are effective strategies for answering behavioral interview questions?"]],title = 'Job Interview Prep Coach')if __name__ == "__main__":demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263644 unmodified lines6970717273747576777879802 unmodified lines8384858687888990919293949596979899100101102103104105106107108109110111112113114import gradio as grfrom huggingface_hub import InferenceClientfrom typing import List, Tupleimport fitz # PyMuPDFfrom sentence_transformers import SentenceTransformer, utilimport numpy as npimport faissclient = InferenceClient("HuggingFaceH4/zephyr-7b-beta")class MyApp:def __init__(self) -> None:self.documents = []self.embeddings = Noneself.index = Noneself.load_pdf("YOURPDFFILE")self.build_vector_db()def load_pdf(self, file_path: str) -> None:"""Extracts text from a PDF file and stores it in the app's documents."""doc = fitz.open(file_path)self.documents = []for page_num in range(len(doc)):page = doc[page_num]text = page.get_text()self.documents.append({"page": page_num + 1, "content": text})print("PDF processed successfully!")def build_vector_db(self) -> None:"""Builds a vector database using the content of the PDF."""model = SentenceTransformer('all-MiniLM-L6-v2')# Generate embeddings for all document contentsself.embeddings = model.encode([doc["content"] for doc in self.documents])# Create a FAISS indexself.index = faiss.IndexFlatL2(self.embeddings.shape[1])# Add the embeddings to the indexself.index.add(np.array(self.embeddings))print("Vector database built successfully!")def search_documents(self, query: str, k: int = 3) -> List[str]:"""Searches for relevant documents using vector similarity."""model = SentenceTransformer('all-MiniLM-L6-v2')# Generate an embedding for the queryquery_embedding = model.encode([query])# Perform a search in the FAISS indexD, I = self.index.search(np.array(query_embedding), k)# Retrieve the top-k documentsresults = [self.documents[i]["content"] for i in I[0]]return results if results else ["No relevant documents found."]app = MyApp()def respond(message: str,history: List[Tuple[str, str]],system_message: str,max_tokens: int,temperature: float,top_p: float,):system_message = "You are a knowledgeable DBT coach. You always talk about one options at at a time. you add greetings and you ask questions like real counsellor. Remember you are helpful and a good listener. You are concise and never ask multiple questions, or give long response. You response like a human counsellor accurately and correctly. consider the users as your client. and practice verbal cues only where needed. Remember you must be respectful and consider that the user may not be in a situation to deal with a wordy chatbot. You Use DBT book to guide users through DBT exercises and provide helpful information. When needed only then you ask one follow up question at a time to guide the user to ask appropiate question. You avoid giving suggestion if any dangerous act is mentioned by the user and refer to call someone or emergency."messages = [{"role": "system", "content": system_message}]for val in history:4 unmodified linesmessages.append({"role": "user", "content": message})# RAG - Retrieve relevant documentsretrieved_docs = app.search_documents(message)context = "\n".join(retrieved_docs)messages.append({"role": "system", "content": "Relevant documents: " + context})response = ""for message in client.chat_completion(messages,max_tokens=max_tokens,2 unmodified linestop_p=top_p,):token = message.choices[0].delta.contentresponse += tokenyield responsedemo = gr.Blocks()with demo:gr.Markdown("🧘♀️ **Dialectical Behaviour Therapy**")gr.Markdown("‼️Disclaimer: This chatbot is based on a DBT exercise book that is publicly available. ""We are not medical practitioners, and the use of this chatbot is at your own responsibility.‼️")chatbot = gr.ChatInterface(respond,examples=[["I feel overwhelmed with work."],["Can you guide me through a quick meditation?"],["How do I stop worrying about things I can't control?"],["What are some DBT skills for managing anxiety?"],["Can you explain mindfulness in DBT?"],["I am interested in DBT excercises"],["I feel restless. Please help me."],["I have destructive thoughts coming to my mind repetatively."]],title='Dialectical Behaviour Therapy Assistant 👩⚕️')if __name__ == "__main__":demo.launch()
app.py+80-37