Spaces:
Runtime error
Runtime error
#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": [] | |
} | |
# Use OpenAI's function calling to extract information | |
# (Assuming you have set up the function calling as per OpenAI's documentation) | |
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.inputs.Textbox(lines=10, label="Student Description"), | |
outputs=gr.outputs.JSON(label="Extracted Student Information"), | |
title="Student Information Extractor", | |
description="Enter a student's description to extract structured information." | |
) | |
iface.launch() | |