awacke1 commited on
Commit
9d717c2
1 Parent(s): 4ed4474

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -34
app.py CHANGED
@@ -1546,7 +1546,7 @@ def save_image_old2(image, filename):
1546
 
1547
  # Now filename length protected for linux and windows filename lengths
1548
  def save_image(image, filename):
1549
- max_filename_length = min(os.pathconf('/', 'PC_NAME_MAX'), 255, 260) # Linux: 255, Windows: 260 (including path)
1550
  filename_stem, extension = os.path.splitext(filename)
1551
  truncated_stem = filename_stem[:max_filename_length - len(extension)] if len(filename) > max_filename_length else filename_stem
1552
  filename = f"{truncated_stem}{extension}"
@@ -1557,7 +1557,15 @@ def save_image(image, filename):
1557
  def extract_boldface_terms(text):
1558
  return re.findall(r'\*\*(.*?)\*\*', text)
1559
 
1560
- def process_image(image_input):
 
 
 
 
 
 
 
 
1561
  if image_input:
1562
  st.markdown('Processing image: ' + image_input.name )
1563
  if image_input:
@@ -1567,7 +1575,7 @@ def process_image(image_input):
1567
  messages=[
1568
  {"role": "system", "content": "You are a helpful assistant that responds in Markdown."},
1569
  {"role": "user", "content": [
1570
- {"type": "text", "text": "Help me understand what is in this picture and list ten facts as markdown outline with appropriate emojis that describes what you see."},
1571
  {"type": "image_url", "image_url": {
1572
  "url": f"data:image/png;base64,{base64_image}"}
1573
  }
@@ -1589,9 +1597,10 @@ def process_image(image_input):
1589
  f.write(image_response)
1590
 
1591
  # Extract boldface terms from image_response then autoname save file
1592
- boldface_terms = extract_boldface_terms(image_response)
 
1593
  filename_stem, extension = os.path.splitext(image_input.name)
1594
- filename_img = f"{filename_stem}_{' '.join(boldface_terms)}{extension}"
1595
  newfilename = save_image(image_input, filename_img)
1596
  filename_md = newfilename.replace('.png', '.md')
1597
  create_file(filename_md, '', image_response, True)
@@ -1604,34 +1613,6 @@ def save_imageold(image_input, filename_txt):
1604
  f.write(image_input.getbuffer())
1605
  return image_input.name
1606
 
1607
- def process_imageold(image_input):
1608
- if image_input:
1609
- base64_image = base64.b64encode(image_input.read()).decode("utf-8")
1610
- response = client.chat.completions.create(
1611
- model=MODEL,
1612
- messages=[
1613
- {"role": "system", "content": "You are a helpful assistant that responds in Markdown."},
1614
- {"role": "user", "content": [
1615
- {"type": "text", "text": "Help me understand what is in this picture and list ten facts as markdown outline with appropriate emojis that describes what you see."},
1616
- {"type": "image_url", "image_url": {
1617
- "url": f"data:image/png;base64,{base64_image}"}
1618
- }
1619
- ]}
1620
- ],
1621
- temperature=0.0,
1622
- )
1623
- image_response = response.choices[0].message.content
1624
- st.markdown(image_response)
1625
-
1626
- filename_txt = generate_filename(image_response, "md") # Save markdown on image AI output from gpt4o
1627
- create_file(filename_txt, image_response, '', True) #create_file() # create_file() 3 required positional arguments: 'filename', 'prompt', and 'response'
1628
-
1629
- filename_txt = generate_filename(image_response, "png")
1630
- save_image(image_input, filename_txt) # Save copy of image with new filename
1631
- #st.rerun() # rerun to show new image and new markdown files
1632
-
1633
- return image_response
1634
-
1635
 
1636
  def process_audio(audio_input):
1637
  if audio_input:
@@ -1747,8 +1728,10 @@ def main():
1747
  if (text_input > ''):
1748
  textResponse = process_text(text_input)
1749
  elif option == "Image":
 
 
1750
  image_input = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
1751
- image_response = process_image(image_input)
1752
 
1753
 
1754
 
 
1546
 
1547
  # Now filename length protected for linux and windows filename lengths
1548
  def save_image(image, filename):
1549
+ max_filename_length = 250
1550
  filename_stem, extension = os.path.splitext(filename)
1551
  truncated_stem = filename_stem[:max_filename_length - len(extension)] if len(filename) > max_filename_length else filename_stem
1552
  filename = f"{truncated_stem}{extension}"
 
1557
  def extract_boldface_terms(text):
1558
  return re.findall(r'\*\*(.*?)\*\*', text)
1559
 
1560
+ def extract_title(text):
1561
+ boldface_terms = re.findall(r'\*\*(.*?)\*\*', text)
1562
+ if boldface_terms:
1563
+ title = ' '.join(boldface_terms)
1564
+ else:
1565
+ title = re.sub(r'[^a-zA-Z0-9_\-]', ' ', text[-200:])
1566
+ return title[-200:]
1567
+
1568
+ def process_image(image_input, user_prompt):
1569
  if image_input:
1570
  st.markdown('Processing image: ' + image_input.name )
1571
  if image_input:
 
1575
  messages=[
1576
  {"role": "system", "content": "You are a helpful assistant that responds in Markdown."},
1577
  {"role": "user", "content": [
1578
+ {"type": "text", "text": user_prompt},
1579
  {"type": "image_url", "image_url": {
1580
  "url": f"data:image/png;base64,{base64_image}"}
1581
  }
 
1597
  f.write(image_response)
1598
 
1599
  # Extract boldface terms from image_response then autoname save file
1600
+ #boldface_terms = extract_boldface_terms(image_response)
1601
+ boldface_terms = extract_title(image_response)
1602
  filename_stem, extension = os.path.splitext(image_input.name)
1603
+ filename_img = f"{filename_stem} {''.join(boldface_terms)}{extension}"
1604
  newfilename = save_image(image_input, filename_img)
1605
  filename_md = newfilename.replace('.png', '.md')
1606
  create_file(filename_md, '', image_response, True)
 
1613
  f.write(image_input.getbuffer())
1614
  return image_input.name
1615
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1616
 
1617
  def process_audio(audio_input):
1618
  if audio_input:
 
1728
  if (text_input > ''):
1729
  textResponse = process_text(text_input)
1730
  elif option == "Image":
1731
+ text = "Help me understand what is in this picture and list ten facts as markdown outline with appropriate emojis that describes what you see."
1732
+ text_input = st.text_input(label="Enter text prompt to use with Image context.", value=text)
1733
  image_input = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
1734
+ image_response = process_image(image_input, text_input)
1735
 
1736
 
1737