File size: 1,157 Bytes
eadc45f
e1ca57e
eadc45f
e1ca57e
 
 
 
 
 
 
 
 
eadc45f
e1ca57e
 
 
 
 
 
 
 
 
 
 
 
 
 
778fa62
e1ca57e
 
 
 
 
778fa62
 
eadc45f
be63571
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
import gradio as gr
from transformers import pipeline

# --- Model Loading ---
MODEL_ID = "MadhavRupala/SD15-ControlNet" 
print(f"Loading model '{MODEL_ID}' on CPU...")
try:
    pipe = pipeline("text2text-generation", model=MODEL_ID, device=-1)
    print("✅ Model loaded successfully!")
except Exception as e:
    print(f"❌ Error loading model: {e}")
    pipe = None

# --- Inference Function ---
# The parameter name 'prompt' is important for the client.
def run_inference(prompt: str):
    if pipe is None:
        return "Error: The model could not be loaded."
    if not prompt:
        return "Please enter a prompt."
    try:
        result = pipe(prompt, max_length=100)
        return result[0]['generated_text']
    except Exception as e:
        return f"An error occurred during inference: {e}"

# --- Gradio Interface ---
iface = gr.Interface(
    fn=run_inference,
    title=f"🤖 Self-Hosted Model API: `{MODEL_ID}`",
    description="This Space hosts a model for unlimited access.",
    inputs=gr.Textbox(label="Your Prompt", lines=4),
    outputs=gr.Textbox(label="Model Response", lines=4),
    allow_flagging="never"
)

iface.launch()