fastai / app.py
drmurataltun's picture
Update app.py
2367c9c verified
import streamlit as st
from fastapi import FastAPI, Request
import requests
# FastAPI setup
app = FastAPI()
# Streamlit app
st.set_page_config(page_title="FastAPI-Streamlit App")
# Home page
st.title("Welcome to the FastAPI-Streamlit App")
st.write("This app demonstrates the integration of FastAPI and Streamlit.")
# FastAPI endpoint
@app.post("/predict")
async def predict(request: Request):
data = await request.json()
# Tahmin mantığınızı burada gerçekleştirin
prediction = "Your prediction result"
return {"prediction": prediction}
# Streamlit interface
if st.button("Make Prediction"):
with st.spinner("Making prediction..."):
try:
# FastAPI endpoint'ine Hugging Face Spaces üzerinden çağrı yapın
url = "https://huggingface.co/spaces/drmurataltun/fastai/predict" # URL'nizin doğru olduğundan emin olun
response = requests.post(url, json={})
response.raise_for_status() # HTTP hata kodları için kontrol
# Yanıtın içerik türünü kontrol edin
content_type = response.headers.get('Content-Type')
if 'application/json' not in content_type:
st.error(f"Unexpected content type: {content_type}")
else:
prediction = response.json().get("prediction", "No prediction found")
st.success(f"Prediction: {prediction}")
except requests.exceptions.RequestException as e:
st.error(f"An error occurred: {e}")
except ValueError as e:
st.error(f"Invalid JSON response: {e}")
st.write(response.text) # Yanıtın kendisini yazdırın
# Streamlit uygulamasının çalışması için FastAPI sunucusunu başlatma kodunu kaldırıyoruz.