Update src/streamlit_app.py
Browse files- src/streamlit_app.py +91 -38
src/streamlit_app.py
CHANGED
@@ -1,40 +1,93 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
|
|
4 |
import streamlit as st
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import mimetypes
|
3 |
+
import cv2
|
4 |
+
import uuid
|
5 |
import streamlit as st
|
6 |
+
from google import generativeai as genai
|
7 |
+
import pywhatkit as pw
|
8 |
|
9 |
+
# Configure Gemini API
|
10 |
+
def configure_api():
|
11 |
+
api_key = "AIzaSyBDkLcFKzE_T6r1E5XjbuXVaoW40Szn71s" # Use your real key here
|
12 |
+
genai.configure(api_key=api_key)
|
13 |
+
return genai.GenerativeModel('gemini-2.0-flash')
|
14 |
+
|
15 |
+
def capture_from_webcam():
|
16 |
+
cam = cv2.VideoCapture(0)
|
17 |
+
if not cam.isOpened():
|
18 |
+
st.error("Could not open webcam.")
|
19 |
+
return None
|
20 |
+
|
21 |
+
st.info("Capturing image... Press SPACE to capture.")
|
22 |
+
filename = "ahmad.jpg"
|
23 |
+
while True:
|
24 |
+
ret, frame = cam.read()
|
25 |
+
if not ret:
|
26 |
+
st.error("Failed to grab frame.")
|
27 |
+
break
|
28 |
+
|
29 |
+
cv2.imshow("Camera Feed - Press SPACE to capture", frame)
|
30 |
+
key = cv2.waitKey(1)
|
31 |
+
if key % 256 == 32: # SPACE pressed
|
32 |
+
cv2.imwrite(filename, frame)
|
33 |
+
break
|
34 |
+
elif key % 256 == 27: # ESC
|
35 |
+
break
|
36 |
+
|
37 |
+
cam.release()
|
38 |
+
cv2.destroyAllWindows()
|
39 |
+
if os.path.exists(filename):
|
40 |
+
return filename
|
41 |
+
return None
|
42 |
+
|
43 |
+
def process_image(image_path, model):
|
44 |
+
mime_type, _ = mimetypes.guess_type(image_path)
|
45 |
+
if not mime_type:
|
46 |
+
mime_type = "image/jpeg"
|
47 |
+
|
48 |
+
with open(image_path, "rb") as img_file:
|
49 |
+
image_data = img_file.read()
|
50 |
+
|
51 |
+
response = model.generate_content(
|
52 |
+
[
|
53 |
+
{"mime_type": mime_type, "data": image_data},
|
54 |
+
"""complete analyze the given picture pay attention any detail of picture and tell which pakistani song is suitble according person in picture
|
55 |
+
just give me the name of one hindi song according to peroson in pic and just give the name of song no other detail just name ."""
|
56 |
+
]
|
57 |
+
)
|
58 |
+
return response.text.strip()
|
59 |
+
|
60 |
+
# Streamlit UI
|
61 |
+
st.set_page_config(page_title="Hindi Song Recommender", layout="centered")
|
62 |
+
st.title("🎶 Hindi Song Recommender from Image")
|
63 |
+
|
64 |
+
st.write("### Aap image kaise doge?")
|
65 |
+
option = st.radio("Choose input method:", ["Upload Image", "Capture from Webcam"])
|
66 |
+
|
67 |
+
model = configure_api()
|
68 |
+
|
69 |
+
if option == "Upload Image":
|
70 |
+
uploaded_file = st.file_uploader("Drag and drop your image here", type=["jpg", "jpeg", "png"])
|
71 |
+
if uploaded_file:
|
72 |
+
path = f"temp_{uuid.uuid4().hex[:8]}.jpg"
|
73 |
+
with open(path, "wb") as f:
|
74 |
+
f.write(uploaded_file.read())
|
75 |
+
st.session_state.image_path = path
|
76 |
+
st.image(path, caption="Uploaded Image", use_container_width=True)
|
77 |
+
|
78 |
+
elif option == "Capture from Webcam":
|
79 |
+
if st.button("Capture Image from Webcam"):
|
80 |
+
path = capture_from_webcam()
|
81 |
+
if path:
|
82 |
+
st.session_state.image_path = path
|
83 |
+
st.image(path, caption="Captured Image", use_container_width=True)
|
84 |
+
|
85 |
+
if "image_path" in st.session_state and st.button("Suggest Hindi Song"):
|
86 |
+
with st.spinner("Analyzing image and finding a song..."):
|
87 |
+
try:
|
88 |
+
suggested_song = process_image(st.session_state.image_path, model)
|
89 |
+
st.success(f"🎵 Suggested Hindi Song: {suggested_song}")
|
90 |
+
pw.playonyt(suggested_song)
|
91 |
+
st.info("Playing song on YouTube...")
|
92 |
+
except Exception as e:
|
93 |
+
st.error(f"Error: {e}")
|