File size: 2,351 Bytes
4e8865b
 
 
79ee59b
4e8865b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ab3821
 
4e8865b
 
 
 
 
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
#structure data extraction 
from openai import OpenAI
import json
import os
from dotenv import load_dotenv

load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key=api_key)

def extract_student_info(description):
    """
    Extracts structured student information from a given description.
    """
    # Initialize the dictionary to store extracted information
    student_info = {
        "name": None,
        "major": None,
        "school": None,
        "grades": None,
        "club": []
    }

    return student_info

functions = [
    {
        "name": "extract_student_info",
        "description": "Extracts structured student information from a given description.",
        "parameters": {
            "type": "object",
            "properties": {
                "description": {
                    "type": "string",
                    "description": "A detailed description of the student."
                }
            },
            "required": ["description"]
        }
    }
]

def query_openai(prompt):
    response = client.chat.completions.create(
        model='gpt-4o-mini',
        messages=[{'role': 'user', 'content': prompt}],
        functions=functions,
        function_call='auto',
    )

    message = response['choices'][0]['message']

    if message.get('function_call'):
        function_name = message['function_call']['name']
        function_args = json.loads(message['function_call']['arguments'])

        if function_name == 'extract_student_info':
            description = function_args.get('description')
            function_response = extract_student_info(description)

            return function_response

    return message['content']

import gradio as gr

def gradio_interface(description):
    prompt = f"Please extract the following information from the given text and return it as a JSON object:\n\nname\nmajor\nschool\ngrades\nclub\nThis is the body of text to extract the information from:\n{description}"
    result = query_openai(prompt)
    return result

iface = gr.Interface(
    fn=gradio_interface,
    inputs=gr.Textbox(lines=10, label="Student Description"),
    outputs=gr.JSON(label="Extracted Student Information"),
    title="Student Information Extractor",
    description="Enter a student's description to extract structured information."
)

iface.launch()