CoolFace
Apppublic

Matteo-CNPPS/CSRD_reports_analysis

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py129 linesDownload Raw Back to root
1import gradio as gr2from huggingface_hub import login3from smolagents import HfApiModel, Tool, CodeAgent4 5import os6import sys7import json8 9if './lib' not in sys.path :10    sys.path.append('./lib')11from ingestion_chroma import retrieve_info_from_db12 13############################################################################################14################################### TOOLS ##################################################15############################################################################################16 17def find_key(data, target_key):18    if isinstance(data, dict):19        for key, value in data.items():20            if key == target_key:21                return value22            else:23                result = find_key(value, target_key)24                if result is not None:25                    return result26    return "Indicator not found"27 28############################################################################################29 30class Chroma_retrieverTool(Tool):31    name = "request"32    description = "Using semantic similarity, retrieve the text from the knowledge base that has the embedding closest to the query."33    inputs = {34        "query": {35            "type": "string",36            "description": "The query to execute must be semantically close to the text to search. Use the affirmative form rather than a question.",37        },38    }39    output_type = "string"40    41    def forward(self, query: str) -> str:42        assert isinstance(query, str), "The request needs to be a string."43        44        query_results = retrieve_info_from_db(query)45        str_result = "\nRetrieval texts : \n" + "".join([f"===== Text {str(i)} =====\n" + query_results['documents'][0][i] for i in range(len(query_results['documents'][0]))])46 47        return str_result48        49############################################################################################50 51class ESRS_info_tool(Tool):52    name = "find_ESRS"53    description = "Find ESRS description to help you to find what indicators the user want"54    inputs = {55        "indicator": {56            "type": "string",57            "description": "The indicator name. return the description of the indicator demanded.",58        },59    }60    output_type = "string"61    62    def forward(self, indicator: str) -> str:63        assert isinstance(indicator, str), "The request needs to be a string."64 65        with open('./data/dico_esrs.json') as json_data:66            dico_esrs = json.load(json_data)67        68        result = find_key(dico_esrs, indicator)69 70        return result71        72############################################################################################73############################################################################################74############################################################################################75 76def respond(message,77    history: list[tuple[str, str]],78    system_message,79    max_tokens,80    temperature,81    top_p,):82    system_prompt_added = """You are an expert in environmental and corporate social responsibility. You must respond to requests using the query function in the document database. 83User's question : """84    agent_output = agent.run(system_prompt_added + message)85    86    yield agent_output87    88############################################################################################89hf_token = os.getenv("HF_TOKEN_all")90login(hf_token)91model = HfApiModel("Qwen/Qwen2.5-Coder-32B-Instruct")92 93retriever_tool = Chroma_retrieverTool()94get_ESRS_info_tool = ESRS_info_tool()95agent = CodeAgent(96    tools=[97            get_ESRS_info_tool,98            retriever_tool,99          ],100    model=model,101    max_steps=10,102    max_print_outputs_length=16000,103    additional_authorized_imports=['pandas', 'matplotlib', 'datetime']104    )105 106 107"""108For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface109"""110demo = gr.ChatInterface(111    respond,112    additional_inputs=[113        gr.Textbox(value="You are a friendly Chatbot.", label="System message"),114        gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),115        gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),116        gr.Slider(117            minimum=0.1,118            maximum=1.0,119            value=0.95,120            step=0.05,121            label="Top-p (nucleus sampling)",122        ),123    ],124)125 126 127if __name__ == "__main__":128    demo.launch()129