Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import streamlit.components.v1 as components
|
| 3 |
+
|
| 4 |
+
# Title of the app
|
| 5 |
+
st.title("Controllable Cursor with Text-to-Speech")
|
| 6 |
+
|
| 7 |
+
# Add a description of the app
|
| 8 |
+
st.write("""
|
| 9 |
+
This app allows you to move a virtual cursor using mouse (for laptop) or touch (for mobile).
|
| 10 |
+
You can click on any word or sentence, and it will read the text aloud in the selected language.
|
| 11 |
+
""")
|
| 12 |
+
|
| 13 |
+
# Language selection dropdown
|
| 14 |
+
language = st.selectbox(
|
| 15 |
+
"Select Language for Text-to-Speech",
|
| 16 |
+
("en", "es", "fr", "de", "it", "pt", "ja", "ko", "zh-CN", "ar")
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Embedded HTML and JavaScript for cursor control and text-to-speech
|
| 20 |
+
html_string = f"""
|
| 21 |
+
<html>
|
| 22 |
+
<head>
|
| 23 |
+
<style>
|
| 24 |
+
body {{
|
| 25 |
+
margin: 0;
|
| 26 |
+
}}
|
| 27 |
+
#cursor {{
|
| 28 |
+
position: absolute;
|
| 29 |
+
width: 30px;
|
| 30 |
+
height: 30px;
|
| 31 |
+
background-color: red;
|
| 32 |
+
border-radius: 50%;
|
| 33 |
+
pointer-events: none;
|
| 34 |
+
z-index: 9999;
|
| 35 |
+
}}
|
| 36 |
+
</style>
|
| 37 |
+
</head>
|
| 38 |
+
<body>
|
| 39 |
+
<div id="cursor"></div>
|
| 40 |
+
|
| 41 |
+
<div onclick="speakText(event)" style="padding: 50px; font-size: 18px;">
|
| 42 |
+
<p>Click on any word or sentence below to hear it read aloud.</p>
|
| 43 |
+
<p>This is a simple <strong>text-to-speech</strong> demo. <span>Click me</span> and I will be read aloud.</p>
|
| 44 |
+
</div>
|
| 45 |
+
|
| 46 |
+
<script>
|
| 47 |
+
const cursor = document.getElementById("cursor");
|
| 48 |
+
const language = "{language}"; // Set the selected language
|
| 49 |
+
|
| 50 |
+
// Mouse move handler for laptop/desktop
|
| 51 |
+
document.addEventListener("mousemove", function(event) {{
|
| 52 |
+
cursor.style.left = event.clientX - cursor.offsetWidth / 2 + "px";
|
| 53 |
+
cursor.style.top = event.clientY - cursor.offsetHeight / 2 + "px";
|
| 54 |
+
}});
|
| 55 |
+
|
| 56 |
+
// Touch move handler for mobile
|
| 57 |
+
document.addEventListener("touchmove", function(event) {{
|
| 58 |
+
event.preventDefault(); // Prevent scrolling
|
| 59 |
+
const touch = event.touches[0];
|
| 60 |
+
cursor.style.left = touch.clientX - cursor.offsetWidth / 2 + "px";
|
| 61 |
+
cursor.style.top = touch.clientY - cursor.offsetHeight / 2 + "px";
|
| 62 |
+
}});
|
| 63 |
+
|
| 64 |
+
// Prevent default touch behavior to avoid scrolling
|
| 65 |
+
document.body.style.touchAction = "none";
|
| 66 |
+
|
| 67 |
+
// Function to read the text aloud
|
| 68 |
+
function speakText(event) {{
|
| 69 |
+
const selectedText = event.target.innerText;
|
| 70 |
+
const speech = new SpeechSynthesisUtterance(selectedText);
|
| 71 |
+
speech.lang = language; // Set language for speech
|
| 72 |
+
window.speechSynthesis.speak(speech);
|
| 73 |
+
}}
|
| 74 |
+
</script>
|
| 75 |
+
</body>
|
| 76 |
+
</html>
|
| 77 |
+
"""
|
| 78 |
+
|
| 79 |
+
# Render the HTML and JavaScript inside the Streamlit app
|
| 80 |
+
components.html(html_string, height=500)
|