Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from smolagents import CodeAgent, HfApiModel, tool
|
| 5 |
+
from huggingface_hub import InferenceClient
|
| 6 |
+
|
| 7 |
+
# Retrieve Hugging Face token
|
| 8 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 9 |
+
if not hf_token:
|
| 10 |
+
raise ValueError("Hugging Face token not found. Please set it in the environment or Streamlit secrets.")
|
| 11 |
+
|
| 12 |
+
# Initialize the Hugging Face Inference client
|
| 13 |
+
client = InferenceClient(token=hf_token)
|
| 14 |
+
|
| 15 |
+
# Custom tools for SmolAgents
|
| 16 |
+
@tool
|
| 17 |
+
def search_harry_potter_lore(query: str) -> str:
|
| 18 |
+
"""Search for Harry Potter-related lore or facts from external sources.
|
| 19 |
+
Args:
|
| 20 |
+
query: A specific question or topic about Harry Potter lore.
|
| 21 |
+
Returns:
|
| 22 |
+
A concise and informative response based on the query.
|
| 23 |
+
"""
|
| 24 |
+
# Placeholder for a real implementation using an API or web scraping
|
| 25 |
+
# In production, you could integrate with fandom APIs or use libraries like BeautifulSoup.
|
| 26 |
+
if "horcrux" in query.lower():
|
| 27 |
+
return "A Horcrux is a dark magical object created by splitting one's soul. Tom Riddle created seven of them."
|
| 28 |
+
elif "chamber of secrets" in query.lower():
|
| 29 |
+
return "The Chamber of Secrets was built by Salazar Slytherin to purge the school of those he deemed unworthy to study magic."
|
| 30 |
+
else:
|
| 31 |
+
return "The information you're looking for is not available in my current resources."
|
| 32 |
+
|
| 33 |
+
# Initialize the SmolAgent model
|
| 34 |
+
model = HfApiModel(model_id="meta-llama/Llama-3.2-3B-Instruct", token=hf_token)
|
| 35 |
+
|
| 36 |
+
# Create the agent
|
| 37 |
+
agent = CodeAgent(tools=[search_harry_potter_lore], model=model)
|
| 38 |
+
|
| 39 |
+
def apply_custom_styles():
|
| 40 |
+
st.markdown(
|
| 41 |
+
"""
|
| 42 |
+
<style>
|
| 43 |
+
.stApp {
|
| 44 |
+
background-color: #fefbe9;
|
| 45 |
+
background-image: radial-gradient(circle, rgba(250,240,200,1) 0%, rgba(245,235,185,1) 100%);
|
| 46 |
+
font-family: 'Dancing Script', cursive;
|
| 47 |
+
display: flex;
|
| 48 |
+
justify-content: center;
|
| 49 |
+
align-items: center;
|
| 50 |
+
height: 100vh;
|
| 51 |
+
}
|
| 52 |
+
textarea {
|
| 53 |
+
width: 100%;
|
| 54 |
+
height: 100%;
|
| 55 |
+
font-size: 20px;
|
| 56 |
+
font-family: 'Dancing Script', cursive;
|
| 57 |
+
color: #000000;
|
| 58 |
+
background-color: rgba(255, 255, 255, 0.8);
|
| 59 |
+
border: none;
|
| 60 |
+
box-shadow: none;
|
| 61 |
+
outline: none;
|
| 62 |
+
resize: none;
|
| 63 |
+
padding: 10px;
|
| 64 |
+
}
|
| 65 |
+
textarea::placeholder {
|
| 66 |
+
color: rgba(0, 0, 0, 0.5);
|
| 67 |
+
}
|
| 68 |
+
textarea:focus {
|
| 69 |
+
outline: none;
|
| 70 |
+
border: 2px solid rgba(0, 0, 0, 0.3);
|
| 71 |
+
}
|
| 72 |
+
.response {
|
| 73 |
+
font-size: 22px;
|
| 74 |
+
color: #000000;
|
| 75 |
+
text-align: center;
|
| 76 |
+
font-family: 'Dancing Script', cursive;
|
| 77 |
+
padding: 20px;
|
| 78 |
+
animation: fade-in 2s ease-in;
|
| 79 |
+
position: relative;
|
| 80 |
+
}
|
| 81 |
+
@keyframes fade-in {
|
| 82 |
+
from { opacity: 0; }
|
| 83 |
+
to { opacity: 1; }
|
| 84 |
+
}
|
| 85 |
+
</style>
|
| 86 |
+
""",
|
| 87 |
+
unsafe_allow_html=True
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
def main():
|
| 91 |
+
apply_custom_styles()
|
| 92 |
+
|
| 93 |
+
# Initialize session state variables if not set
|
| 94 |
+
if "state_reset" not in st.session_state:
|
| 95 |
+
st.session_state["state_reset"] = True
|
| 96 |
+
|
| 97 |
+
if st.session_state["state_reset"]:
|
| 98 |
+
st.session_state["user_input"] = ""
|
| 99 |
+
st.session_state["response"] = None
|
| 100 |
+
st.session_state["waiting_for_input"] = True
|
| 101 |
+
st.session_state["state_reset"] = False
|
| 102 |
+
|
| 103 |
+
def process_input():
|
| 104 |
+
"""Fetch Tom Riddle's response based on user input."""
|
| 105 |
+
user_input = st.session_state["user_input"]
|
| 106 |
+
if user_input.strip():
|
| 107 |
+
try:
|
| 108 |
+
response = agent.run(
|
| 109 |
+
f"You are Tom Riddle, a cunning and enigmatic character from Harry Potter. Respond concisely and pragmatically: {user_input}"
|
| 110 |
+
)
|
| 111 |
+
st.session_state["response"] = response
|
| 112 |
+
st.session_state["waiting_for_input"] = False
|
| 113 |
+
|
| 114 |
+
except Exception as e:
|
| 115 |
+
st.error(f"An error occurred: {e}")
|
| 116 |
+
|
| 117 |
+
# Show input area or response based on state
|
| 118 |
+
if st.session_state["waiting_for_input"]:
|
| 119 |
+
st.text_area(
|
| 120 |
+
"",
|
| 121 |
+
placeholder="Write your thoughts here...",
|
| 122 |
+
key="user_input",
|
| 123 |
+
on_change=process_input,
|
| 124 |
+
)
|
| 125 |
+
elif st.session_state["response"]:
|
| 126 |
+
st.markdown(
|
| 127 |
+
f"<div class='response'>{st.session_state['response']}</div>",
|
| 128 |
+
unsafe_allow_html=True,
|
| 129 |
+
)
|
| 130 |
+
# Wait for fade-out to complete
|
| 131 |
+
time.sleep(6.5)
|
| 132 |
+
# Fully reset state to start over
|
| 133 |
+
st.session_state["state_reset"] = True
|
| 134 |
+
st.rerun()
|
| 135 |
+
|
| 136 |
+
if __name__ == "__main__":
|
| 137 |
+
main()
|