Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#structure data extraction
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import json
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
+
client = OpenAI(api_key=api_key)
|
| 9 |
+
|
| 10 |
+
def extract_student_info(description):
|
| 11 |
+
"""
|
| 12 |
+
Extracts structured student information from a given description.
|
| 13 |
+
"""
|
| 14 |
+
# Initialize the dictionary to store extracted information
|
| 15 |
+
student_info = {
|
| 16 |
+
"name": None,
|
| 17 |
+
"major": None,
|
| 18 |
+
"school": None,
|
| 19 |
+
"grades": None,
|
| 20 |
+
"club": []
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
# Use OpenAI's function calling to extract information
|
| 24 |
+
# (Assuming you have set up the function calling as per OpenAI's documentation)
|
| 25 |
+
|
| 26 |
+
return student_info
|
| 27 |
+
|
| 28 |
+
functions = [
|
| 29 |
+
{
|
| 30 |
+
"name": "extract_student_info",
|
| 31 |
+
"description": "Extracts structured student information from a given description.",
|
| 32 |
+
"parameters": {
|
| 33 |
+
"type": "object",
|
| 34 |
+
"properties": {
|
| 35 |
+
"description": {
|
| 36 |
+
"type": "string",
|
| 37 |
+
"description": "A detailed description of the student."
|
| 38 |
+
}
|
| 39 |
+
},
|
| 40 |
+
"required": ["description"]
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
def query_openai(prompt):
|
| 46 |
+
response = client.chat.completions.create(
|
| 47 |
+
model='gpt-4o-mini',
|
| 48 |
+
messages=[{'role': 'user', 'content': prompt}],
|
| 49 |
+
functions=functions,
|
| 50 |
+
function_call='auto',
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
message = response['choices'][0]['message']
|
| 54 |
+
|
| 55 |
+
if message.get('function_call'):
|
| 56 |
+
function_name = message['function_call']['name']
|
| 57 |
+
function_args = json.loads(message['function_call']['arguments'])
|
| 58 |
+
|
| 59 |
+
if function_name == 'extract_student_info':
|
| 60 |
+
description = function_args.get('description')
|
| 61 |
+
function_response = extract_student_info(description)
|
| 62 |
+
|
| 63 |
+
return function_response
|
| 64 |
+
|
| 65 |
+
return message['content']
|
| 66 |
+
|
| 67 |
+
import gradio as gr
|
| 68 |
+
|
| 69 |
+
def gradio_interface(description):
|
| 70 |
+
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}"
|
| 71 |
+
result = query_openai(prompt)
|
| 72 |
+
return result
|
| 73 |
+
|
| 74 |
+
iface = gr.Interface(
|
| 75 |
+
fn=gradio_interface,
|
| 76 |
+
inputs=gr.inputs.Textbox(lines=10, label="Student Description"),
|
| 77 |
+
outputs=gr.outputs.JSON(label="Extracted Student Information"),
|
| 78 |
+
title="Student Information Extractor",
|
| 79 |
+
description="Enter a student's description to extract structured information."
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
iface.launch()
|