CoolFace
Apppublic

Kaludi/OpenAI-Chatbot_App

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
4likes
app.py66 linesDownload Raw Back to root
1import streamlit as st2import requests3import json4 5st.title("OpenAI Chatbot Interface")6st.write("Interact with OpenAI's GPT-3 models in real-time using your OpenAI API. Choose from a selection of their best models, set the temperature and max tokens, and start a conversation. Delete the conversation at any time to start fresh.")7 8if "history" not in st.session_state:9    st.session_state.history = []10 11st.sidebar.markdown("## Configuration")12KEY = st.sidebar.text_input("Enter Your OpenAI API Key", placeholder="API Key", value="")13models = ['text-davinci-003', 'text-curie-001', 'text-babbage-001', 'text-ada-001']14model = st.sidebar.selectbox("Select a model", models, index=0)15 16temperature = st.sidebar.slider("Temperature", 0.0, 1.0, 0.7)17max_tokens = st.sidebar.slider("Max Tokens", 0, 4000, 1786)18 19if st.sidebar.button("Delete Conversation"):20    st.session_state.history = []21st.sidebar.markdown("## GPT-3")22st.sidebar.markdown("OpenAI's GPT-3 models can understand and generate natural language. They offer four main models with different levels of power suitable for different tasks. Davinci is the most capable model, and Ada is the fastest.")23st.sidebar.markdown("text-davinci-003 | 4,000 max tokens")24st.sidebar.markdown("text-curie-001 | 2,048 max tokens")25st.sidebar.markdown("text-babbage-001 | 2,048 max tokens")26st.sidebar.markdown("text-ada-001 | 2,048 max tokens")27 28def generate_answer(prompt):29    API_KEY = KEY30    API_URL = "https://api.openai.com/v1/completions"31    headers = {32        'Content-Type': 'application/json',33        'Authorization': 'Bearer ' + API_KEY34    }35    previous_messages = [chat['message'] for chat in st.session_state.history if not chat['is_user']]36    previous_messages_text = '\n'.join(previous_messages)37    full_prompt = previous_messages_text + '\n' + prompt if previous_messages_text else prompt38    data = {39        "model": model,40        "prompt": full_prompt,41        "temperature": temperature,42        "max_tokens": max_tokens43    }44    if not API_KEY:45        st.warning("Please input your API key")46        return47    response = requests.post(API_URL, headers=headers, data=json.dumps(data))48    result = response.json()49    if 'choices' in result:50        message_bot = result['choices'][0]['text'].strip()51        st.session_state.history.append({"message": prompt, "is_user": True})52        st.session_state.history.append({"message": message_bot, "is_user": False})53    else:54        st.error("An error occurred while processing the API response. If using a model other than text-davinci-003, then lower the Max Tokens.")55 56prompt = st.text_input("Prompt", placeholder="Prompt Here", value="")57if st.button("Submit"):58    generate_answer(prompt)59    with st.spinner("Waiting for the response from the bot..."):60        for chat in st.session_state.history:61            if chat['is_user']:62                st.markdown("<img src='https://i.ibb.co/zVSbGvb/585e4beacb11b227491c3399.png' width='50' height='50' style='float:right;'>", unsafe_allow_html=True)63                st.markdown(f"<div style='float:right; padding:10px; background-color: #2E2E2E; border-radius:10px; margin:10px;'>{chat['message']}</div>", unsafe_allow_html=True)64            else:65                st.markdown("<img src='https://i.ibb.co/LZFvDND/5841c0bda6515b1e0ad75a9e-1.png' width='50' height='50' style='float:left;'>", unsafe_allow_html=True)66                st.markdown(f"<div style='float:left; padding:10px; background-color: #2E2E2E; border-radius:10px; margin:10px;'>{chat['message']}</div>", unsafe_allow_html=True)