Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from huggingface_hub import hf
|
| 4 |
+
|
| 5 |
+
# Retrieve your API key from the Secrets Manager
|
| 6 |
+
api_key = hf.secrets.get("testing")
|
| 7 |
+
|
| 8 |
+
API_URL_SEMANTICS = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-large"
|
| 9 |
+
API_URL_CAPTION = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
| 10 |
+
|
| 11 |
+
headers = {"Authorization": f"Bearer api_key"}
|
| 12 |
+
st.set_page_config(page_title="Instagram Post Improvement")
|
| 13 |
+
def generateSemantic(file):
|
| 14 |
+
response = requests.post(API_URL_SEMANTICS, headers=headers, data=file)
|
| 15 |
+
return response.json()[0]['generated_text']
|
| 16 |
+
|
| 17 |
+
def generateCaption(payload):
|
| 18 |
+
response = requests.post(API_URL_CAPTION , headers=headers, json=payload)
|
| 19 |
+
return response.json()[0]['generated_text']
|
| 20 |
+
|
| 21 |
+
st.title("Create an Eye-Catching Instagram Post 📸✨")
|
| 22 |
+
st.write(""" 🌟 This project utilizes two free pre-trained models from Hugging Face to enhance the engagement and attractiveness of your Instagram posts for your followers. It accomplishes this through two steps:
|
| 23 |
+
|
| 24 |
+
1-🚀 Capturing the semantics of an image.
|
| 25 |
+
|
| 26 |
+
2-🎀 Transforming the captured semantics into an appealing Instagram post. """)
|
| 27 |
+
st.sidebar.title('About app')
|
| 28 |
+
st.sidebar.info(
|
| 29 |
+
"This is a Streamlit application created by Gasbaoui Mohammed el Amin.\n"
|
| 30 |
+
"It demonstrates how to interact with pre-trained model hagging face."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
file=st.file_uploader("upload an image",type=["jpg","jpeg","png"])
|
| 34 |
+
if file:
|
| 35 |
+
col1,col2=st.columns(2)
|
| 36 |
+
with col1:
|
| 37 |
+
st.image(file,use_column_width=True)
|
| 38 |
+
with col2:
|
| 39 |
+
|
| 40 |
+
with st.spinner("Generating semantics..."):
|
| 41 |
+
outputSemantic=generateSemantic(file)
|
| 42 |
+
st.subheader("Output Semantic")
|
| 43 |
+
st.markdown(
|
| 44 |
+
"""
|
| 45 |
+
<style>
|
| 46 |
+
/* Style for the container */
|
| 47 |
+
.fancy-text {
|
| 48 |
+
padding: 10px;
|
| 49 |
+
border-radius: 10px; /* Make edges curved */
|
| 50 |
+
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); /* Add box shadow */
|
| 51 |
+
border: 2px solid #ccc; /* Add a border */
|
| 52 |
+
}
|
| 53 |
+
</style>
|
| 54 |
+
""",
|
| 55 |
+
unsafe_allow_html=True
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Now use the styled container for your text
|
| 59 |
+
st.markdown(f'<div class="fancy-text">{outputSemantic}</div>',
|
| 60 |
+
unsafe_allow_html=True)
|
| 61 |
+
|
| 62 |
+
with st.spinner("Generating caption..."):
|
| 63 |
+
promptDictionary={
|
| 64 |
+
"inputs": f"convert the following image semantics '{outputSemantic}' "
|
| 65 |
+
f"to an instagram caption make sure to add hashtags and emojis.,"
|
| 66 |
+
f"Answer: ",
|
| 67 |
+
|
| 68 |
+
}
|
| 69 |
+
st.subheader("Caption")
|
| 70 |
+
outputCaption=generateCaption(promptDictionary)
|
| 71 |
+
result=outputCaption.split("Answer: ")[1]
|
| 72 |
+
st.markdown(f'<div class="fancy-text">{result}</div>',
|
| 73 |
+
unsafe_allow_html=True)
|
| 74 |
+
|