forked from ong-ck/Lablab-Open-AI-Hackathon-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
78 lines (67 loc) · 2.18 KB
/
main.py
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
# run "export REPLICATE_API_TOKEN=<API-TOKEN>"
# then run "streamlit run main.py" in the Shell to start
import replicate
import streamlit as st
from PIL import Image
from audiorecorder import audiorecorder
import string
import updateScore as uS
import updatePointer as uP
print("")
print("Start of code")
st.title("Logo Guessing Game")
# Logo list
logos = [
'assets/Adidas.jpg',
'assets/Amazon.jpg',
'assets/Apple.jpg',
'assets/Chanel.jpg',
'assets/Facebook.jpg',
'assets/Google.jpg',
'assets/Pepsi.jpg',
'assets/Starbucks.jpg',
'assets/Tommy.jpg'
]
x = uP.getPointer()
uP.updatePointer()
logo = logos[x]
logoAns = logos[x - 1]
start = logoAns.index('/')
end = logoAns.index('.', start + 1)
answer = logoAns[start + 1:end] # Correct answer to compare with useranswer
image = Image.open(logo)
st.image(image, caption='guess the logo', width=100)
st.text("Score: " + uS.printScore()) # Display score
# Audio Record Button
audio = audiorecorder("Click to record", "Recording...")
print("Record")
# Check if audio has been recorded and save as file locally
if len(audio) > 0:
print("Recording")
# To save audio to a file:
wav_file = open("audio.mp3", "wb")
wav_file.write(audio.tobytes())
# Uses replicate API to run whisper (cause Replit RAM too little to be able to run whisper locally, keeps crashing)
# Passes locally saved audio into API and retrieves transcription to be printed/used
# Comment out to prevent wasting free API uses
model = replicate.models.get("openai/whisper")
version = model.versions.get("30414ee7c4fffc37e260fcab7842b5be470b9b840f2b608f5baa9bbef9a259ed")
output = version.predict(audio=open("audio.mp3", "rb"))
reply = output["transcription"]
# Removes additional space at the front of the word whisper records
reply = reply[1:]
reply = reply.translate(str.maketrans('', '', string.punctuation))
st.text(reply)
st.text(answer)
print("x: ", x)
print("Recorded: ", reply)
print("Answer: ", answer)
if (reply.lower() == answer.lower()):
image = Image.open(logos[x])
uS.updateScore()
st.success('Correct!')
else:
st.error("Please try again!")
if st.button("Reset Score"):
uS.resetScore()
st.experimental_rerun()