|
import openai |
|
import requests |
|
import json |
|
import gradio as gr |
|
|
|
|
|
headers = {} |
|
|
|
def init_apis(openai_api_key, huggingface_api_key): |
|
|
|
openai.api_key = openai_api_key |
|
headers["Authorization"] = f"Bearer {huggingface_api_key}" |
|
return "APIs initialized successfully." |
|
|
|
|
|
init_interface = gr.Interface( |
|
fn=init_apis, |
|
inputs=[ |
|
gr.inputs.Textbox(label="OpenAI Key", type="password"), |
|
gr.inputs.Textbox(label="HuggingFace Key", type="password"), |
|
], |
|
outputs="text", |
|
title="Initialize APIs", |
|
description="Enter your OpenAI and Hugging Face API keys.", |
|
) |
|
|
|
def query(url): |
|
|
|
API_URL = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50" |
|
response = requests.get(url) |
|
response.raise_for_status() |
|
data = response.content |
|
api_response = requests.request("POST", API_URL, headers=headers, data=data) |
|
return json.loads(api_response.content.decode("utf-8")) |
|
|
|
def process_query(user_query): |
|
|
|
function_descriptions = [ |
|
{ |
|
"name": "目标检测模型", |
|
"description": "Send an image URL to the Hugging Face API and get the detected objects in the image", |
|
"parameters": { |
|
"type": "object", |
|
"properties": { |
|
"url": { |
|
"type": "string", |
|
"description": "The URL of the image to analyze", |
|
} |
|
}, |
|
"required": ["url"], |
|
}, |
|
} |
|
] |
|
|
|
response = openai.ChatCompletion.create( |
|
model="gpt-3.5-turbo-0613", |
|
messages=[{"role": "user", "content": user_query}], |
|
functions=function_descriptions, |
|
function_call="auto", |
|
) |
|
|
|
ai_response_message = response["choices"][0]["message"] |
|
url = eval(ai_response_message['function_call']['arguments']).get("url") |
|
function_response = query(url=url) |
|
|
|
second_response = openai.ChatCompletion.create( |
|
model="gpt-3.5-turbo-0613", |
|
messages=[ |
|
{"role": "user", "content": user_query}, |
|
ai_response_message, |
|
{ |
|
"role": "function", |
|
"name": "query", |
|
"content": json.dumps(function_response), |
|
}, |
|
], |
|
) |
|
|
|
return second_response['choices'][0]['message']['content'] |
|
|
|
|
|
query_interface = gr.Interface( |
|
fn=process_query, |
|
inputs=[ |
|
gr.inputs.Textbox(label="Question") |
|
], |
|
outputs="text", |
|
title="Process Query", |
|
description="Enter your question. The model will return the detected objects in the image.", |
|
) |
|
|
|
init_interface.launch() |
|
query_interface.launch() |