miqueiascoutinho commited on
Commit
1b22cf8
·
1 Parent(s): 1f5f8f5

Gerador de histórias a partir de uma imagem

Browse files
Files changed (3) hide show
  1. .gitignore +4 -0
  2. app.py +95 -0
  3. requirements.txt +3 -0
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ .env
2
+ *.jpg
3
+ *.wav
4
+ *.venv
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ from dotenv import load_dotenv
3
+ import requests
4
+ import os
5
+ import streamlit as st
6
+
7
+ # Adicionar o contexto de execução as secrets configuradas no arquivo .env
8
+ load_dotenv()
9
+
10
+ def gerar_contexto_imagem(input_imagem):
11
+
12
+ api_url = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-large"
13
+ access_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
14
+ headers = {"Authorization": f"Bearer {access_token}"}
15
+
16
+ with open(input_imagem, "rb") as f:
17
+ data = f.read()
18
+ response = requests.post(api_url, headers=headers, data=data).json()
19
+
20
+ return response[0]['generated_text']
21
+
22
+ def traduzir_texto(texto):
23
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
24
+ chat = client.chat.completions.create(
25
+ model="gpt-3.5-turbo-0125",
26
+ messages=[
27
+ {"role":"system", "content": "Traduza do inglês para o português"},
28
+ {"role": "user", "content": texto}
29
+ ])
30
+
31
+ return chat.choices[0].message.content
32
+
33
+ def gerar_historia(contexto):
34
+ llm_provider = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
35
+
36
+ historia = llm_provider.chat.completions.create(
37
+ model='gpt-3.5-turbo-0125',
38
+ max_tokens=300,
39
+ temperature=0.85,
40
+ messages=[
41
+ {"role": "system", "content":"Dado o contexto do usuário, crie uma história no idioma PORTIGUÊS. O texto deve entre 150 e 200 caracteres"},
42
+ {"role": "user", "content": contexto}
43
+ ]
44
+ )
45
+
46
+ return historia.choices[0].message.content
47
+
48
+ # text to speech
49
+ def gerar_audio(texto):
50
+ audio_name = 'historia.wav'
51
+ api_url = "https://api-inference.huggingface.co/models/facebook/mms-tts-por"
52
+ access_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
53
+ headers = {"Authorization": f"Bearer {access_token}"}
54
+ payload = {
55
+ "inputs": texto
56
+ }
57
+
58
+ response = requests.post(api_url, headers=headers, json=payload)
59
+
60
+ with open(audio_name, 'wb') as file:
61
+ file.write(response.content)
62
+
63
+ return audio_name
64
+
65
+ # APP / Interface gráfica
66
+ def main():
67
+ st.set_page_config(page_title="imagem para história em áudio")
68
+ st.header("Imagem para história em áudio")
69
+
70
+ upload_file = st.file_uploader("Selecione uma imagem", type="jpg")
71
+
72
+ if upload_file is not None:
73
+ print(upload_file)
74
+ imagem_data_bytes = upload_file.getvalue()
75
+
76
+ with open(upload_file.name, "wb") as file:
77
+ file.write(imagem_data_bytes)
78
+
79
+ st.image(upload_file, caption="Imagem / Fotografia", use_column_width=True)
80
+
81
+ contexto_em_ingles = gerar_contexto_imagem(upload_file.name)
82
+ contexto_traduzido = traduzir_texto(contexto_em_ingles)
83
+ historia = gerar_historia(contexto_traduzido)
84
+ historia_em_audio = gerar_audio(historia)
85
+
86
+ with st.expander("Contexto"):
87
+ st.write(contexto_traduzido)
88
+
89
+ with st.expander("Historia"):
90
+ st.write(historia)
91
+
92
+ st.audio(historia_em_audio)
93
+
94
+ if __name__ == '__main__':
95
+ main()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ python-dotenv==1.0.1
2
+ openai==1.13.3
3
+ streamlit==1.36.0