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