File size: 2,530 Bytes
64daff8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
import streamlit as st
import sqlite3
from datetime import datetime
import speech_recognition as sr
from docx import Document
from io import BytesIO
# Database setup
def create_db():
conn = sqlite3.connect("records.db")
c = conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS records (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT,
timestamp TEXT
)
""")
conn.commit()
conn.close()
def insert_record(text, timestamp):
conn = sqlite3.connect("records.db")
c = conn.cursor()
c.execute("INSERT INTO records (text, timestamp) VALUES (?, ?)", (text, timestamp))
conn.commit()
conn.close()
def fetch_records():
conn = sqlite3.connect("records.db")
c = conn.cursor()
c.execute("SELECT * FROM records")
records = c.fetchall()
conn.close()
return records
# Speech to text
def speech_to_text():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
st.info("Listening...")
try:
audio = recognizer.listen(source, timeout=10)
text = recognizer.recognize_google(audio)
return text
except sr.UnknownValueError:
return "Could not understand audio"
except sr.RequestError as e:
return f"Error: {e}"
# Generate a Word document
def create_document(content):
doc = Document()
doc.add_heading("Recorded Text", level=1)
doc.add_paragraph(content)
buffer = BytesIO()
doc.save(buffer)
buffer.seek(0)
return buffer
# Streamlit app
def main():
create_db()
st.title("Voice to Text App")
# Voice recording
st.header("1. Record Your Voice")
if st.button("Record Voice"):
text = speech_to_text()
if text:
st.success("Text Recorded!")
st.write(text)
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
insert_record(text, timestamp)
# Display records
st.header("2. View Saved Records")
records = fetch_records()
for record in records:
st.write(f"{record[1]} (Recorded on {record[2]})")
# Download document
st.header("3. Download or Share Document")
if records:
if st.button("Generate Document"):
content = "\n\n".join([f"{rec[1]} (Recorded on {rec[2]})" for rec in records])
doc_file = create_document(content)
st.download_button("Download Document", doc_file, "recorded_text.docx")
if __name__ == "__main__":
main()
|