Saicharan21 commited on
Commit
883ca29
·
verified ·
1 Parent(s): 37dfba3

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -94,22 +94,27 @@ def voice_chat(audio, history):
94
  return history
95
 
96
  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
- # Step 1 - Use Groq to enhance prompt for accuracy
101
  enhanced = prompt
 
102
  if GROQ_KEY:
103
  try:
104
  client = Groq(api_key=GROQ_KEY)
105
  msgs = [
106
- {'role':'system','content':'You are an expert at writing image generation prompts for biomedical and engineering topics. Convert the user request into a highly detailed, specific image generation prompt. Be very specific about colors, materials, labels, dimensions, and visual style. Always say: photorealistic, highly detailed, scientific illustration, white background, labeled diagram, professional medical illustration.'},
107
- {'role':'user','content':'Create an image generation prompt for: ' + prompt + '. Context: SJSU CardioLab uses 27mm SJM Regent bileaflet mechanical heart valves, Sylgard 184 transparent silicone, green laser PIV system, Arduino Uno with stepper motor for TGT, Whatman paper microfluidic uPAD devices for CKD creatinine detection using Jaffe reaction orange-red color.'}
108
  ]
109
- resp = client.chat.completions.create(model='llama-3.3-70b-versatile', messages=msgs, max_tokens=200)
110
- enhanced = resp.choices[0].message.content
111
- except: pass
112
- # Step 2 - Send enhanced prompt to FLUX
 
 
 
 
 
113
  headers = {'Authorization': 'Bearer ' + HF_TOKEN, 'Content-Type': 'application/json'}
114
  payload = {'inputs': enhanced, 'parameters': {'num_inference_steps': 8, 'guidance_scale': 7.5}}
115
  models = [
@@ -121,11 +126,11 @@ def generate_image(prompt):
121
  r = requests.post(model_url, headers=headers, json=payload, timeout=60)
122
  if r.status_code == 200:
123
  img = Image.open(io.BytesIO(r.content))
124
- return img, 'Generated! Enhanced prompt: ' + enhanced[:150] + '...'
125
  except: continue
126
- return None, 'Models busy. Please try again in 30 seconds.'
127
  except Exception as e:
128
- return None, 'Error: ' + str(e)
129
 
130
  def piv_tool(velocity, shear, hr):
131
  v = 'HIGH - stenosis risk' if float(velocity)>2.0 else 'NORMAL'
@@ -185,8 +190,9 @@ with gr.Blocks(title='CardioLab AI', css=CSS) as demo:
185
  with gr.Column(scale=1):
186
  img_btn = gr.Button('Generate Image', variant='primary')
187
  img_status = gr.Textbox(label='Status', lines=2)
 
188
  img_output = gr.Image(label='Generated Image', type='pil', height=500)
189
- img_btn.click(generate_image, inputs=img_prompt, outputs=[img_output, img_status])
190
  gr.Markdown('**Try:** `27mm bileaflet mechanical heart valve` | `microfluidic paper device for CKD testing` | `blood flow visualization PIV` | `Arduino circuit with stepper motor`')
191
  with gr.Tab('PIV'):
192
  with gr.Row():
 
94
  return history
95
 
96
  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
  enhanced = prompt
101
+ description = ''
102
  if GROQ_KEY:
103
  try:
104
  client = Groq(api_key=GROQ_KEY)
105
  msgs = [
106
+ {'role':'system','content':'You are a biomedical visualization expert for SJSU CardioLab. Do two things: 1) Write a clear 2-3 sentence description of what the image will show. 2) Write a detailed image generation prompt. Format your response as: DESCRIPTION: [your description here] PROMPT: [your detailed prompt here]'},
107
+ {'role':'user','content':'Create image for: ' + prompt + '. CardioLab context: 27mm SJM Regent bileaflet mechanical heart valve, Sylgard 184 transparent silicone MCL, green laser PIV system, Arduino Uno stepper motor TGT, Whatman paper uPAD microfluidic device, Jaffe reaction orange-red color for CKD creatinine detection.'}
108
  ]
109
+ resp = client.chat.completions.create(model='llama-3.3-70b-versatile', messages=msgs, max_tokens=300)
110
+ full_resp = resp.choices[0].message.content
111
+ if 'DESCRIPTION:' in full_resp and 'PROMPT:' in full_resp:
112
+ description = full_resp.split('DESCRIPTION:')[1].split('PROMPT:')[0].strip()
113
+ enhanced = full_resp.split('PROMPT:')[1].strip()
114
+ else:
115
+ description = full_resp[:200]
116
+ enhanced = 'Highly detailed scientific biomedical illustration: ' + prompt + ', professional medical diagram, photorealistic, high quality, labeled'
117
+ except: enhanced = prompt
118
  headers = {'Authorization': 'Bearer ' + HF_TOKEN, 'Content-Type': 'application/json'}
119
  payload = {'inputs': enhanced, 'parameters': {'num_inference_steps': 8, 'guidance_scale': 7.5}}
120
  models = [
 
126
  r = requests.post(model_url, headers=headers, json=payload, timeout=60)
127
  if r.status_code == 200:
128
  img = Image.open(io.BytesIO(r.content))
129
+ return img, 'Image generated successfully!', description
130
  except: continue
131
+ return None, 'Models busy. Try again in 30 seconds.', description
132
  except Exception as e:
133
+ return None, 'Error: ' + str(e), ''
134
 
135
  def piv_tool(velocity, shear, hr):
136
  v = 'HIGH - stenosis risk' if float(velocity)>2.0 else 'NORMAL'
 
190
  with gr.Column(scale=1):
191
  img_btn = gr.Button('Generate Image', variant='primary')
192
  img_status = gr.Textbox(label='Status', lines=2)
193
+ img_desc = gr.Textbox(label='AI Description - What will be generated', lines=3, interactive=False)
194
  img_output = gr.Image(label='Generated Image', type='pil', height=500)
195
+ img_btn.click(generate_image, inputs=img_prompt, outputs=[img_output, img_status, img_desc])
196
  gr.Markdown('**Try:** `27mm bileaflet mechanical heart valve` | `microfluidic paper device for CKD testing` | `blood flow visualization PIV` | `Arduino circuit with stepper motor`')
197
  with gr.Tab('PIV'):
198
  with gr.Row():