CoolFace
Apppublic

Shanayyyyy/AdventureBot

sourceHugging Facemitupdated 8mo agoView on Hugging Face
0likes
app.py108 linesDownload Raw Back to root
1import streamlit as st2from openai import OpenAI3from pyngrok import ngrok4 5# ---------------------------6# ---- GROQ API SETUP ----7# ---------------------------8# Hard-coded Groq API key9client = OpenAI(10    api_key="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",11    base_url="https://api.groq.com/openai/v1"12)13 14# ---------------------------15# ---- STREAMLIT APP SETUP ----16# ---------------------------17st.set_page_config(page_title="๐ŸŒธ AI Adventure Bot ๐ŸŒธ", page_icon="โœจ", layout="centered")18 19# Apply cute pastel theme using markdown20st.markdown(21    """22    <style>23    .stApp {24        background-color: #FFF0F5;  /* light lavender */25        color: #FF69B4;26        font-family: 'Comic Sans MS', cursive, sans-serif;27    }28    .stButton>button {29        background-color: #FFB6C1;30        color: white;31        font-size: 18px;32        border-radius: 15px;33        height: 50px;34        width: 120px;35    }36    input[type=text] {37        border-radius: 12px;38        height: 40px;39        font-size: 16px;40    }41    </style>42    """, unsafe_allow_html=True43)44 45st.markdown("<h1 style='text-align:center;'>๐ŸŒธ AI Choose Your Adventure ๐ŸŒธ</h1>", unsafe_allow_html=True)46st.markdown("<p style='text-align:center;'>Your choices will shape a magical story ๐Ÿ’–โœจ</p>", unsafe_allow_html=True)47 48# ---------------------------49# ---- SESSION STATE ----50# ---------------------------51if "story" not in st.session_state:52    st.session_state.story = ["You wake up in a mysterious magical forest. ๐ŸŒฒโœจ"]53 54# ---------------------------55# ---- GENERATE NEXT STORY ----56# ---------------------------57def generate_next_story(user_input, story_so_far):58    messages = [59        {60            "role": "system",61            "content": "You are a whimsical storyteller who writes cute, magical adventure stories with emojis."62        },63        {64            "role": "user",65            "content": f"Story so far:\n{story_so_far}\n\nUser choice: {user_input}\nContinue the story in 2โ€“3 sentences with emojis."66        }67    ]68 69    response = client.chat.completions.create(70        model="llama-3.3-70b-versatile",71        messages=messages,72        max_tokens=150,73        temperature=0.874    )75 76    # โœ… Fix: use .content instead of ["content"]77    return response.choices[0].message.content78 79# ---------------------------80# ---- DISPLAY STORY ----81# ---------------------------82st.markdown("### Story so far:")83for line in st.session_state.story:84    st.markdown(f"- {line}")85 86# ---------------------------87# ---- USER INPUT ----88# ---------------------------89st.markdown("### Choose your next action:")90user_input = st.text_input("Type your choice")91 92# Cute Go button93if st.button("Go!"):94    if user_input.strip():95        next_text = generate_next_story(user_input, "\n".join(st.session_state.story))96        st.session_state.story.append(f"**You chose:** {user_input}")97        st.session_state.story.append(next_text)98        st.experimental_rerun()99    else:100        st.warning("Please type your choice! ๐ŸŒธ")101 102# ---------------------------103# ---- OPTIONAL NGROK FOR COLAB ----104# ---------------------------105if "COLAB_GPU" in st.session_state:106    public_url = ngrok.connect(port="8501")107    st.markdown(f"**Open your bot here:** [Click me]({public_url})")108