Spaces:
Paused
Paused
Update app/main.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546from fastapi import FastAPIfrom pydantic import BaseModelfrom huggingface_hub import InferenceClientimport osimport gradio as gr# ----- Hugging Face token and model -----HF_TOKEN = os.environ.get("HF_TOKEN")HF_MODEL = "google/flan-t5-small"client = InferenceClient(token=HF_TOKEN)# ----- FastAPI app (optional, for health checks) -----app = FastAPI()@app.get("/")def home():return {"status": "ok", "message": "Relay is running!"}# ----- FastAPI relay endpoint (optional) -----class Query(BaseModel):inputs: str@app.post("/predict")async def predict(query: Query):if not HF_TOKEN:return {"error": "HF_TOKEN not set"}result = client.text_generation(query.inputs)return {"result": result}# ----- Gradio wrapper for Hugging Face Space -----def relay_gradio(text: str) -> str:"""Function called by Gradio; returns model output."""output = client.text_generation(text)return outputiface = gr.Interface(fn=relay_model,inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),outputs=gr.Textbox(label="Model output"),title="HF Inference Relay",description="Students type text and get outputs from Google Flan-T5-Small",allow_flagging=False # <- disable flagging to avoid permission errors)# Launch Gradio app for the Spaceiface.launch(server_name="0.0.0.0", server_port=7860)1234567891011121314151617181920212223242526272829import osfrom huggingface_hub import InferenceClientimport gradio as gr# ----- Hugging Face token & model -----HF_TOKEN = os.environ.get("HF_TOKEN")HF_MODEL = "google/flan-t5-small"client = InferenceClient(token=HF_TOKEN)# ----- Relay function for Gradio -----def relay_model(text: str) -> str:"""Send text to HF Inference API and return output."""if not HF_TOKEN:return "HF_TOKEN not set!"output = client.text_generation(text)return output# ----- Gradio interface -----iface = gr.Interface(fn=relay_model,inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),outputs=gr.Textbox(label="Model output"),title="HF Inference Relay",description="Students type text and get outputs from Google Flan-T5-Small",allow_flagging=False # Disable flagging to avoid permission errors)# Launch the Gradio app (Spaces automatically maps port 7860)iface.launch(server_name="0.0.0.0", server_port=7860)
app/main.py+9-26