Spaces:
Running
Running
import streamlit as st | |
import requests | |
from PIL import Image | |
import pytesseract | |
import os | |
api_key = os.environ.get("HFBearer") | |
# API URL and headers | |
API_URL = "https://pllfc7e5i0rujahy.us-east-1.aws.endpoints.huggingface.cloud" | |
headers = { | |
"Accept": "application/json", | |
"Authorization": api_key, # Replace with your actual token | |
"Content-Type": "application/json" | |
} | |
# Function to query the API | |
def query(payload): | |
response = requests.post(API_URL, headers=headers, json=payload) | |
return response.json() | |
# Function to extract text from image | |
def extract_text_from_image(image_path): | |
image = Image.open(image_path) | |
text = pytesseract.image_to_string(image) | |
return text | |
# Streamlit app layout | |
st.title("API Query App") | |
st.write("This app allows you to query the API and retrieve responses.") | |
user_input = """ | |
Extrais les paramètres suivants dans un json: | |
- Date de naissance | |
- Prénom | |
- Nom du patient | |
Dans ta réponse, le json (uniquement) doit apparaitre entre <JSON> et </JSON>. | |
Ne répond que par le json entre les balises, si les paramètres n'existent pas, laisse les champs vides. | |
Voici le texte qui contient les paramètres à extraire: | |
""" | |
# File uploader for the image | |
uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"]) | |
# Submit button | |
if st.button("Submit"): | |
if uploaded_image is not None: | |
with st.spinner("Extracting text from image..."): | |
# Extract text from the uploaded image | |
extracted_text = extract_text_from_image(uploaded_image) | |
st.write("Extracted text from image.") | |
with st.spinner("Fetching response from API..."): | |
# Query the API with user input | |
llm_input = user_input + extracted_text + "\n Donne uniquement le json entre balises, pas le texte:" | |
output = query({"inputs": llm_input, "parameters": {}}) | |
st.success("Response received!") | |
st.write(output) # Display the response | |