CoolFace
Apppublic

dkedar7/embedchain-fastdash

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
app.py118 linesDownload Raw Back to root
1import os2from fast_dash import FastDash, Fastify, dcc, dmc3 4from embedchain import App5from embedchain.config import QueryConfig6from string import Template7 8# Define app configurations9PROMPT = Template(10    """Use the given context to answer the question at the end.11If you don't know the answer, say so, but don't try to make one up.12At the end of the answer, also give the sources as a bulleted list.13Display the answer as markdown text.14 15Context: $context16 17Query: $query18 19Answer:"""20)21query_config = QueryConfig(22    template=PROMPT, number_documents=5, max_tokens=2000, model="gpt-4"23)24 25# Define components26openai_api_key_component = dmc.PasswordInput(27    placeholder="API Key",28    description="Get yours at https://platform.openai.com/account/api-keys",29    required=True,30)31 32web_page_urls_component = dmc.MultiSelect(33    description="Include all the reference web URLs",34    placeholder="Enter URLs separated by commas",35    searchable=True,36    creatable=True,37)38 39text_component = dmc.Textarea(40    placeholder="Write your query here",41    autosize=True,42    minRows=4,43    description="Any additional information that could be useful",44)45 46query_component = dmc.Textarea(47    placeholder="Write your query here",48    autosize=True,49    minRows=4,50    required=True,51    description="Write your query here",52)53 54answer_component = dcc.Markdown(55    style={"text-align": "left", "padding": "1%"}, link_target="_blank"56)57 58 59def explore_your_knowledge_base(60    openai_api_key: openai_api_key_component,61    web_page_urls: web_page_urls_component,62    youtube_urls: web_page_urls_component,63    pdf_urls: web_page_urls_component,64    text: text_component,65    query: text_component,66) -> answer_component:67    """68    Input your sources and let GPT4 find answers. Built with Fast Dash.69    This app uses embedchain.ai, which abstracts the entire process of loading and chunking datasets, creating embeddings, and storing them in a vector database.70    Embedchain itself uses Langchain and OpenAI's ChatGPT API.71    """72    answer_suffix = ""73 74    if not openai_api_key:75        return "Did you forget adding your OpenAI API key? If you don't have one, you can get it [here](https://platform.openai.com/account/api-keys)."76 77    if not query:78        return "Did you forget writing your query in the query box?"79 80    os.environ["OPENAI_API_KEY"] = openai_api_key81    app = App()82 83    try:84        if web_page_urls:85            [app.add("web_page", url) for url in web_page_urls]86 87        if youtube_urls:88            [app.add("youtube_video", url) for url in youtube_urls]89 90        if pdf_urls:91            [app.add("pdf_file", url) for url in pdf_urls]92 93        if text:94            app.add_local("text", text)95 96    except Exception as e:97        print(str(e))98        answer_suffix = "I couldn't analyze some sources. If you think this is an error, please try again later or make a suggestion [here](https://github.com/dkedar7/embedchain-fastdash/issues)."99 100    answer = app.query(query, query_config)101    answer = f"""{answer}102 103    {answer_suffix}104    """105 106    return answer107 108 109# Build app (this is all it takes!). Fast Dash understands what it needs to do.110app = FastDash(111    explore_your_knowledge_base,112    github_url="https://github.com/dkedar7/embedchain-fastdash",113)114server = app.server115 116if __name__ == "__main__":117    app.run()118