import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import sentencepiece as spm
import streamlit as st
import time
import asyncio
import sys
from huggingface_hub import hf_hub_download
os.environ["STREAMLIT_WATCHED_FILES"] = ""
if sys.platform.startswith("win"):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
torch.classes.__path__ = [os.path.join(torch.__path__[0], torch.classes.__file__)]
# # or simply:
# torch.classes.__path__ = []
# Page configuration
st.set_page_config(
page_title="Mehfil-e-Sukhan",
page_icon="📜",
layout="wide" # Using wide layout for side-by-side content
)
# Custom spinner implementation
def custom_spinner():
spinner_html = """
', unsafe_allow_html=True)
st.markdown('
Number of Words
', unsafe_allow_html=True)
st.markdown('
Total words in the generated poetry
', unsafe_allow_html=True)
# Number of Words Control
num_words = st.selectbox("Number of Words", options=[12, 18, 24, 30, 36, 42, 48], index=0, label_visibility="collapsed")
st.markdown('
', unsafe_allow_html=True)
# Creativity (Temperature) Control
st.markdown('', unsafe_allow_html=True)
st.markdown('
Creativity
', unsafe_allow_html=True)
st.markdown('
Higher values generate more unique poetry
', unsafe_allow_html=True)
# Creativity (Temperature) Control
temperature = st.slider("Creativity", 0.5, 2.0, 1.2, 0.1, key="creativity", label_visibility="collapsed")
st.markdown('
', unsafe_allow_html=True)
# Focus (Top-p) Control
st.markdown('', unsafe_allow_html=True)
st.markdown('
Focus
', unsafe_allow_html=True)
st.markdown('
Higher focus makes the AI stick to probable words
', unsafe_allow_html=True)
# Focus (Top-p) Control
top_p = st.slider("Focus", 0.5, 1.0, 0.85, 0.05, key="focus", label_visibility="collapsed")
st.markdown('
', unsafe_allow_html=True)
st.markdown('', unsafe_allow_html=True)
# Right Column (70%) - Input and Output
with col2:
# Add a container with top margin
st.markdown('', unsafe_allow_html=True)
# Starting Word Input with proper left margin
st.markdown('
Starting Word/Phrase
', unsafe_allow_html=True)
# Starting Word Input
start_word = st.text_input("Starting Word/Phrase", value="ishq", placeholder="Enter a Roman Urdu word", label_visibility="collapsed")
# Generate Button with custom width
st.markdown('
', unsafe_allow_html=True)
generate_button = st.button("Generate Poetry")
st.markdown('
', unsafe_allow_html=True)
# And then in your button click handler:
if generate_button:
if not sp or not model:
st.error("Models not properly loaded. Please check the model files.")
else:
# Create a placeholder for the spinner
spinner_placeholder = st.empty()
# Show custom spinner
spinner_placeholder.markdown(custom_spinner(), unsafe_allow_html=True)
# Generate poetry
time.sleep(0.1) # Add slight delay for smooth transition
st.session_state.poetry = generate_poetry_nucleus(
model,
sp,
start_word,
num_words=num_words,
temperature=temperature,
top_p=top_p
)
# Clear the spinner once poetry is generated
spinner_placeholder.empty()
# Display generated poetry
if st.session_state.poetry:
st.markdown(f"""
{st.session_state.poetry}
""", unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
# Footer with quote
st.markdown("""
""", unsafe_allow_html=True)
if __name__ == "__main__":
main()