CoolFace
Apppublic

bacancydataprophets/AMC_Bot

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
app.py112 linesDownload Raw Back to root
1import streamlit as st2from app_config import SYSTEM_PROMPT3from langchain_groq import ChatGroq4from dotenv import load_dotenv5from pathlib import Path6import os7import session_manager8 9from langchain_community.utilities import GoogleSerperAPIWrapper10env_path = Path('.') / '.env'11load_dotenv(dotenv_path=env_path)12 13 14st.markdown(15    """16<style>17    .st-emotion-cache-janbn0 {18        flex-direction: row-reverse;19        text-align: right;20    }21    .st-emotion-cache-1ec2a3d{22        display: none;23    }24</style>25""",26    unsafe_allow_html=True,27)28 29# Intialize chat history30print("SYSTEM MESSAGE")31if "messages" not in st.session_state:32    st.session_state.messages = [{"role": "system", "content": SYSTEM_PROMPT}]33 34print("SYSTEM MODEL")35if "llm" not in st.session_state:36    st.session_state.llm = ChatGroq(37        model="llama-3.3-70b-versatile",38        temperature=0,39        max_tokens=None,40        timeout=None,41        max_retries=2,42        api_key=str(os.getenv('GROQ_API'))43    )44 45if "search_tool" not in st.session_state:46    st.session_state.search_tool = GoogleSerperAPIWrapper(47        serper_api_key=str(os.getenv('SERPER_API')))48 49 50def get_answer(query):51    new_search_query = st.session_state.llm.invoke(52        f"Convert below query to english for Ahmedabad Municipal Corporation (AMC) You just need to give translated query. Don't add any additional details.\n Query: {query}").content53    search_result = st.session_state.search_tool.run(54        f"{new_search_query} site:https://ahmedabadcity.gov.in/")55 56    system_prompt = """You are a helpful assistance for The Ahmedabad Municipal Corporation (AMC). which asnwer user query from given context only. Output language should be as same as language of `original_query_from_user`.57  context: {context}58  original_query_from_user: {original_query}59  query: {query}"""60 61    return st.session_state.llm.invoke(system_prompt.format(context=search_result, query=new_search_query, original_query=query)).content62 63 64session_manager.set_session_state(st.session_state)65 66print("container")67# Display chat messages from history68st.markdown("<h1 style='text-align: center;'>AMC Bot</h1>", unsafe_allow_html=True)69container = st.container(height=700)70for message in st.session_state.messages:71    if message["role"] != "system":72        with container.chat_message(message["role"]):73            if message['type'] == "table":74                st.dataframe(message['content'].set_index(75                    message['content'].columns[0]))76            elif message['type'] == "html":77                st.markdown(message['content'], unsafe_allow_html=True)78            else:79                st.write(message["content"])80 81# When user gives input82if prompt := st.chat_input("Enter your query here... "):83    with container.chat_message("user"):84        st.write(prompt)85    st.session_state.messages.append(86        {"role": "user", "content": prompt, "type": "string"})87    st.session_state.last_query = prompt88 89    with container.chat_message("assistant"):90        current_conversation = """"""91 92        # if st.session_state.next_agent != "general_agent" and st.session_state.next_agent in st.session_state.agent_history:93        # for message in st.session_state.messages:94        #     if message['role'] == 'user':95        #         current_conversation += f"""user: {message['content']}\n"""96        #     if message['role'] == 'assistant':97        #         current_conversation += f"""ai: {message['content']}\n"""98 99        # current_conversation += f"""user: {prompt}\n"""100 101        print("****************************************** Messages ******************************************")102        print("messages", current_conversation)103        print()104        print()105        response = get_answer(prompt)106        print("******************************************************** Response ********************************************************")107        print("MY RESPONSE IS:", response)108 109        st.write(response)110        st.session_state.messages.append(111            {"role": "assistant", "content": response, "type": "string"})112