Spaces:
Runtime error
Runtime error
rev
Browse files
app.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import mediapy as media
|
3 |
+
import random
|
4 |
+
import sys
|
5 |
+
import torch
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
from rembg import remove
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
|
10 |
+
from typing import List
|
11 |
+
from langchain.output_parsers import PydanticOutputParser
|
12 |
+
from langchain_core.prompts import PromptTemplate
|
13 |
+
from langchain_core.pydantic_v1 import BaseModel, Field, validator
|
14 |
+
from getpass import getpass
|
15 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
16 |
+
from langchain.chains import LLMChain
|
17 |
+
from diffusers import DiffusionPipeline, TCDScheduler
|
18 |
+
from huggingface_hub import hf_hub_download
|
19 |
+
from PIL import Image
|
20 |
+
|
21 |
+
# set hf inference endpoint with lama for story
|
22 |
+
# get a token: https://huggingface.co/docs/api-inference/quicktour#get-your-api-token
|
23 |
+
|
24 |
+
|
25 |
+
load_dotenv()
|
26 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
27 |
+
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
|
28 |
+
|
29 |
+
# Récupérer la clé API à partir des variables d'environnement
|
30 |
+
API_KEY = os.getenv("API_KEY")
|
31 |
+
|
32 |
+
# Vérifier si la clé API est présente
|
33 |
+
if API_KEY is None:
|
34 |
+
st.error("La clé API n'est pas définie. Veuillez la définir dans le fichier .env.")
|
35 |
+
st.stop()
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
class Story(BaseModel):
|
41 |
+
title: str = Field(description="A captivating title for the story.")
|
42 |
+
characters: list[str] = Field(
|
43 |
+
description="""Important:no json format. Six elements mandatory, each formatted as:
|
44 |
+
"[Character Name], [comma-separated adjectives], cartoon, style africa, painting".
|
45 |
+
Describe each character's appearance in detail. Be creative!"""
|
46 |
+
)
|
47 |
+
scenes: list[str] = Field(
|
48 |
+
description="""Important:no json format.no json format. Six elements mandatory, each a string describing a character's action.very important:use charaters description apperance, use only action verbs.
|
49 |
+
Each scene must follow the previous one chronologically, creating a complete narrative when combined.
|
50 |
+
Develop your story by detailing what each character DOES in each scene.Instead to use only name of characters to write this part, use name and this key word 'painting bening style mushgot 'as description appearence, it's very import to do loke that.if it's a new characters in the story, instead use his name use his name and add the keyword 'painting benin style'it is mandatory.Use your imagination!"""
|
51 |
+
)
|
52 |
+
metadonne: list[str] = Field(
|
53 |
+
description="""Important: no json format.Six elements mandatory, each a concise one-sentence description of the corresponding scene in the 'scenes' field.
|
54 |
+
Explain the action taking place in each scene. Come up with your own unique descriptions!"""
|
55 |
+
)
|
56 |
+
|
57 |
+
|
58 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
59 |
+
|
60 |
+
llm = ChatGoogleGenerativeAI(model="gemini-pro",google_api_key=api_key)
|
61 |
+
model=llm
|
62 |
+
|
63 |
+
system="All instructions must be follow is very important, all story related to african culture and history is mandatory.You are a storyteller who specializes in creating educational tales about African culture. Your mission is to craft a narrative that teaches African children about their rich heritage. Your story is based on real events from the past, incorporating historical references, myths, and legends. story size is short length. Your narrative will be presented in six panels.Very important, For each panel, you will provide: A description of the characters, using precise and unique descriptions each time, ending with the keywords 'high quality', 'watercolor painting', 'painting Benin style', and 'mugshot', 'cartoon africa style' in the scenes or characters is mandatory.For description, using only words or groups of words separated by commas, without sentences. Each sentence in the panel's text should start with the character's name, and each sentence should be no longer than two small sentences. Each story has only three characters. Your story must always revolve around African legends and kingdoms, splitting the scenario into six parts. Be creative in each story"
|
64 |
+
|
65 |
+
st.title("Storytelling with AI")
|
66 |
+
# Create input zone
|
67 |
+
title = st.text_input("Discover a new story on africa, tape a topic !")
|
68 |
+
|
69 |
+
story_query=system+title
|
70 |
+
parser = PydanticOutputParser(pydantic_object=Story)
|
71 |
+
|
72 |
+
prompt = PromptTemplate(
|
73 |
+
template="Answer the user query.\n{format_instructions}\n{query}\n",
|
74 |
+
input_variables=["query"],
|
75 |
+
partial_variables={"format_instructions": parser.get_format_instructions()},
|
76 |
+
)
|
77 |
+
|
78 |
+
chain = prompt | model | parser
|
79 |
+
|
80 |
+
chain.invoke({"query": story_query})
|
81 |
+
|
82 |
+
response =chain.invoke({"query": story_query})
|
83 |
+
|
84 |
+
response
|
85 |
+
|
86 |
+
# modele load
|
87 |
+
# Choose among 1, 2, 4 and 8:
|
88 |
+
num_inference_steps = 8
|
89 |
+
|
90 |
+
import streamlit as st
|
91 |
+
import requests
|
92 |
+
import io
|
93 |
+
from PIL import Image
|
94 |
+
|
95 |
+
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
96 |
+
headers = {"Authorization": f"Bearer {API_KEY}"}
|
97 |
+
|
98 |
+
# Fonction pour appeler l'API et générer une image pour une scène donnée
|
99 |
+
def generate_image(scene):
|
100 |
+
payload = {
|
101 |
+
"inputs": scene,
|
102 |
+
"guidance_scale": 0.8,
|
103 |
+
"num_inference_steps": 8,
|
104 |
+
"eta": 0.5,
|
105 |
+
"seed": 46,
|
106 |
+
"negative_prompt": negative_prompt
|
107 |
+
}
|
108 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
109 |
+
image_bytes = response.content
|
110 |
+
image = Image.open(io.BytesIO(image_bytes))
|
111 |
+
return image
|
112 |
+
|
113 |
+
# Contenu de la variable response
|
114 |
+
scenes =response.scenes
|
115 |
+
|
116 |
+
metadonne =response.metadonne
|
117 |
+
# Générer les images pour chaque scène et afficher avec les métadonnées dans une grille 2x3
|
118 |
+
st.title("Images générées avec métadonnées dans une grille 2x3")
|
119 |
+
for i in range(0, len(scenes), 2):
|
120 |
+
col1, col2 = st.columns(2)
|
121 |
+
col1.write(f"**Scène {i+1}:** {metadonne[i]}")
|
122 |
+
col1.image(generate_image(scenes[i]), caption=f"Image de la scène {i+1}", width=300)
|
123 |
+
|
124 |
+
# Vérifie si une deuxième scène existe pour afficher la deuxième image
|
125 |
+
if i+1 < len(scenes):
|
126 |
+
col2.write(f"**Scène {i+2}:** {metadonne[i+1]}")
|
127 |
+
col2.image(generate_image(scenes[i+1]), caption=f"Image de la scène {i+2}", width=300)
|
128 |
+
|
129 |
+
|
130 |
+
|
131 |
+
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
|
136 |
+
|