CoolFace
Apppublic

ahmfzui/chatbot_edm

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
edm.py46 linesDownload Raw Back to root
1import os2from huggingface_hub import InferenceClient3import streamlit as st4 5# Access your Hugging Face API token from the environment variable6api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")7if api_token is None:8    st.error("Hugging Face API token is not set.")9else:10    st.title("Tanya Gizi!")11 12    # Initialize chat history if not already present13    if 'messages' not in st.session_state:14        st.session_state.messages = []15 16    # Display chat history17    for message in st.session_state.messages:18        st.chat_message(message['role']).markdown(message['content'])19 20    # Input area for the user21    prompt = st.chat_input('Masukan pertanyaanmu di sini!')22 23    # Process user input24    if prompt:25        st.chat_message('user').markdown(prompt)26        st.session_state.messages.append({'role': 'user', 'content': prompt})27 28        # Generate a response using InferenceClient29        client = InferenceClient(30            model="mistralai/Mistral-Large-Instruct-2407",31            token=api_token32        )33 34        # Generating response35        response = client.chat_completion(36            messages=[{"role": "user", "content": prompt}],37            max_tokens=100,38            stream=False  # Disable streaming as it's not supported39        )40 41        response_text = response['choices'][0]['message']['content']42 43        # Display and store the assistant's response44        st.chat_message('assistant').markdown(response_text)45        st.session_state.messages.append({'role': 'assistant', 'content': response_text})46