File size: 3,056 Bytes
79f15ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

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()