Spaces:
Running
Running
Commit
•
51926ca
1
Parent(s):
013d9d6
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import freeGPT
|
3 |
+
from freeGPT import Client as GPT
|
4 |
+
import io
|
5 |
+
import os
|
6 |
+
from PIL import Image
|
7 |
+
import requests
|
8 |
+
|
9 |
+
st.title("ChatGPT Vision")
|
10 |
+
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")
|
11 |
+
msgs = st.container(height=500)
|
12 |
+
inco = msgs.container(height=100)
|
13 |
+
st.write("Upload image to use vision (optinal)")
|
14 |
+
file = st.file_uploader("Upload image")
|
15 |
+
inpt = st.chat_input(placeholder="Enter question...")
|
16 |
+
API_URL_BLIP = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-large"
|
17 |
+
API_URL_IMG = "https://api-inference.huggingface.co/models/stablediffusionapi/realistic-vision-v51"
|
18 |
+
api_token = os.environ.get("api_token")
|
19 |
+
headers = {"Authorization": f"Bearer {api_token}"}
|
20 |
+
|
21 |
+
def blip_query(filename):
|
22 |
+
with open(filename, "rb") as f:
|
23 |
+
data = f.read()
|
24 |
+
response = requests.post(API_URL_BLIP, headers=headers, data=data)
|
25 |
+
return response.json()
|
26 |
+
|
27 |
+
def generate_answer(prompt):
|
28 |
+
resp = Client.create_completion("gpt3", prompt)
|
29 |
+
return resp
|
30 |
+
|
31 |
+
def generate_image(payload):
|
32 |
+
response = requests.post(API_URL_IMG, headers=headers, json=payload)
|
33 |
+
return response.content
|
34 |
+
|
35 |
+
if inpt and file is not None:
|
36 |
+
imgp = blip_query(file)
|
37 |
+
pp = "Generate answer on this question: " + inpt + ". Use this image description to give answer: " + imgp[0]['generated_text']
|
38 |
+
output = generate_answer(pp)
|
39 |
+
aimsg = msgs.chat_message("Assistant")
|
40 |
+
aimsg.write(output)
|
41 |
+
|
42 |
+
elif inpt and file is None:
|
43 |
+
output = generate_answer(inpt)
|
44 |
+
aimsg = msgs.chat_message("Assistant")
|
45 |
+
aimsg.write(output)
|
46 |
+
|
47 |
+
elif inpt and ("Generate" in inpt or "Draw" in inpt or "Imagine" in inpt):
|
48 |
+
prompt = inpt.split("--")[1]
|
49 |
+
output = generate_image(prompt)
|
50 |
+
if output:
|
51 |
+
aimsg = msgs.chat_message("Assistant")
|
52 |
+
aimsg.write("Image generated successfully!")
|
53 |
+
aimsg.image(output, caption="Generated Image")
|