KeviniveK commited on
Commit
de2ff7c
·
verified ·
1 Parent(s): 603ae29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -54
app.py CHANGED
@@ -1,56 +1,36 @@
1
- import streamlit as st
2
- from transformers import pipeline, AutoProcessor, AutoModel
3
- from scipy.io.wavfile import write as write_wav
4
- import numpy as np
5
  import torch
6
 
7
- def img2text(image_path):
8
- img2caption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
9
- return img2caption(image_path)[0]['generated_text']
10
-
11
- def text2story(text):
12
- pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
13
- story_text = pipe(text, max_length=100)[0]['generated_text']
14
- return story_text
15
-
16
- def text2audio(story_text):
17
- processor = AutoProcessor.from_pretrained("facebook/mms-tts-eng")
18
- model = AutoModel.from_pretrained("facebook/mms-tts-eng")
19
-
20
- inputs = processor(text=story_text, return_tensors="pt")
21
- with torch.no_grad():
22
- output = model(**inputs).waveform
23
-
24
- audio_array = output.cpu().numpy().squeeze()
25
- sample_rate = 16000
26
-
27
- return audio_array, sample_rate
28
-
29
- # Streamlit UI
30
- st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
31
- st.header("Turn Your Image to Audio Story")
32
- uploaded_file = st.file_uploader("Select an Image...")
33
-
34
- if uploaded_file is not None:
35
- bytes_data = uploaded_file.getvalue()
36
- with open(uploaded_file.name, "wb") as file:
37
- file.write(bytes_data)
38
- st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
39
-
40
- # Stage 1: Image to Text
41
- st.text('Processing img2text...')
42
- scenario = img2text(uploaded_file.name)
43
- st.write(scenario)
44
-
45
- # Stage 2: Text to Story
46
- st.text('Generating a story...')
47
- story = text2story(scenario)
48
- st.write(story)
49
-
50
- # Stage 3: Story to Audio
51
- st.text('Generating audio...')
52
- audio_array, sample_rate = text2audio(story)
53
-
54
- audio_file = "output_audio.wav"
55
- write_wav(audio_file, sample_rate, audio_array)
56
- st.audio(audio_file)
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ from diffusers import StableDiffusionPipeline
 
4
  import torch
5
 
6
+ # 文本生成模型:使用 GPT-2 或其他公开模型
7
+ text_generator = pipeline("text-generation", model="gpt2")
8
+
9
+ # 图像生成模型:使用 Stable Diffusion
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ image_generator = StableDiffusionPipeline.from_pretrained(
12
+ "runwayml/stable-diffusion-v1-5",
13
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
14
+ ).to(device)
15
+
16
+ def generate_text_and_image(keyword):
17
+ # Step 1: 根据关键词生成文本
18
+ prompt = f"Write a short paragraph about {keyword}."
19
+ text_output = text_generator(prompt, max_length=100, do_sample=True, temperature=0.8)[0]["generated_text"]
20
+
21
+ # Step 2: 使用生成的文本生成图像
22
+ image = image_generator(text_output).images[0]
23
+
24
+ return text_output, image
25
+
26
+ # Gradio界面
27
+ iface = gr.Interface(
28
+ fn=generate_text_and_image,
29
+ inputs=gr.Textbox(lines=1, placeholder="请输入关键词,例如:a futuristic city"),
30
+ outputs=[gr.Textbox(label="生成的文字"), gr.Image(label="根据文字生成的图像")],
31
+ title="关键词到文字 + 图像生成器",
32
+ description="输入关键词,先生成一段文字,再根据这段文字生成图像(使用Hugging Face模型)。"
33
+ )
34
+
35
+ if __name__ == "__main__":
36
+ iface.launch()