Spaces:
Sleeping
Sleeping
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"] | |
def get_response(context, prompt): | |
return gpt_response | |
uploaded_file = st.file_uploader("Choose a file") | |
if uploaded_file is not None: | |
#image = Image.open(uploaded_file) | |
#st.image(image, caption='Sunrise by the mountains') | |
extractedInformation = pytesseract.image_to_string(image) | |
st.write(extractedInformation) | |
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}') | |