Patrick changed the structure #10
4 changed files with 57 additions and 13 deletions
|
@ -1,6 +1,5 @@
|
|||
from api import API
|
||||
|
||||
|
||||
class CLIChat:
|
||||
@staticmethod
|
||||
def chat():
|
||||
|
|
|
@ -71,16 +71,19 @@
|
|||
<!-- Output section: Simulating a conversation with AI -->
|
||||
<div class="output">
|
||||
<div class="conversation">
|
||||
<div class="user-message">User: What is the weather today?</div>
|
||||
<div class="ai-message">AI: It's sunny with a slight breeze.</div>
|
||||
<div class="user-message">User: Great! Thank you!</div>
|
||||
<div class="ai-message">AI: You're welcome!</div>
|
||||
{% for message in messages %}
|
||||
{% if message.startswith('User:') %}
|
||||
<div class="user-message">{{ message }}</div>
|
||||
{% else %}
|
||||
<div class="ai-message">{{ message }}</div>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Input section: Where user input is provided -->
|
||||
<form class="input" method="POST" action="">
|
||||
<input type="text" name="name" placeholder="Type your message here..." />
|
||||
<input type="text" name="user_message" placeholder="Type your message here..." />
|
||||
<button type="submit" name="option" value="voice"><img src="/static/img/microphone.svg" alt="microphone"></button>
|
||||
<button type="submit" name="option" value="chat">Send</button>
|
||||
</form>
|
33
py/voice_recognition.py
Normal file
33
py/voice_recognition.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
import speech_recognition as sr
|
||||
|
||||
|
||||
class Voice:
|
||||
@staticmethod
|
||||
def listen():
|
||||
recognizer = sr.Recognizer()
|
||||
|
||||
try:
|
||||
recognizer = sr.Recognizer()
|
||||
with sr.Microphone() as source:
|
||||
print("Adjusting for ambient noise...")
|
||||
recognizer.adjust_for_ambient_noise(source)
|
||||
print("Listening...")
|
||||
audio_data = recognizer.listen(source)
|
||||
print("Audio captured")
|
||||
try:
|
||||
text = recognizer.recognize_sphinx(audio_data) # Using Google Web Speech API
|
||||
except sr.UnknownValueError:
|
||||
text = "ERROR"
|
||||
except sr.RequestError as e:
|
||||
text = "ERROR"
|
||||
|
||||
except sr.RequestError as e:
|
||||
text = "ERROR"
|
||||
except sr.UnknownValueError:
|
||||
text = "ERROR"
|
||||
except Exception as e:
|
||||
text = "ERROR"
|
||||
return text
|
||||
print(listen())
|
||||
|
||||
|
|
@ -1,20 +1,29 @@
|
|||
from flask import Flask, request
|
||||
from flask import Flask, request, render_template
|
||||
from api import API
|
||||
|
||||
APP = Flask(__name__)
|
||||
api = API()
|
||||
messages = []
|
||||
|
||||
|
||||
@APP.route('/', methods=['GET', 'POST'])
|
||||
def index():
|
||||
global messages
|
||||
|
||||
if request.method == 'POST':
|
||||
name = request.form['name']
|
||||
option = request.form['option']
|
||||
|
||||
if option == "voice":
|
||||
print(name)
|
||||
elif option == "chat":
|
||||
print(name)
|
||||
user_message = request.form['user_message']
|
||||
|
||||
return APP.send_static_file('index.html')
|
||||
if option == "voice":
|
||||
messages.append(f"User: {user_message}")
|
||||
elif option == "chat":
|
||||
messages.append(f"User: {user_message}")
|
||||
|
||||
ai_response = "AI: " + api.send_message(user_message, 1)
|
||||
messages.append(ai_response)
|
||||
|
||||
return render_template('index.html', messages=messages)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in a new issue