Spaces:
Sleeping
Sleeping
Delete Tryapp.py
Browse files
Tryapp.py
DELETED
|
@@ -1,173 +0,0 @@
|
|
| 1 |
-
# from typing import Any, List, Tuple
|
| 2 |
-
# import gradio as gr
|
| 3 |
-
# from langchain_openai import OpenAIEmbeddings
|
| 4 |
-
# from langchain_community.vectorstores import Chroma
|
| 5 |
-
# from langchain.chains import ConversationalRetrievalChain
|
| 6 |
-
# from langchain_openai import ChatOpenAI
|
| 7 |
-
# from langchain_community.document_loaders import PyMuPDFLoader
|
| 8 |
-
# import fitz
|
| 9 |
-
# from PIL import Image
|
| 10 |
-
# import os
|
| 11 |
-
# import openai
|
| 12 |
-
|
| 13 |
-
# # MyApp class to handle the processes
|
| 14 |
-
# class MyApp:
|
| 15 |
-
# def __init__(self) -> None:
|
| 16 |
-
# self.OPENAI_API_KEY: str = None # Initialize with None
|
| 17 |
-
# self.chain = None
|
| 18 |
-
# self.chat_history: list = []
|
| 19 |
-
# self.documents = None
|
| 20 |
-
# self.file_name = None
|
| 21 |
-
|
| 22 |
-
# def set_api_key(self, api_key: str):
|
| 23 |
-
# self.OPENAI_API_KEY = api_key
|
| 24 |
-
# openai.api_key = api_key
|
| 25 |
-
|
| 26 |
-
# def process_file(self, file) -> Image.Image:
|
| 27 |
-
# loader = PyMuPDFLoader(file.name)
|
| 28 |
-
# self.documents = loader.load()
|
| 29 |
-
# self.file_name = os.path.basename(file.name)
|
| 30 |
-
# doc = fitz.open(file.name)
|
| 31 |
-
# page = doc[0]
|
| 32 |
-
# pix = page.get_pixmap(dpi=150)
|
| 33 |
-
# image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 34 |
-
# return image
|
| 35 |
-
|
| 36 |
-
# def build_chain(self, file) -> str:
|
| 37 |
-
# embeddings = OpenAIEmbeddings(openai_api_key=self.OPENAI_API_KEY)
|
| 38 |
-
# pdfsearch = Chroma.from_documents(
|
| 39 |
-
# self.documents,
|
| 40 |
-
# embeddings,
|
| 41 |
-
# collection_name=self.file_name,
|
| 42 |
-
# )
|
| 43 |
-
# self.chain = ConversationalRetrievalChain.from_llm(
|
| 44 |
-
# ChatOpenAI(temperature=0.0, openai_api_key=self.OPENAI_API_KEY),
|
| 45 |
-
# retriever=pdfsearch.as_retriever(search_kwargs={"k": 1}),
|
| 46 |
-
# return_source_documents=True,
|
| 47 |
-
# )
|
| 48 |
-
# return "Vector database built successfully!"
|
| 49 |
-
|
| 50 |
-
# # Function to add text to chat history
|
| 51 |
-
# def add_text(history: List[Tuple[str, str]], text: str) -> List[Tuple[str, str]]:
|
| 52 |
-
# if not text:
|
| 53 |
-
# raise gr.Error("Enter text")
|
| 54 |
-
# history.append((text, ""))
|
| 55 |
-
# return history
|
| 56 |
-
|
| 57 |
-
# # Function to get response from the model
|
| 58 |
-
# def get_response(history, query):
|
| 59 |
-
# if app.chain is None:
|
| 60 |
-
# raise gr.Error("The chain has not been built yet. Please ensure the vector database is built before querying.")
|
| 61 |
-
|
| 62 |
-
# try:
|
| 63 |
-
# result = app.chain.invoke(
|
| 64 |
-
# {"question": query, "chat_history": app.chat_history}
|
| 65 |
-
# )
|
| 66 |
-
# app.chat_history.append((query, result["answer"]))
|
| 67 |
-
# source_docs = result["source_documents"]
|
| 68 |
-
# source_texts = []
|
| 69 |
-
# for doc in source_docs:
|
| 70 |
-
# source_texts.append(f"Page {doc.metadata['page'] + 1}: {doc.page_content}")
|
| 71 |
-
# source_texts_str = "\n\n".join(source_texts)
|
| 72 |
-
# history[-1] = (history[-1][0], result["answer"])
|
| 73 |
-
# return history, source_texts_str
|
| 74 |
-
# except Exception as e:
|
| 75 |
-
# app.chat_history.append((query, "I have no information about it. Feed me knowledge, please!"))
|
| 76 |
-
# return history, f"I have no information about it. Feed me knowledge, please! Error: {str(e)}"
|
| 77 |
-
|
| 78 |
-
# # Function to refresh chat
|
| 79 |
-
# def refresh_chat():
|
| 80 |
-
# app.chat_history = []
|
| 81 |
-
# return []
|
| 82 |
-
|
| 83 |
-
# app = MyApp()
|
| 84 |
-
|
| 85 |
-
# # Function to set API key
|
| 86 |
-
# def set_api_key(api_key):
|
| 87 |
-
# app.set_api_key(api_key)
|
| 88 |
-
# # Pre-process the saved PDF file after setting the API key
|
| 89 |
-
# saved_file_path = "THEDIA1.pdf"
|
| 90 |
-
# with open(saved_file_path, 'rb') as saved_file:
|
| 91 |
-
# app.process_file(saved_file)
|
| 92 |
-
# app.build_chain(saved_file)
|
| 93 |
-
# return f"API Key set to {api_key[:4]}...{api_key[-4:]} and vector database built successfully!"
|
| 94 |
-
|
| 95 |
-
# # List of determined questions
|
| 96 |
-
# questions = [
|
| 97 |
-
# "What is the primary goal of Dialectical Behaviour Therapy?",
|
| 98 |
-
# "How can mindfulness help in managing emotions?",
|
| 99 |
-
# "What are some techniques to handle distressing situations?",
|
| 100 |
-
# "Can you explain the concept of radical acceptance?",
|
| 101 |
-
# "How does DBT differ from other types of therapy?",
|
| 102 |
-
# "What are the four modules of DBT?",
|
| 103 |
-
# "How can DBT skills be applied in daily life?",
|
| 104 |
-
# "What is the importance of emotional regulation in DBT?",
|
| 105 |
-
# "How does DBT address interpersonal effectiveness?",
|
| 106 |
-
# "What are some common myths about DBT?",
|
| 107 |
-
# "How can one practice distress tolerance skills?",
|
| 108 |
-
# "What role does validation play in DBT?",
|
| 109 |
-
# "How does DBT incorporate cognitive-behavioral techniques?",
|
| 110 |
-
# "What are the stages of DBT treatment?",
|
| 111 |
-
# "How can one use DBT skills to improve self-awareness?"
|
| 112 |
-
# ]
|
| 113 |
-
|
| 114 |
-
# # Gradio interface
|
| 115 |
-
# with gr.Blocks() as demo:
|
| 116 |
-
# gr.Markdown("🧘♀️ **Dialectical Behaviour Therapy**")
|
| 117 |
-
# gr.Markdown(
|
| 118 |
-
# "Disclaimer: This chatbot is based on a DBT exercise book that is publicly available. "
|
| 119 |
-
# "We are not medical practitioners, and the use of this chatbot is at your own responsibility."
|
| 120 |
-
# )
|
| 121 |
-
|
| 122 |
-
# api_key_input = gr.Textbox(label="OpenAI API Key", type="password", placeholder="Enter your OpenAI API Key")
|
| 123 |
-
# api_key_btn = gr.Button("Set API Key")
|
| 124 |
-
# api_key_status = gr.Textbox(value="API Key status", interactive=False)
|
| 125 |
-
|
| 126 |
-
# api_key_btn.click(
|
| 127 |
-
# fn=set_api_key,
|
| 128 |
-
# inputs=[api_key_input],
|
| 129 |
-
# outputs=[api_key_status]
|
| 130 |
-
# )
|
| 131 |
-
|
| 132 |
-
# chatbot_current = gr.Chatbot(elem_id="chatbot_current")
|
| 133 |
-
# txt_current = gr.Textbox(
|
| 134 |
-
# show_label=False,
|
| 135 |
-
# placeholder="Enter text and press submit",
|
| 136 |
-
# scale=2
|
| 137 |
-
# )
|
| 138 |
-
# submit_btn_current = gr.Button("Submit", scale=1)
|
| 139 |
-
# refresh_btn_current = gr.Button("Refresh Chat", scale=1)
|
| 140 |
-
# source_texts_output_current = gr.Textbox(label="Source Texts", interactive=False)
|
| 141 |
-
|
| 142 |
-
# submit_btn_current.click(
|
| 143 |
-
# fn=add_text,
|
| 144 |
-
# inputs=[chatbot_current, txt_current],
|
| 145 |
-
# outputs=[chatbot_current],
|
| 146 |
-
# queue=False,
|
| 147 |
-
# ).success(
|
| 148 |
-
# fn=get_response, inputs=[chatbot_current, txt_current], outputs=[chatbot_current, source_texts_output_current]
|
| 149 |
-
# )
|
| 150 |
-
|
| 151 |
-
# refresh_btn_current.click(
|
| 152 |
-
# fn=refresh_chat,
|
| 153 |
-
# inputs=[],
|
| 154 |
-
# outputs=[chatbot_current],
|
| 155 |
-
# )
|
| 156 |
-
|
| 157 |
-
# question_dropdown = gr.Dropdown(
|
| 158 |
-
# label="Select an example question",
|
| 159 |
-
# choices=questions
|
| 160 |
-
# )
|
| 161 |
-
# question_submit_btn = gr.Button("Submit Question")
|
| 162 |
-
|
| 163 |
-
# question_submit_btn.click(
|
| 164 |
-
# fn=add_text,
|
| 165 |
-
# inputs=[chatbot_current, question_dropdown],
|
| 166 |
-
# outputs=[chatbot_current],
|
| 167 |
-
# queue=False,
|
| 168 |
-
# ).success(
|
| 169 |
-
# fn=get_response, inputs=[chatbot_current, question_dropdown], outputs=[chatbot_current, source_texts_output_current]
|
| 170 |
-
# )
|
| 171 |
-
|
| 172 |
-
# demo.queue()
|
| 173 |
-
# demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|