Spaces:
Sleeping
Sleeping
import os | |
import time | |
import streamlit as st | |
from langchain.llms import openai | |
def get_openai_response(question): | |
try: | |
api_key = os.environ["OPENAI_API_KEY"] | |
llm = openai.OpenAI( | |
model_name="text-davinci-003", temperature=0.5, openai_api_key=api_key | |
) | |
with st.spinner("Generating response..."): | |
response = llm(question) | |
return response | |
except Exception as e: | |
st.error(f"Error: {e}") | |
return None | |
st.set_page_config(page_title="Enhanced Q&A Demo") | |
st.header("Xoxo-Langchain App") | |
input_text = st.text_input("Ask me anything:", key="input") | |
submit_button = st.button("Get Answer") | |
response_history = st.empty() | |
if submit_button: | |
with st.spinner("Processing..."): | |
response = get_openai_response(input_text) | |
if response: | |
st.subheader("The answer is:") | |
st.write(response) | |
# Add response to history | |
response_history.code(f""" | |
{input_text} | |
**Answer:** {response} | |
--- | |
""") | |
else: | |
st.error("Something went wrong. Please try again later.") | |