Spaces:
Sleeping
Sleeping
tool_str="" | |
import json | |
# Specify the file path to your JSON file | |
file_path = 'data.json' | |
# Reading JSON data | |
with open(file_path, 'r') as file: | |
data = json.load(file) | |
for t in data: | |
tool_str+=t['new_name']+" : " | |
tool_str+=t['text']+"\n" | |
system_prompt=f"""You help out with tools! From the users request, pick the most usable tool from the list. | |
{tool_str}""" | |
from openai import OpenAI | |
import os | |
client = OpenAI(api_key=os.environ.get("OPEN_AI_KEY")) | |
system_prompt=f"""You help out with tools! From the users request, pick the most usable tool from the list. | |
{tool_str}""" | |
def run_conversation(text): | |
# Step 1: send the conversation and available functions to the model | |
messages = [ | |
{ | |
"role": "system", | |
"content": system_prompt | |
}, | |
{ | |
"role": "user", | |
"content": text | |
} | |
] | |
tools = [ | |
{ | |
"type": "function", | |
"function": { | |
"name": "output_file_path", | |
"description": "outputs the best suited file path", | |
"parameters": { | |
"type": "object", | |
"properties": { | |
"file_path": { | |
"type": "string", | |
"description": "File path starting with captured_images/...", | |
}, | |
}, | |
"required": ["file_path"], | |
}, | |
}, | |
} | |
] | |
response = client.chat.completions.create( | |
model="gpt-3.5-turbo-0125", | |
messages=messages, | |
tools=tools, | |
tool_choice="required", | |
) | |
response_message = response.choices[0].message | |
image_path=json.loads(response.choices[0].message.tool_calls[0].function.arguments)['file_path'] | |
messages.append({ | |
"role": "system", | |
"content": f"Chosen file_path: {image_path}\n Now respond with a friendly answer with your reasoning in the same language as the incoming message" | |
}) | |
response2 = client.chat.completions.create( | |
model="gpt-3.5-turbo-0125", | |
messages=messages | |
) | |
return image_path, response2.choices[0].message.content | |
import gradio as gr | |
from PIL import Image | |
import re | |
def show_image(text): | |
try: | |
image_path, response=run_conversation(text) | |
img = Image.open(image_path) | |
return img, response | |
except Exception as e: | |
return f"Error: {str(e)}" # Return error message if path is invalid or file is not found | |
def main(): | |
# Define the interface | |
interface = gr.Interface( | |
fn=show_image, | |
inputs="text", | |
outputs=["image", "text"], | |
title="Johans verktyg!", | |
description="Be om ett verktyg så plockar Johan fram det!" | |
) | |
# Launch the interface | |
interface.launch(share=True) | |
if __name__ == "__main__": | |
main() | |