File size: 1,703 Bytes
52f0235
51b7502
52f0235
 
 
 
51b7502
 
 
52f0235
 
51b7502
 
 
 
52f0235
51b7502
52f0235
 
51b7502
 
 
52f0235
 
 
 
 
 
51b7502
 
52f0235
 
51b7502
52f0235
51b7502
 
 
52f0235
 
51b7502
 
 
 
52f0235
 
51b7502
 
 
 
52f0235
51b7502
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import torch
import gradio as gr
from groq import Groq
from diffusers import AutoPipelineForText2Image

# ----------------------------------------------------
# 1. GROQ CHATBOT  (uses HF Secret: GROQ_API_KEY)
# ----------------------------------------------------
client = Groq(api_key=os.getenv("GROQ_API_KEY"))

def chat_with_groq(text):
    completion = client.chat.completions.create(
        model="llama-3.1-8b-instant",
        messages=[{"role": "user", "content": text}]
    )
    return completion.choices[0].message.content


# ----------------------------------------------------
# 2. CPU IMAGE GENERATOR (uses SD-Turbo, CPU friendly)
# ----------------------------------------------------
pipe = AutoPipelineForText2Image.from_pretrained(
    "stabilityai/sd-turbo",
    torch_dtype=torch.float32
).to("cpu")

def generate_image(prompt):
    img = pipe(prompt, num_inference_steps=6).images[0]
    return img


# ----------------------------------------------------
# 3. GRADIO MULTI-TAB UI
# ----------------------------------------------------
with gr.Blocks() as app:
    gr.Markdown("## 🤖 Groq Chatbot + 🎨 Image Generator (CPU Version)")

    with gr.Tab("💬 Chatbot"):
        in_txt = gr.Textbox(label="Enter text", lines=4)
        out_txt = gr.Textbox(label="Response", lines=10)
        btn_chat = gr.Button("Send")
        btn_chat.click(chat_with_groq, inputs=in_txt, outputs=out_txt)

    with gr.Tab("🎨 Image Generator"):
        prompt = gr.Textbox(label="Enter a prompt")
        img_out = gr.Image(label="Generated Image")
        btn_img = gr.Button("Generate Image")
        btn_img.click(generate_image, inputs=prompt, outputs=img_out)

app.launch()