Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,27 +3,31 @@ import openai
|
|
| 3 |
|
| 4 |
def main():
|
| 5 |
st.title("AI Chatbot")
|
| 6 |
-
|
| 7 |
# Input field for OpenAI API Key
|
| 8 |
api_key = st.text_input("Enter your OpenAI API Key", type="password")
|
| 9 |
-
|
| 10 |
if api_key:
|
| 11 |
openai.api_key = api_key
|
| 12 |
-
|
| 13 |
user_input = st.text_area("Ask me anything:")
|
| 14 |
-
|
| 15 |
if st.button("Generate Response"):
|
| 16 |
-
if user_input.strip()
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
else:
|
| 25 |
st.warning("Please enter a question.")
|
| 26 |
else:
|
| 27 |
st.warning("Please enter your OpenAI API Key to proceed.")
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
| 3 |
|
| 4 |
def main():
|
| 5 |
st.title("AI Chatbot")
|
| 6 |
+
|
| 7 |
# Input field for OpenAI API Key
|
| 8 |
api_key = st.text_input("Enter your OpenAI API Key", type="password")
|
| 9 |
+
|
| 10 |
if api_key:
|
| 11 |
openai.api_key = api_key
|
|
|
|
| 12 |
user_input = st.text_area("Ask me anything:")
|
| 13 |
+
|
| 14 |
if st.button("Generate Response"):
|
| 15 |
+
if user_input.strip():
|
| 16 |
+
try:
|
| 17 |
+
response = openai.ChatCompletion.create(
|
| 18 |
+
model="gpt-3.5-turbo",
|
| 19 |
+
messages=[{"role": "user", "content": user_input}]
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
st.toast("Generating response...") # Show a popup message
|
| 23 |
+
st.subheader("Response:")
|
| 24 |
+
st.write(response["choices"][0]["message"]["content"])
|
| 25 |
+
except Exception as e:
|
| 26 |
+
st.error(f"Error: {str(e)}") # Show error popup
|
| 27 |
else:
|
| 28 |
st.warning("Please enter a question.")
|
| 29 |
else:
|
| 30 |
st.warning("Please enter your OpenAI API Key to proceed.")
|
| 31 |
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
main()
|