subhan971 commited on
Commit
a1dd6d0
·
verified ·
1 Parent(s): fdfab64

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +91 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,93 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
 
4
  import streamlit as st
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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}")