Spaces:
Sleeping
Sleeping
Download app.py from Pezh/Chat: direct link, hf CLI and curl.
- Browser
- Download file 1.52 kB
-
https://huggingface.co/spaces/Pezh/Chat/resolve/main/app.py
- Command line
-
hf download hf://spaces/Pezh/Chat/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Pezh/Chat/resolve/main/app.py
1.52 kB
| import streamlit as st | |
| import openai | |
| from streamlit_chat import message | |
| openai.api_key = "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" | |
| def api_calling(prompt): | |
| completions = openai.Completion.create( | |
| engine="text-davinci-003", | |
| prompt=prompt, | |
| max_tokens=1024, | |
| n=1, | |
| stop=None, | |
| temperature=0.5, | |
| ) | |
| message = completions.choices[0].text | |
| return message | |
| st.title("ChatGPT ChatBot With Streamlit and OpenAI") | |
| if 'user_input' not in st.session_state: | |
| st.session_state['user_input'] = [] | |
| if 'openai_response' not in st.session_state: | |
| st.session_state['openai_response'] = [] | |
| def get_text(): | |
| input_text = st.text_input("write here", key="input") | |
| return input_text | |
| user_input = get_text() | |
| if user_input: | |
| output = api_calling(user_input) | |
| output = output.lstrip("\n") | |
| # Store the output | |
| st.session_state.openai_response.append(user_input) | |
| st.session_state.user_input.append(output) | |
| message_history = st.empty() | |
| if st.session_state['user_input']: | |
| for i in range(len(st.session_state['user_input']) - 1, -1, -1): | |
| # This function displays user input | |
| message(st.session_state["user_input"][i], | |
| key=str(i),avatar_style="icons") | |
| # This function displays OpenAI response | |
| message(st.session_state['openai_response'][i], | |
| avatar_style="miniavs",is_user=True, | |
| key=str(i) + 'data_by_user') |