Spaces:
Build error
Build error
Upload Untitled1.ipynb
Browse files- Untitled1.ipynb +81 -0
Untitled1.ipynb
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nbformat": 4,
|
| 3 |
+
"nbformat_minor": 0,
|
| 4 |
+
"metadata": {
|
| 5 |
+
"colab": {
|
| 6 |
+
"provenance": []
|
| 7 |
+
},
|
| 8 |
+
"kernelspec": {
|
| 9 |
+
"name": "python3",
|
| 10 |
+
"display_name": "Python 3"
|
| 11 |
+
},
|
| 12 |
+
"language_info": {
|
| 13 |
+
"name": "python"
|
| 14 |
+
}
|
| 15 |
+
},
|
| 16 |
+
"cells": [
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"execution_count": null,
|
| 20 |
+
"metadata": {
|
| 21 |
+
"id": "Dn6Ey7mV7-YW"
|
| 22 |
+
},
|
| 23 |
+
"outputs": [],
|
| 24 |
+
"source": [
|
| 25 |
+
"# import part\n",
|
| 26 |
+
"!pip install streamlit pyngrok\n",
|
| 27 |
+
"import streamlit as st\n",
|
| 28 |
+
"from transformers import pipeline\n",
|
| 29 |
+
"from PIL import Image\n",
|
| 30 |
+
"import io\n",
|
| 31 |
+
"\n",
|
| 32 |
+
"\n",
|
| 33 |
+
"# function part\n",
|
| 34 |
+
"def generate_image_caption(image):\n",
|
| 35 |
+
" \"\"\"Generates a caption for the given image using a pre-trained model.\"\"\"\n",
|
| 36 |
+
" img2caption = pipeline(\"image-to-text\", model=\"Salesforce/blip-image-captioning-base\")\n",
|
| 37 |
+
" result = img2caption(image)\n",
|
| 38 |
+
" return result[0]['generated_text']\n",
|
| 39 |
+
"\n",
|
| 40 |
+
"def text2story(text):\n",
|
| 41 |
+
" \"\"\"Generates a children's story from text input with genre adaptation\"\"\"\n",
|
| 42 |
+
" story_prompt = f\"Create a funny 100-word story for 8-year-olds about: {text}. Include: \"\n",
|
| 43 |
+
" story_prompt += \"1) A silly character 2) Magical object 3) Sound effects 4) Happy ending\"\n",
|
| 44 |
+
"\n",
|
| 45 |
+
" pipe = pipeline(\"text-generation\", model=\"pranavpsv/genre-story-generator-v2\")\n",
|
| 46 |
+
" story_text = pipe(\n",
|
| 47 |
+
" story_prompt,\n",
|
| 48 |
+
" max_new_tokens=200,\n",
|
| 49 |
+
" temperature=0.9,\n",
|
| 50 |
+
" top_k=50\n",
|
| 51 |
+
" )[0]['generated_text']\n",
|
| 52 |
+
" return story_text.split(\"Happy ending\")[-1].strip() # Clean output\n",
|
| 53 |
+
"\n",
|
| 54 |
+
"def main():\n",
|
| 55 |
+
" st.title(\"📖 Image Story Generator\")\n",
|
| 56 |
+
" st.write(\"Upload an image and get a magical children's story!\")\n",
|
| 57 |
+
"\n",
|
| 58 |
+
" uploaded_image = st.file_uploader(\"Choose an image...\", type=[\"jpg\", \"jpeg\", \"png\"])\n",
|
| 59 |
+
"\n",
|
| 60 |
+
" if uploaded_image:\n",
|
| 61 |
+
" image = Image.open(uploaded_image).convert(\"RGB\")\n",
|
| 62 |
+
" st.image(image, use_column_width=True)\n",
|
| 63 |
+
"\n",
|
| 64 |
+
" with st.spinner(\"✨ Analyzing image...\"):\n",
|
| 65 |
+
" caption = generate_image_caption(image)\n",
|
| 66 |
+
"\n",
|
| 67 |
+
" st.subheader(\"Image Understanding\")\n",
|
| 68 |
+
" st.write(caption)\n",
|
| 69 |
+
"\n",
|
| 70 |
+
" with st.spinner(\"📖 Writing story...\"):\n",
|
| 71 |
+
" story = text2story(caption)\n",
|
| 72 |
+
"\n",
|
| 73 |
+
" st.subheader(\"Magical Story\")\n",
|
| 74 |
+
" st.write(story)\n",
|
| 75 |
+
"\n",
|
| 76 |
+
"if __name__ == \"__main__\":\n",
|
| 77 |
+
" main()"
|
| 78 |
+
]
|
| 79 |
+
}
|
| 80 |
+
]
|
| 81 |
+
}
|