Spaces:
Running
Running
File size: 2,220 Bytes
51926ca b36f458 51926ca 25fbe63 51926ca 63cd697 25fbe63 027864c 51926ca 63cd697 51926ca 6f67272 a397059 51926ca |
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 |
import streamlit as st
import freeGPT
from freeGPT import Client
import io
import os
from PIL import Image
import requests
st.title("ChatGPT Vision")
st.write("Tap 'upload image' to upload image to vision and give question, if you want to generate image, then type 'Generate-- [prompt]' or 'Draw-- [prompt]' in your query")
msgs = st.container(height=500)
st.write("Upload image to use vision (optinal)")
file = st.file_uploader("Upload image")
inpt = st.chat_input(placeholder="Enter question...")
API_URL_BLIP = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-large"
API_URL_IMG = "https://api-inference.huggingface.co/models/stablediffusionapi/realistic-vision-v51"
api_token = os.environ.get("api_token")
headers = {"Authorization": f"Bearer {api_token}"}
def blip_query(image_bytes):
response = requests.post(API_URL_BLIP, headers=headers, data=image_bytes)
return response.json()
def generate_answer(prompt):
resp = Client.create_completion("gpt3", prompt)
return resp
def generate_image(payload):
response = requests.post(API_URL_IMG, headers=headers, json=payload)
return response.content
if inpt and file is not None and not("Generate" in inpt or "Draw" in inpt or "Imagine" in inpt):
image = Image.open(file)
image_bytes = io.BytesIO()
image.save(image_bytes, format=image.format)
image_bytes = image_bytes.getvalue()
imgp = blip_query(image_bytes)
pp = "Generate answer on this question: " + inpt + ". Use this image description to give answer: " + imgp[0]['generated_text']
output = generate_answer(pp)
aimsg = msgs.chat_message("Assistant")
aimsg.write(output)
elif inpt and file is None and not("Generate" in inpt or "Draw" in inpt or "Imagine" in inpt):
output = generate_answer(inpt)
aimsg = msgs.chat_message("Assistant")
aimsg.write(output)
elif inpt and ("Generate" in inpt or "Draw" in inpt or "Imagine" in inpt):
prompt = inpt.split("--")[1]
payload = {"prompts": prompt}
output = generate_image(payload)
if output:
aimsg = msgs.chat_message("Assistant")
aimsg.write("Image generated successfully!")
aimsg.image(output, caption="Generated Image") |