Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.agents import load_tools
|
2 |
+
from langchain.agents import initialize_agent
|
3 |
+
from langchain.agents import AgentType
|
4 |
+
from langchain.chat_models import AzureChatOpenAI
|
5 |
+
from langchain.llms import OpenAI
|
6 |
+
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
7 |
+
|
8 |
+
import os
|
9 |
+
|
10 |
+
|
11 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
12 |
+
OPENAI_API_BASE = os.getenv("OPENAI_API_BASE")
|
13 |
+
#llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, temperature=0, model_name='gpt-3.5-turbo',openai_api_base=OPENAI_API_BASE)
|
14 |
+
llm = AzureChatOpenAI(deployment_name="bitservice_chat_35",openai_api_base=OPENAI_API_BASE,openai_api_key=OPENAI_API_KEY,openai_api_version="2023-03-15-preview",model_name="gpt-3.5-turbo")
|
15 |
+
|
16 |
+
|
17 |
+
import torch
|
18 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
19 |
+
|
20 |
+
image_to_text_model = "Salesforce/blip-image-captioning-large"
|
21 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
22 |
+
|
23 |
+
processor = BlipProcessor.from_pretrained(image_to_text_model)
|
24 |
+
model = BlipForConditionalGeneration.from_pretrained(image_to_text_model).to(device)
|
25 |
+
|
26 |
+
from transformers.models.oneformer.modeling_oneformer import OneFormerModelOutput
|
27 |
+
import requests
|
28 |
+
from PIL import Image
|
29 |
+
|
30 |
+
def describeImage(image_url):
|
31 |
+
image_object = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
|
32 |
+
# image
|
33 |
+
inputs = processor(image_object, return_tensors="pt").to(device)
|
34 |
+
outputs = model.generate(**inputs)
|
35 |
+
return processor.decode(outputs[0], skip_special_tokens=True)
|
36 |
+
|
37 |
+
|
38 |
+
from langchain.tools import BaseTool
|
39 |
+
|
40 |
+
class DescribeImageTool(BaseTool):
|
41 |
+
name = "Describe Image Tool"
|
42 |
+
description = 'use this tool to describe an image.'
|
43 |
+
|
44 |
+
def _run(self, url: str):
|
45 |
+
description = describeImage(url)
|
46 |
+
return description
|
47 |
+
|
48 |
+
def _arun(self, query: str):
|
49 |
+
raise NotImplementedError("Async operation not supported yet")
|
50 |
+
|
51 |
+
tools = [DescribeImageTool()]
|
52 |
+
|
53 |
+
|
54 |
+
agent = initialize_agent(
|
55 |
+
agent='chat-conversational-react-description',
|
56 |
+
tools=tools,
|
57 |
+
llm=llm,
|
58 |
+
verbose=True,
|
59 |
+
max_iterations=3,
|
60 |
+
early_stopping_method='generate',
|
61 |
+
memory=ConversationBufferWindowMemory(
|
62 |
+
memory_key='chat_history',
|
63 |
+
k=5,
|
64 |
+
return_messages=True
|
65 |
+
)
|
66 |
+
)
|
67 |
+
|
68 |
+
image_url = 'https://images.unsplash.com/photo-1682228287072-5e23cbffd487?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=987&q=80'
|
69 |
+
agent(f"Please describe the following image:\n{image_url}")
|