vishash commited on
Commit
1cab52a
·
verified ·
1 Parent(s): d5c1983

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3
+ import torch
4
+ from diffusers import StableDiffusionPipeline
5
+
6
+ # Load the text model
7
+ @st.cache_resource
8
+ def load_text_model():
9
+ model_name = "meta-llama/Llama-2-7b-chat-hf"
10
+ bnb_config = BitsAndBytesConfig(
11
+ load_in_4bit=True,
12
+ bnb_4bit_compute_dtype=torch.float16,
13
+ bnb_4bit_use_double_quant=True
14
+ )
15
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
16
+ model = AutoModelForCausalLM.from_pretrained(
17
+ model_name,
18
+ quantization_config=bnb_config,
19
+ device_map="auto"
20
+ )
21
+ return pipeline("text-generation", model=model, tokenizer=tokenizer)
22
+
23
+ # Load the image model
24
+ @st.cache_resource
25
+ def load_image_model():
26
+ model_id = "runwayml/stable-diffusion-v1-5"
27
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
28
+ pipe.to("cuda") # Use GPU
29
+ return pipe
30
+
31
+ llama_pipe = load_text_model()
32
+ image_pipe = load_image_model()
33
+
34
+ # Streamlit UI
35
+ st.title("🚀 Blog & Image Generator")
36
+
37
+ topic = st.text_input("Enter a blog topic:", "The Future of AI in Healthcare")
38
+
39
+ if st.button("Generate Blog"):
40
+ with st.spinner("Generating Blog..."):
41
+ blog = llama_pipe(f"Write a detailed blog on {topic}:", max_length=800, truncation=True)[0]['generated_text']
42
+ st.subheader("Generated Blog")
43
+ st.write(blog)
44
+
45
+ # Extract first 3 lines for image keywords
46
+ keywords = " ".join(blog.split("\n")[:3])
47
+
48
+ # Generate Image
49
+ with st.spinner("Generating Image..."):
50
+ image = image_pipe(keywords).images[0]
51
+ st.subheader("Generated Image")
52
+ st.image(image, caption="AI-Generated Image", use_column_width=True)
53
+