Spaces:
Sleeping
Sleeping
File size: 1,601 Bytes
4ddaa8e 2575900 cc5676d 4ddaa8e 2575900 79a6f44 641d571 63107d9 641d571 cc5676d |
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 |
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}')
|