Defkhan5960 commited on
Commit
4ba3e6e
1 Parent(s): 9c9a2fe

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from PIL import Image
4
+ from io import BytesIO
5
+ import base64
6
+
7
+ API_KEY = "AIzaSyAbKZgDxZ-lL-vNXStTl0S4vZftDSYu890"
8
+
9
+
10
+
11
+ st.set_page_config(page_title="Image To Text", page_icon="icon.png", layout="centered", initial_sidebar_state="auto", menu_items=None)
12
+
13
+ # Streamlit başlık
14
+ st.title("Gemini API Resim Analizi")
15
+
16
+
17
+ # Kullanıcının resim yüklemesine izin verme
18
+ uploaded_file = st.file_uploader("Lütfen bir resim yükleyin", type=["jpg", "jpeg", "png"])
19
+
20
+ st.subheader("""
21
+ Resim yükledikten sonra resim hakkında istediğiniz soruyu aşağıda çıkacak alana sorabilirsiniz.
22
+ Lütfen sadece resim hakkında soru sorun !!!
23
+ """)
24
+
25
+
26
+
27
+ # Eğer resim yüklendiyse
28
+ if uploaded_file is not None:
29
+
30
+
31
+
32
+ # Resmi görüntüle
33
+ image = Image.open(uploaded_file)
34
+ st.image(image, caption="Yüklenen Resim", use_column_width=True)
35
+ prompt_text = st.text_area("Lütfen resimde sormak istediğiniz şeyi yazın")
36
+ buton = st.button("Resmi Yorumla")
37
+ image = image.convert('RGB')
38
+
39
+ # Gemini API için resmi hazırla
40
+ image_bytes = BytesIO()
41
+ image.save(image_bytes, format='JPEG')
42
+ image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
43
+
44
+ # Gemini API isteği yap
45
+ api_url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateContent?key=AIzaSyAbKZgDxZ-lL-vNXStTl0S4vZftDSYu890"
46
+ headers = {'Content-Type': 'application/json'}
47
+
48
+
49
+
50
+ prompt_text_ben = """
51
+
52
+ Bu resimde gördüğünüz sahneyi ayrıntılı bir şekilde tanımlamanızı istiyorum. Lütfen resimdeki ana nesne veya nesneleri belirtin, renklerini ve konumlarını açıklayın. Ayrıca, resimdeki atmosferi veya duygusal tonu yakalamak için kullanabileceğiniz ifadeleri eklemeniz beni memnun eder.
53
+
54
+ """
55
+ request_data = {
56
+ "contents": [
57
+ {
58
+ "parts": [
59
+ {"text": prompt_text},
60
+ {
61
+ "inline_data": {
62
+ "mime_type": "image/jpeg",
63
+ "data": image_base64
64
+ }
65
+ }
66
+ ]
67
+ }
68
+ ]
69
+ }
70
+
71
+
72
+
73
+ if buton:
74
+ response = requests.post(api_url, json=request_data, headers=headers)
75
+ print(response.json())
76
+
77
+ # Gemini API'den gelen cevabı işle
78
+ result_text = response.json()['candidates'][0]['content']['parts'][0]['text']
79
+
80
+ # Kullanıcıya sonucu göster
81
+ st.subheader("Resmin İçeriği:")
82
+ st.write(result_text)