Commit
·
6ea05d6
1
Parent(s):
1f356cb
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# 定义headers
|
7 |
+
headers = {}
|
8 |
+
|
9 |
+
def init_apis(openai_api_key, huggingface_api_key):
|
10 |
+
# 这个函数用于初始化OpenAI和Hugging Face API
|
11 |
+
openai.api_key = openai_api_key
|
12 |
+
headers["Authorization"] = f"Bearer {huggingface_api_key}"
|
13 |
+
print("APIs initialized successfully.")
|
14 |
+
|
15 |
+
def query(url):
|
16 |
+
# 这个函数会向Hugging Face API发送图像URL,并获取图像中检测到的对象
|
17 |
+
API_URL = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50"
|
18 |
+
response = requests.get(url)
|
19 |
+
response.raise_for_status()
|
20 |
+
data = response.content
|
21 |
+
api_response = requests.request("POST", API_URL, headers=headers, data=data)
|
22 |
+
return json.loads(api_response.content.decode("utf-8"))
|
23 |
+
|
24 |
+
def process_query(openai_api_key, huggingface_api_key, user_query):
|
25 |
+
init_apis(openai_api_key, huggingface_api_key)
|
26 |
+
|
27 |
+
print("Processing the image object detection task...")
|
28 |
+
function_descriptions = [
|
29 |
+
{
|
30 |
+
"name": "目标检测模型",
|
31 |
+
"description": "Send an image URL to the Hugging Face API and get the detected objects in the image",
|
32 |
+
"parameters": {
|
33 |
+
"type": "object",
|
34 |
+
"properties": {
|
35 |
+
"url": {
|
36 |
+
"type": "string",
|
37 |
+
"description": "The URL of the image to analyze",
|
38 |
+
}
|
39 |
+
},
|
40 |
+
"required": ["url"],
|
41 |
+
},
|
42 |
+
}
|
43 |
+
]
|
44 |
+
|
45 |
+
response = openai.ChatCompletion.create(
|
46 |
+
model="gpt-3.5-turbo-0613",
|
47 |
+
messages=[{"role": "user", "content": user_query}],
|
48 |
+
functions=function_descriptions,
|
49 |
+
function_call="auto",
|
50 |
+
)
|
51 |
+
|
52 |
+
ai_response_message = response["choices"][0]["message"]
|
53 |
+
url = eval(ai_response_message['function_call']['arguments']).get("url")
|
54 |
+
function_response = query(url=url)
|
55 |
+
|
56 |
+
second_response = openai.ChatCompletion.create(
|
57 |
+
model="gpt-3.5-turbo-0613",
|
58 |
+
messages=[
|
59 |
+
{"role": "user", "content": user_query},
|
60 |
+
ai_response_message,
|
61 |
+
{
|
62 |
+
"role": "function",
|
63 |
+
"name": "query",
|
64 |
+
"content": json.dumps(function_response),
|
65 |
+
},
|
66 |
+
],
|
67 |
+
)
|
68 |
+
|
69 |
+
return second_response['choices'][0]['message']['content']
|
70 |
+
|
71 |
+
# Gradio界面
|
72 |
+
iface = gr.Interface(
|
73 |
+
fn=process_query,
|
74 |
+
inputs=[
|
75 |
+
gr.inputs.Textbox(label="OpenAI Key", type="password"),
|
76 |
+
gr.inputs.Textbox(label="HuggingFace Key", type="password"),
|
77 |
+
gr.inputs.Textbox(label="Question")
|
78 |
+
],
|
79 |
+
outputs="text",
|
80 |
+
title="Image Object Detection with Hugging Face and OpenAI",
|
81 |
+
description="Enter your OpenAI and Hugging Face API keys, and your question. The model will return the detected objects in the image.",
|
82 |
+
)
|
83 |
+
|
84 |
+
iface.launch()
|