Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
# Title of the app
|
| 4 |
+
st.title("Enhanced Q&A Chatbot With OpenAI")
|
| 5 |
+
|
| 6 |
+
# Sidebar for settings
|
| 7 |
+
st.sidebar.title("Settings")
|
| 8 |
+
api_key = st.sidebar.text_input("Enter your OpenAI API Key:", type="password")
|
| 9 |
+
|
| 10 |
+
# Select the OpenAI model
|
| 11 |
+
engine = st.sidebar.selectbox("Select OpenAI model", ["gpt-4", "gpt-4-turbo", "gpt-4o"])
|
| 12 |
+
|
| 13 |
+
# Adjust response parameters
|
| 14 |
+
temperature = st.sidebar.slider("Temperature", min_value=0.0, max_value=1.0, value=0.7)
|
| 15 |
+
max_tokens = st.sidebar.slider("Max Tokens", min_value=50, max_value=300, value=150)
|
| 16 |
+
|
| 17 |
+
# Main interface for user input
|
| 18 |
+
st.write("Go ahead and ask any question:")
|
| 19 |
+
user_input = st.text_input("You:")
|
| 20 |
+
|
| 21 |
+
# Function to generate responses
|
| 22 |
+
def generate_response(user_input, api_key, engine, temperature, max_tokens):
|
| 23 |
+
import openai
|
| 24 |
+
openai.api_key = api_key
|
| 25 |
+
response = openai.Completion.create(
|
| 26 |
+
engine=engine,
|
| 27 |
+
prompt=user_input,
|
| 28 |
+
temperature=temperature,
|
| 29 |
+
max_tokens=max_tokens,
|
| 30 |
+
)
|
| 31 |
+
return response.choices[0].text.strip()
|
| 32 |
+
|
| 33 |
+
# Display response
|
| 34 |
+
if user_input and api_key:
|
| 35 |
+
try:
|
| 36 |
+
response = generate_response(user_input, api_key, engine, temperature, max_tokens)
|
| 37 |
+
st.write(response)
|
| 38 |
+
except Exception as e:
|
| 39 |
+
st.error(f"Error: {e}")
|
| 40 |
+
elif user_input:
|
| 41 |
+
st.warning("Please enter the OpenAI API Key in the sidebar.")
|
| 42 |
+
else:
|
| 43 |
+
st.write("Please provide user input.")
|