emnagc6 commited on
Commit
df32f0c
1 Parent(s): 3f57fe5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Gerekli kütüphaneleri import et
2
+
3
+ import streamlit as st
4
+ from transformers import BlipProcessor, BlipForConditionalGeneration
5
+ from PIL import Image
6
+ import requests
7
+ from langchain_openai import ChatOpenAI
8
+ import os
9
+
10
+ # Kullanacağımız dil modeli için api key ayarlayalım
11
+ os.environ["OPENAI_API_KEY"] = "sk-proj-p2nvtxZx1k6EQDI57QmAT3BlbkFJmHgjHI1xMjqV1058WTuG"
12
+
13
+ # Model yolunu belirt
14
+ model_dir = "C:\\Users\\M. Emin\\Desktop\\st\\model"
15
+
16
+ # BlipProcessor ve BlipForConditionalGeneration sınıflarını kullanarak
17
+ # yereldeki eğittiğimiz modeli yükleyelim
18
+ processor = BlipProcessor.from_pretrained(model_dir)
19
+ model = BlipForConditionalGeneration.from_pretrained(model_dir)
20
+
21
+
22
+ # Altyazı oluşturma fonksiyonu
23
+ def get_image_captions(images):
24
+ captions = []
25
+ for image in images:
26
+ inputs = processor(image, return_tensors="pt")
27
+ out = model.generate(**inputs)
28
+ caption = processor.decode(out[0], skip_special_tokens=True)
29
+ captions.append(caption)
30
+ return captions
31
+
32
+ # Streamlit uygulamasının başlangıcı
33
+ def main():
34
+ st.title("Çoklu resimlere yönelik altyazı oluşturma")
35
+
36
+ # Kullanıcıdan resimleri al
37
+ uploaded_images = st.file_uploader("Lütfen resimleri yükleyin", accept_multiple_files=True, type=['jpg', 'png', 'jpeg'])
38
+
39
+ if uploaded_images:
40
+ images = [Image.open(img) for img in uploaded_images]
41
+
42
+ # Resimlerden açıklamaları al
43
+ captions = get_image_captions(images)
44
+
45
+ # Açıklamaları ekrana yazdır
46
+ st.subheader("Resim Açıklamaları:")
47
+ for i, caption in enumerate(captions):
48
+ translate = f"translate this in turkish: {caption} "
49
+ llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.6,)
50
+ response = llm.invoke(translate)
51
+ st.write(f"{i+1}. {response.content.strip()}")
52
+
53
+ # OpenAI ile etkileşim
54
+ prompt = f"You will be given a list.If there is a common object in the pictures in this list, write the object or the group to which the object belongs, or whatever relevance there is between them. if there is a common verb, write the verb, if there is a common object, write the object, if there is a common relevance, write the relevance. if there are two things in common (e.g. object and verb), write both. if there are all three, write all three. it will be completely common and completely objective. write only one of them as an answer. e.g. 'tech gadgets', 'people playing games', 'extreme sports' etc. write the answer only in Turkish.list: {captions}"
55
+
56
+ # OpenAI'ye prompt gönder
57
+ llm = ChatOpenAI(model="gpt-4", temperature=0.6,)
58
+ response = llm.invoke(prompt)
59
+
60
+ # OpenAI'den gelen yanıtı ekrana yazdır
61
+ st.subheader("Resim koleksiyonunuzun altyazısı:")
62
+ st.write(response.content.strip())
63
+
64
+ # Uygulamayı çalıştır
65
+ if __name__ == "__main__":
66
+ main()