Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,22 @@
|
|
1 |
-
|
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 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
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 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
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 |
-
|
69 |
-
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
2 |
|
|
|
3 |
|
4 |
+
import gradio as gr
|
5 |
+
import langChain_test
|
6 |
+
|
7 |
+
def image_to_text(image_path,question):
|
8 |
+
return langChain_test.imageAnalyse(image_path,question)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
|
11 |
+
with gr.Blocks() as demo:
|
12 |
+
image_path = gr.Image(type="filepath",label="请选择一张图片")
|
13 |
+
question = gr.Textbox(label="输入您的问题",lines=2)
|
14 |
+
output = gr.Textbox(label="输出答案",lines=2,placeholder="提交后将自动输出答案")
|
15 |
+
greet_btn = gr.Button("提交",variant="primary")
|
16 |
+
greet_btn.click(fn=image_to_text, inputs=[image_path,question], outputs=output, api_name="image_to_text")
|
17 |
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
demo.launch()
|
21 |
+
|
22 |
+
#demo.launch(share=True)
|