Spaces:
Build error
Build error
| import openai | |
| import requests | |
| import json | |
| import gradio as gr | |
| # 定义headers | |
| headers = {} | |
| def init_apis(openai_api_key, huggingface_api_key): | |
| # 这个函数用于初始化OpenAI和Hugging Face API | |
| openai.api_key = openai_api_key | |
| headers["Authorization"] = f"Bearer {huggingface_api_key}" | |
| return "APIs initialized successfully." | |
| # 创建用于初始化API的Gradio接口 | |
| 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): | |
| # 这个函数会向Hugging Face API发送图像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'] | |
| # 创建用于处理查询的Gradio接口 | |
| 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() |