import streamlit as st import pytesseract from io import StringIO from PIL import Image import json import openai import pandas as pd openai.api_key = st.secrets["open_ai_key"] uploaded_file = st.file_uploader("Choose a file") if uploaded_file is not None: image = Image.open(uploaded_file) with st.expander('Uploaded image', expanded=False): st.image(image) with st.expander('Raw OCR', expanded=False): extractedInformation = pytesseract.image_to_string(image) st.write(extractedInformation) with st.expander('GPT cleaned output', expanded=True): context = '''parse the unstructured input of a cooking recipe into a valid JSON array of objects in the following format: [{ "recipe_name": "the name of the recipe expressed as a string", "recipe_ingredients": "list of ingredients specified in the recipe expressed as string", "recipe_steps": "list of steps to cook the recipe expressed as a string" }]''' gpt_response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": context}, {"role": "user", "content": extractedInformation} ], temperature=0.2, max_tokens=1000 ) st.json((json.loads(gpt_response['choices'][0]['message']['content']))) cost = gpt_response['usage']["prompt_tokens"]*(0.0015/1000) + gpt_response['usage']["completion_tokens"]*(0.002/1000) st.write(f'Cost for query was approx ${cost}')