Spaces:
Build error
Build error
Download app.py from Yapp99/LLM_Endpoint_Demo: direct link, hf CLI and curl.
- Browser
- Download file 1.23 kB
-
https://huggingface.co/spaces/Yapp99/LLM_Endpoint_Demo/resolve/main/app.py
- Command line
-
hf download hf://spaces/Yapp99/LLM_Endpoint_Demo/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Yapp99/LLM_Endpoint_Demo/resolve/main/app.py
1.23 kB
| import streamlit as st | |
| import requests | |
| from init import init | |
| import os | |
| try: | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| except ModuleNotFoundError: | |
| pass | |
| init() | |
| url = os.getenv("ENDPOINT_URL") | |
| st.set_page_config(page_title="Chat") | |
| def send_chat_request(): | |
| data = dict( | |
| chat_history=[ | |
| { | |
| "role": "system", | |
| "content": st.session_state.system_prompt, | |
| }, | |
| *st.session_state.messages, | |
| ], | |
| **st.session_state.params, | |
| ) | |
| with requests.Session() as session: | |
| response = session.post(f"{url}/chat", json=data) | |
| return response.json()["response"] | |
| def reset(): | |
| st.session_state.messages = [] | |
| st.button("Reset", on_click=reset) | |
| for message in st.session_state.messages: | |
| with st.chat_message(message["role"]): | |
| st.markdown(message["content"]) | |
| if prompt := st.chat_input("What is up?"): | |
| st.chat_message("user").markdown(prompt) | |
| st.session_state.messages.append({"role": "user", "content": prompt}) | |
| response = send_chat_request() | |
| with st.chat_message("assistant"): | |
| st.markdown(response) | |
| st.session_state.messages.append({"role": "assistant", "content": response}) | |