emnagc6's picture
Update app.py
f3f6f0b verified
raw
history blame contribute delete
No virus
2.92 kB
# 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()