Update app.py
Browse files
app.py
CHANGED
@@ -1,103 +1,75 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
from langchain.agents import
|
4 |
-
from langchain.
|
|
|
5 |
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
import torch
|
7 |
-
from transformers import BlipProcessor,BlipForConditionalGeneration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
import requests
|
9 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
from langchain.tools import BaseTool
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
image_to_text_model="Salesforce/blip-image-captioning-large"
|
20 |
-
device= 'cuda' if torch.cuda.is_available() else 'cpu'
|
21 |
-
|
22 |
-
processor=BlipProcessor.from_pretrained(image_to_text_model)
|
23 |
-
|
24 |
-
model=BlipForConditionalGeneration.from_pretrained(image_to_text_model).to(device)
|
25 |
-
|
26 |
-
def descImage(image_url):
|
27 |
-
image_obj=Image.open(image_url).convert('RGB')
|
28 |
-
inputs=processor(image_obj,return_tensors='pt').to(device)
|
29 |
-
outputs=model.generate(**inputs)
|
30 |
-
return processor.decode(outputs[0],skip_special_tokens=True)
|
31 |
-
|
32 |
-
def toChinese(en:str):
|
33 |
-
pp="翻译下面语句到中文\n{en}"
|
34 |
-
prompt = PromptTemplate(
|
35 |
-
input_variables=["en"],
|
36 |
-
template=pp
|
37 |
-
)
|
38 |
-
llchain=LLMChain(llm=llm,prompt=prompt)
|
39 |
-
return llchain.run(en)
|
40 |
-
|
41 |
-
class DescTool(BaseTool):
|
42 |
-
name="Describe Image Tool"
|
43 |
-
description="use this tool to describe an image"
|
44 |
-
|
45 |
-
def _run(self,url:str):
|
46 |
-
description=descImage(url)
|
47 |
return description
|
48 |
-
|
49 |
-
|
50 |
-
raise NotImplementedError(
|
51 |
-
|
52 |
-
tools=[
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
return
|
81 |
-
|
82 |
-
with gr.Blocks(css=".chat-blocks{height:calc(100vh - 332px);} .mychat{flex:1} .mychat .block{min-height:100%} .mychat .block .wrap{max-height: calc(100vh - 330px);} .myinput{flex:initial !important;min-height:180px}") as demo:
|
83 |
-
title = '图像识别'
|
84 |
-
demo.title=title
|
85 |
-
with gr.Column(elem_classes="chat-blocks"):
|
86 |
-
with gr.Row(elem_classes="mychat"):
|
87 |
-
file = gr.Image(type="filepath")
|
88 |
-
chatbot = gr.Chatbot(label="图像识别", show_label=False)
|
89 |
-
with gr.Column(elem_classes="myinput"):
|
90 |
-
user_input = gr.Textbox(show_label=False, placeholder="请输入...", lines=1).style(
|
91 |
-
container=False)
|
92 |
-
submitBtn = gr.Button("提交", variant="primary", elem_classes="btn1")
|
93 |
-
emptyBtn = gr.Button("清除历史").style(container=False)
|
94 |
-
|
95 |
-
history = gr.State([])
|
96 |
-
submitBtn.click(predict, [file,user_input, chatbot,history], [chatbot, history],
|
97 |
-
show_progress=True)
|
98 |
-
submitBtn.click(reset_user_input, [], [user_input])
|
99 |
-
emptyBtn.click(reset_state, outputs=[chatbot, history], show_progress=True)
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
demo.queue(api_open=False,concurrency_count=20).launch()
|
|
|
1 |
+
from langchain.agents import load_tools
|
2 |
+
from langchain.agents import initialize_agent
|
3 |
+
from langchain.agents import AgentType
|
4 |
+
from langchain.llms import OpenAI
|
5 |
+
from langchain.chat_models import ChatOpenAI
|
6 |
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
7 |
+
|
8 |
+
import os
|
9 |
+
|
10 |
+
OPENAI_API_KEY = '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'
|
11 |
+
|
12 |
+
llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, temperature=0, model_name='gpt-3.5-turbo')
|
13 |
+
|
14 |
import torch
|
15 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
16 |
+
|
17 |
+
image_to_text_model = "Salesforce/blip-image-captioning-large"
|
18 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
19 |
+
|
20 |
+
processor = BlipProcessor.from_pretrained(image_to_text_model)
|
21 |
+
model = BlipForConditionalGeneration.from_pretrained(image_to_text_model).to(device)
|
22 |
+
|
23 |
+
from transformers.models.oneformer.modeling_oneformer import OneFormerModelOutput
|
24 |
import requests
|
25 |
from PIL import Image
|
26 |
+
|
27 |
+
def describeImage(image_url):
|
28 |
+
image_object = Image.open(image_url).convert('RGB')
|
29 |
+
# image
|
30 |
+
inputs = processor(image_object, return_tensors="pt").to(device)
|
31 |
+
outputs = model.generate(**inputs)
|
32 |
+
return processor.decode(outputs[0], skip_special_tokens=True)
|
33 |
+
|
34 |
+
|
35 |
from langchain.tools import BaseTool
|
36 |
+
|
37 |
+
class DescribeImageTool(BaseTool):
|
38 |
+
name = "Describe Image Tool"
|
39 |
+
description = 'use this tool to describe an image.'
|
40 |
+
|
41 |
+
def _run(self, url: str):
|
42 |
+
description = describeImage(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
return description
|
44 |
+
|
45 |
+
def _arun(self, query: str):
|
46 |
+
raise NotImplementedError("Async operation not supported yet")
|
47 |
+
|
48 |
+
tools = [DescribeImageTool()]
|
49 |
+
|
50 |
+
|
51 |
+
agent = initialize_agent(
|
52 |
+
agent='chat-conversational-react-description',
|
53 |
+
tools=tools,
|
54 |
+
llm=llm,
|
55 |
+
verbose=True,
|
56 |
+
max_iterations=3,
|
57 |
+
early_stopping_method='generate',
|
58 |
+
memory=ConversationBufferWindowMemory(
|
59 |
+
memory_key='chat_history',
|
60 |
+
k=5,
|
61 |
+
return_messages=True
|
62 |
+
)
|
63 |
+
)
|
64 |
+
|
65 |
+
|
66 |
+
import gradio as gr
|
67 |
+
def segment(image):
|
68 |
+
#pass # Implement your image segmentation model here...
|
69 |
+
print(image)
|
70 |
+
image_url = image
|
71 |
+
|
72 |
+
return agent(f"Describe the following image:\n{image_url}").get('output').replace('The response to your last comment is','')
|
73 |
+
|
74 |
+
demo = gr.Interface(segment, gr.Image(type="filepath",shape=(200, 200)), "text")
|
75 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|