Spaces:
Sleeping
Sleeping
Download app.py from vince1313/TestChatBot: direct link, hf CLI and curl.
- Browser
- Download file 1.7 kB
-
https://huggingface.co/spaces/vince1313/TestChatBot/resolve/main/app.py
- Command line
-
hf download hf://spaces/vince1313/TestChatBot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/vince1313/TestChatBot/resolve/main/app.py
1.7 kB
| import os | |
| # Streamlit link: https://streamlit.io/ | |
| # Streamlit Docs link: https://docs.streamlit.io/ | |
| import streamlit as st | |
| from openai import OpenAI | |
| # Adding dotenv package: https://pypi.org/project/python-dotenv/ | |
| # pip install python-dotenv | |
| from dotenv import load_dotenv | |
| # Load environment variables | |
| load_dotenv() | |
| # Set the OpenAI API key from .env file | |
| client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | |
| # Settings for the OpenAI model | |
| settings = { | |
| "model": "gpt-4o", | |
| "temperature": 0, | |
| "max_tokens": 100, | |
| "top_p": 1, | |
| "frequency_penalty": 0, | |
| "presence_penalty": 0 | |
| } | |
| def get_openai_response(user_message): | |
| response = client.chat.completions.create( | |
| model=settings["model"], | |
| messages=[ | |
| { | |
| "role": "system", | |
| # "content": "You are a helpful assistant in real-estate." | |
| "content": "You are a helpful bot, you always provide programming solutions in python in German, English or Spanish", | |
| }, | |
| { | |
| "role": "user", | |
| "content": user_message, | |
| }, | |
| ], | |
| temperature=settings["temperature"]) | |
| return response.choices[0].message.content | |
| # Streamlit App | |
| st.title("Chat with TechMaloo ChatBot") # Defining the app title in streamlit | |
| user_message = st.text_input("Enter your message:") | |
| if st.button("Send"): | |
| if user_message: | |
| with st.spinner("Generating response..."): | |
| try: | |
| response = get_openai_response(user_message) | |
| st.write(response) | |
| except Exception as e: | |
| st.write(f"Error: {e}") | |
| else: | |
| st.write("Please enter a message.") | |