kingmadhu1 commited on
Commit
d50bc01
Β·
verified Β·
1 Parent(s): e11bc62

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
3
+ from diffusers import StableDiffusionPipeline
4
+ from TTS.api import TTS
5
+ import gradio as gr
6
+ from PIL import Image
7
+
8
+ # Load Text Generation Model (phi-2)
9
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2")
10
+ text_model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2", torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
11
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
+ text_model.to(device)
13
+
14
+ def generate_text(prompt):
15
+ inputs = tokenizer(prompt, return_tensors="pt").to(device)
16
+ outputs = text_model.generate(**inputs, max_length=100)
17
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
18
+
19
+ # Load Image Generation Model (Stable Diffusion)
20
+ pipe = StableDiffusionPipeline.from_pretrained(
21
+ "runwayml/stable-diffusion-v1-5",
22
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
23
+ revision="fp16" if torch.cuda.is_available() else None,
24
+ )
25
+ pipe = pipe.to(device)
26
+
27
+ def generate_image(prompt):
28
+ image = pipe(prompt).images[0]
29
+ return image
30
+
31
+ # Load TTS Model (Coqui TTS)
32
+ tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False)
33
+
34
+ def generate_audio(prompt):
35
+ output_path = "output.wav"
36
+ tts.tts_to_file(text=prompt, file_path=output_path)
37
+ return output_path
38
+
39
+ # Gradio Interface
40
+ with gr.Blocks(title="Charriot_v2") as demo:
41
+ gr.Markdown("# πŸ€– Charriot_v2: Text, Image, and Audio Generator")
42
+
43
+ with gr.Tab("πŸ“ Text Generation"):
44
+ text_input = gr.Textbox(label="Enter Prompt")
45
+ text_output = gr.Textbox(label="Generated Text")
46
+ text_btn = gr.Button("Generate Text")
47
+ text_btn.click(fn=generate_text, inputs=text_input, outputs=text_output)
48
+
49
+ with gr.Tab("🎨 Image Generation"):
50
+ img_prompt = gr.Textbox(label="Enter Prompt")
51
+ img_output = gr.Image(label="Generated Image")
52
+ img_btn = gr.Button("Generate Image")
53
+ img_btn.click(fn=generate_image, inputs=img_prompt, outputs=img_output)
54
+
55
+ with gr.Tab("πŸ”Š Audio Generation"):
56
+ audio_prompt = gr.Textbox(label="Enter Prompt")
57
+ audio_output = gr.Audio(label="Generated Audio")
58
+ audio_btn = gr.Button("Generate Audio")
59
+ audio_btn.click(fn=generate_audio, inputs=audio_prompt, outputs=audio_output)
60
+
61
+ demo.launch()