Saicharan21 commited on
Commit
1d3feef
·
verified ·
1 Parent(s): 98b4b31

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +17 -25
app.py CHANGED
@@ -97,32 +97,24 @@ def generate_image(prompt):
97
  if not prompt.strip(): return None, 'Please enter a description.'
98
  if not HF_TOKEN: return None, 'Error: Add HF_TOKEN to Space Settings Secrets.'
99
  try:
100
- # Add biomedical context to prompt
101
- full_prompt = 'Highly detailed scientific biomedical illustration of: ' + prompt + ', professional medical diagram, high quality, detailed, photorealistic'
102
- headers = {'Authorization': 'Bearer ' + HF_TOKEN}
103
- payload = {'inputs': full_prompt}
104
- # Use FLUX.1-schnell - best free model on HuggingFace
105
- r = requests.post(
106
- 'https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell',
107
- headers=headers, json=payload, timeout=60
108
- )
109
- if r.status_code == 200:
110
- img = Image.open(io.BytesIO(r.content))
111
- return img, 'Image generated successfully!'
112
- elif r.status_code == 503:
113
- # Model loading - try stable diffusion
114
- r2 = requests.post(
115
- 'https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0',
116
- headers=headers, json=payload, timeout=60
117
- )
118
- if r2.status_code == 200:
119
- img = Image.open(io.BytesIO(r2.content))
120
- return img, 'Image generated!'
121
- return None, 'Model loading, please wait 30 seconds and try again.'
122
- else:
123
- return None, 'Error: '+str(r.status_code)+' '+r.text[:200]
124
  except Exception as e:
125
- return None, 'Error: '+str(e)
126
 
127
  def piv_tool(velocity, shear, hr):
128
  v = 'HIGH - stenosis risk' if float(velocity)>2.0 else 'NORMAL'
 
97
  if not prompt.strip(): return None, 'Please enter a description.'
98
  if not HF_TOKEN: return None, 'Error: Add HF_TOKEN to Space Settings Secrets.'
99
  try:
100
+ full_prompt = 'Highly detailed scientific biomedical illustration of: ' + prompt + ', professional medical diagram, high quality, detailed'
101
+ headers = {'Authorization': 'Bearer ' + HF_TOKEN, 'Content-Type': 'application/json'}
102
+ payload = {'inputs': full_prompt, 'parameters': {'num_inference_steps': 4}}
103
+ models = [
104
+ 'https://router.huggingface.co/hf-inference/models/black-forest-labs/FLUX.1-schnell',
105
+ 'https://router.huggingface.co/hf-inference/models/stabilityai/stable-diffusion-xl-base-1.0',
106
+ 'https://router.huggingface.co/hf-inference/models/runwayml/stable-diffusion-v1-5',
107
+ ]
108
+ for model_url in models:
109
+ try:
110
+ r = requests.post(model_url, headers=headers, json=payload, timeout=60)
111
+ if r.status_code == 200:
112
+ img = Image.open(io.BytesIO(r.content))
113
+ return img, 'Image generated!'
114
+ except: continue
115
+ return None, 'All models busy. Please wait 30 seconds and try again.'
 
 
 
 
 
 
 
 
116
  except Exception as e:
117
+ return None, 'Error: ' + str(e)
118
 
119
  def piv_tool(velocity, shear, hr):
120
  v = 'HIGH - stenosis risk' if float(velocity)>2.0 else 'NORMAL'