File size: 2,920 Bytes
df32f0c
 
 
 
 
 
 
 
 
 
f3f6f0b
df32f0c
 
bc89f80
df32f0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35155a7
df32f0c
 
 
 
 
 
 
 
e73b138
df32f0c
 
 
 
 
 
 
 
 
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
# Gerekli kütüphaneleri import et

import streamlit as st
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image
import requests
from langchain_openai import ChatOpenAI
import os

# Kullanacağımız dil modeli için api key ayarlayalım
os.environ["OPENAI_API_KEY"] = "sk-proj-W3OXvRFZne7yFIvSUmzlT3BlbkFJoSoFKoAXMw0vqjjMjiLo"

# Model yolunu belirt
model_dir = "model"

# BlipProcessor ve BlipForConditionalGeneration sınıflarını kullanarak
# yereldeki eğittiğimiz modeli yükleyelim
processor = BlipProcessor.from_pretrained(model_dir)
model = BlipForConditionalGeneration.from_pretrained(model_dir)


# Altyazı oluşturma fonksiyonu
def get_image_captions(images):
    captions = []
    for image in images:
        inputs = processor(image, return_tensors="pt")
        out = model.generate(**inputs)
        caption = processor.decode(out[0], skip_special_tokens=True)
        captions.append(caption)
    return captions

# Streamlit uygulamasının başlangıcı
def main():
    st.title("Çoklu resimlere yönelik altyazı oluşturma")
    
    # Kullanıcıdan resimleri al
    uploaded_images = st.file_uploader("Lütfen resimleri yükleyin", accept_multiple_files=True, type=['jpg', 'png', 'jpeg'])
    
    if uploaded_images:
        images = [Image.open(img) for img in uploaded_images]
        
        # Resimlerden açıklamaları al
        captions = get_image_captions(images)
        
        # Açıklamaları ekrana yazdır
        st.subheader("Resim Açıklamaları:")
        for i, caption in enumerate(captions):
            translate = f"translate this image caption in turkish: {caption} "
            llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.6,)
            response = llm.invoke(translate)
            st.write(f"{i+1}. {response.content.strip()}")
        
        # OpenAI ile etkileşim
        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}"
        
        # OpenAI'ye prompt gönder
        llm = ChatOpenAI(model="gpt-4", temperature=0.6,)
        response = llm.invoke(prompt)
        
        # OpenAI'den gelen yanıtı ekrana yazdır
        st.subheader("Resim koleksiyonunuzun altyazısı:")
        st.write(response.content.strip())

# Uygulamayı çalıştır
if __name__ == "__main__":
    main()