qichangyuan commited on
Commit ·
18e5ea6
1
Parent(s): 7f54391
修改app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ from langchain.agents import load_tools
|
|
| 4 |
from langchain.agents import initialize_agent
|
| 5 |
from langchain.agents import AgentType
|
| 6 |
from langchain.llms import OpenAI
|
| 7 |
-
from langchain.chat_models import AzureChatOpenAI
|
| 8 |
from langchain.chat_models import ChatOpenAI
|
| 9 |
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
| 10 |
from transformers import BlipProcessor,BlipForConditionalGeneration
|
|
@@ -13,15 +12,20 @@ import requests
|
|
| 13 |
from PIL import Image
|
| 14 |
from langchain.tools import BaseTool
|
| 15 |
import gradio as gr
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 18 |
# OPENAI_API_BASE = os.getenv("OPENAI_AI_BASE")
|
| 19 |
# DEP_NAME = os.getenv("deployment name")
|
| 20 |
|
| 21 |
llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, temperature=0, model_name='gpt-3.5-turbo')
|
| 22 |
# llm = AzureChatOpenAI(deployment_name=DEP_NAME,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")
|
| 23 |
|
| 24 |
-
image_to_text_model = "Salesforce/blip-image-captioning-large"
|
|
|
|
|
|
|
| 25 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 26 |
|
| 27 |
processor = BlipProcessor.from_pretrained(image_to_text_model)
|
|
@@ -34,7 +38,7 @@ def describeImage(image_url):
|
|
| 34 |
outputs = model.generate(**inputs)
|
| 35 |
return processor.decode(outputs[0], skip_special_tokens=True)
|
| 36 |
|
| 37 |
-
img_url = 'https://img1.baidu.com/it/u=1919509102,1927615551&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500'
|
| 38 |
# description = describeImage(img_url)
|
| 39 |
# print(description)
|
| 40 |
|
|
@@ -68,21 +72,39 @@ agent = initialize_agent(
|
|
| 68 |
)
|
| 69 |
)
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
print( output )
|
| 74 |
desc = output['output']
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
|
| 83 |
-
|
| 84 |
-
return "Hello " + name + "!!"
|
| 85 |
|
| 86 |
-
|
| 87 |
-
iface = gr.Interface(fn=descImage,inputs="text",outputs="text")
|
| 88 |
-
iface.launch()
|
|
|
|
| 4 |
from langchain.agents import initialize_agent
|
| 5 |
from langchain.agents import AgentType
|
| 6 |
from langchain.llms import OpenAI
|
|
|
|
| 7 |
from langchain.chat_models import ChatOpenAI
|
| 8 |
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
| 9 |
from transformers import BlipProcessor,BlipForConditionalGeneration
|
|
|
|
| 12 |
from PIL import Image
|
| 13 |
from langchain.tools import BaseTool
|
| 14 |
import gradio as gr
|
| 15 |
+
from translate import Translator
|
| 16 |
+
from langchain import PromptTemplate, FewShotPromptTemplate, LLMChain
|
| 17 |
|
| 18 |
+
#OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 19 |
+
OPENAI_API_KEY = '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'
|
| 20 |
# OPENAI_API_BASE = os.getenv("OPENAI_AI_BASE")
|
| 21 |
# DEP_NAME = os.getenv("deployment name")
|
| 22 |
|
| 23 |
llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, temperature=0, model_name='gpt-3.5-turbo')
|
| 24 |
# llm = AzureChatOpenAI(deployment_name=DEP_NAME,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")
|
| 25 |
|
| 26 |
+
# image_to_text_model = "Salesforce/blip-image-captioning-large"
|
| 27 |
+
image_to_text_model = "F:\\code\\Anaconda\\blip-image-captioning-large"
|
| 28 |
+
|
| 29 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 30 |
|
| 31 |
processor = BlipProcessor.from_pretrained(image_to_text_model)
|
|
|
|
| 38 |
outputs = model.generate(**inputs)
|
| 39 |
return processor.decode(outputs[0], skip_special_tokens=True)
|
| 40 |
|
| 41 |
+
#img_url = 'https://img1.baidu.com/it/u=1919509102,1927615551&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500'
|
| 42 |
# description = describeImage(img_url)
|
| 43 |
# print(description)
|
| 44 |
|
|
|
|
| 72 |
)
|
| 73 |
)
|
| 74 |
|
| 75 |
+
# 翻译
|
| 76 |
+
def to_chinese(title):
|
| 77 |
+
pp = "翻译下面语句到中文\n{en}"
|
| 78 |
+
prompt = PromptTemplate(
|
| 79 |
+
input_variables=["en"],
|
| 80 |
+
template=pp
|
| 81 |
+
)
|
| 82 |
+
llchain = LLMChain(llm=llm, prompt=prompt)
|
| 83 |
+
return llchain.run(title)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def descImage(input_text , image_url) :
|
| 88 |
+
output = agent(f"{input_text}:\n{image_url}")
|
| 89 |
print( output )
|
| 90 |
desc = output['output']
|
| 91 |
+
# 对描述文字进行转换
|
| 92 |
+
desc_ch = to_chinese(desc)
|
| 93 |
+
return desc_ch
|
| 94 |
+
|
| 95 |
+
# ret = descImage("描述该图片","https://lmg.jj20.com/up/allimg/4k/s/02/2109250006343S5-0-lp.jpg")
|
| 96 |
+
# print(ret)
|
| 97 |
|
| 98 |
+
with gr.Blocks() as demo:
|
| 99 |
+
with gr.Column():
|
| 100 |
+
file = gr.Image(type='filepath')
|
| 101 |
+
user_input = gr.Textbox(show_label=False,placeholder="请输入问题",lines=1)
|
| 102 |
+
with gr.Column():
|
| 103 |
+
submitBtn = gr.Button("提交",variant="primary")
|
| 104 |
|
| 105 |
+
with gr.Column():
|
| 106 |
+
output = gr.TextArea(show_label=False,placeholder="输出结果",lines=5)
|
| 107 |
|
| 108 |
+
submitBtn.click(descImage,[user_input,file],output,show_progress=True)
|
|
|
|
| 109 |
|
| 110 |
+
demo.launch()
|
|
|
|
|
|