Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +112 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import time
|
| 3 |
+
from langchain.schema import HumanMessage, SystemMessage, AIMessage
|
| 4 |
+
from langchain.chat_models import ChatOpenAI
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def get_chatmodel_response(question):
|
| 8 |
+
# Retry logic
|
| 9 |
+
max_retries = 3
|
| 10 |
+
retries = 0
|
| 11 |
+
|
| 12 |
+
while retries < max_retries:
|
| 13 |
+
try:
|
| 14 |
+
st.session_state['flowmessages'].append(HumanMessage(content=question))
|
| 15 |
+
answer = chat(st.session_state['flowmessages'])
|
| 16 |
+
st.session_state['flowmessages'].append(AIMessage(content=answer.content))
|
| 17 |
+
return answer.content
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"Error: {e}")
|
| 20 |
+
if "Rate limit" in str(e):
|
| 21 |
+
print(f"Rate limit exceeded. Waiting and retrying...")
|
| 22 |
+
time.sleep(5) # Adjust the waiting time as needed
|
| 23 |
+
retries += 1
|
| 24 |
+
else:
|
| 25 |
+
print("Unhandled exception. Please try again later.")
|
| 26 |
+
break
|
| 27 |
+
|
| 28 |
+
print("Exceeded the maximum number of retries. Please try again later.")
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
# Streamlit app setup
|
| 32 |
+
st.set_page_config(page_title="A - Square")
|
| 33 |
+
st.header("Welcome to A - Square")
|
| 34 |
+
|
| 35 |
+
from dotenv import load_dotenv
|
| 36 |
+
load_dotenv()
|
| 37 |
+
import os
|
| 38 |
+
|
| 39 |
+
# ChatOpenAI class
|
| 40 |
+
chat = ChatOpenAI(temperature=0.3)
|
| 41 |
+
|
| 42 |
+
if 'flowmessages' not in st.session_state:
|
| 43 |
+
st.session_state['flowmessages'] = [
|
| 44 |
+
SystemMessage(content=""" Your Name is "Ambicious Angel"/
|
| 45 |
+
|
| 46 |
+
- You were developed by "Ambicious Angels"
|
| 47 |
+
|
| 48 |
+
You are a story teller application. The name of the application is "A - Square"/
|
| 49 |
+
|
| 50 |
+
As a Story teller application. You will perform the following task./
|
| 51 |
+
1. Introduce yourself. And Interact with the user.
|
| 52 |
+
2. The user will be a student or a kid or an adult.
|
| 53 |
+
3. When the user will enter a topic or askes about a toipc./
|
| 54 |
+
i) First, You should understand the Input topic and know the meaning of it.
|
| 55 |
+
|
| 56 |
+
ii) Then, You should tell him a good story about that topic.
|
| 57 |
+
4. You should tell story in a very proper way.
|
| 58 |
+
5. You should be able to tell story based on any topic.
|
| 59 |
+
6. Also the User is interested in "Design thinking". If the user askes about it. You should be more presice and tell an interesting story.
|
| 60 |
+
7. If the User Input is Inappropriate or if the user input is Obscene or a 18 plus content or offencive. you should tell,/
|
| 61 |
+
"I'm sorry, but I cannot comply with that request. It's important to maintain a respectful and safe environment for all users. Is there something else I can assist you with?"
|
| 62 |
+
8. If you did not understand the Input, Tell the user, "I'm sorry, I didn't quite understand that. Could you please provide more context or rephrase your question?"
|
| 63 |
+
9. After telling the story. Write the Author name as "Ambicious Angels".
|
| 64 |
+
10. Finally, Tell a "Thank you" and exit.
|
| 65 |
+
|
| 66 |
+
""")
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
# Streamlit UI
|
| 70 |
+
with st.form(key='my_form',clear_on_submit=True):
|
| 71 |
+
st.markdown(
|
| 72 |
+
"""
|
| 73 |
+
<style>
|
| 74 |
+
.stTextInput {
|
| 75 |
+
border-radius: 15px;
|
| 76 |
+
padding: 12px;
|
| 77 |
+
margin-top: 10px;
|
| 78 |
+
margin-bottom: 10px;
|
| 79 |
+
box-shadow: 2px 2px 5px #888888;
|
| 80 |
+
border: 1px solid #dddddd;
|
| 81 |
+
font-size: 16px;
|
| 82 |
+
width: 100%; /* Make the input box full width */
|
| 83 |
+
height: 100px; /* Set the height of the input box */
|
| 84 |
+
}
|
| 85 |
+
</style>
|
| 86 |
+
""",
|
| 87 |
+
unsafe_allow_html=True
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
input_question = st.text_input("Type here.", key="input")
|
| 91 |
+
|
| 92 |
+
submit = st.form_submit_button("Ask A-Square")
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# Add a "Clear Chat" button next to the "Submit" button
|
| 96 |
+
clear_chat_button = st.button("New Story", key="clear_button")
|
| 97 |
+
|
| 98 |
+
# If the "Clear Chat" button is clicked
|
| 99 |
+
if clear_chat_button:
|
| 100 |
+
# Clear the entire session and chat
|
| 101 |
+
st.session_state['flowmessages'] = []
|
| 102 |
+
|
| 103 |
+
# If the "Ask" button is clicked
|
| 104 |
+
if submit:
|
| 105 |
+
response = get_chatmodel_response(input_question)
|
| 106 |
+
|
| 107 |
+
if response is not None:
|
| 108 |
+
# st.subheader("Here you go,")
|
| 109 |
+
st.write(response)
|
| 110 |
+
|
| 111 |
+
else:
|
| 112 |
+
st.subheader("Error: Unable to get response. Please try again later.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
openai
|
| 3 |
+
huggingface_hub
|
| 4 |
+
python-dotenv
|
| 5 |
+
streamlit
|