Spaces:
Runtime error

Shankarm08 commited on
Commit
fcb2e2e
Β·
verified Β·
1 Parent(s): 1eb5cb6
Files changed (1) hide show
  1. app.py +27 -47
app.py CHANGED
@@ -2,83 +2,63 @@ import torch
2
  import gradio as gr
3
  from diffusers import AutoPipelineForText2Image
4
  from transformers import pipeline
5
- import json
6
 
7
  # ---------- DEVICE ----------
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
 
10
- # ---------- TEXT MODEL (SAFE + FAST) ----------
11
  text_model = pipeline(
12
  "text-generation",
13
- model="google/flan-t5-small"
14
  )
15
 
16
- # ---------- IMAGE MODEL (FAST SDXL TURBO) ----------
17
  pipe = AutoPipelineForText2Image.from_pretrained(
18
  "stabilityai/sdxl-turbo",
19
  torch_dtype=torch.float16 if device == "cuda" else torch.float32
20
  )
21
  pipe = pipe.to(device)
22
 
23
- # ---------- DECISION (AGENT) ----------
24
- def decide_action(user_input):
25
- prompt = f"""
26
- Decide action:
27
- - If user wants image β†’ return JSON: {{"action":"image"}}
28
- - Otherwise β†’ return JSON: {{"action":"text"}}
29
-
30
- User: {user_input}
31
- """
32
-
33
- try:
34
- result = text_model(prompt, max_length=50)[0]["generated_text"]
35
- decision = json.loads(result)
36
- return decision.get("action", "text")
37
- except:
38
- return "text"
39
-
40
- # ---------- TOOLS ----------
41
- def text_tool(prompt):
42
- result = text_model(prompt, max_length=200)
43
- return result[0]["generated_text"]
44
-
45
- def image_tool(prompt):
46
- image = pipe(
47
- prompt,
48
- num_inference_steps=2,
49
- guidance_scale=0.0
50
- ).images[0]
51
- return image
52
-
53
- # ---------- AGENT ----------
54
  def agent(user_input):
55
- action = decide_action(user_input)
56
-
57
- if action == "image":
58
- img = image_tool(user_input)
59
- return "πŸ–ΌοΈ Image generated", img
 
 
 
60
  else:
61
- text = text_tool(user_input)
62
- return text, None
63
 
64
- # ---------- UI ----------
65
  def chat(user_input, history):
66
  text, image = agent(user_input)
67
- history = history + [(user_input, text)]
 
 
 
 
 
68
  return history, image
69
 
 
70
  with gr.Blocks() as demo:
71
  gr.Markdown("# πŸ€– Agentic AI (Text + Image)")
72
 
73
- chatbot = gr.Chatbot()
74
  image_output = gr.Image(label="Generated Image")
75
 
76
  with gr.Row():
77
- inp = gr.Textbox(placeholder="Ask anything or generate image...")
78
  btn = gr.Button("Send")
79
 
80
  state = gr.State([])
81
 
82
- btn.click(chat, [inp, state], [chatbot, image_output])
 
 
83
 
84
  demo.launch()
 
2
  import gradio as gr
3
  from diffusers import AutoPipelineForText2Image
4
  from transformers import pipeline
 
5
 
6
  # ---------- DEVICE ----------
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
 
9
+ # ---------- TEXT MODEL (STABLE) ----------
10
  text_model = pipeline(
11
  "text-generation",
12
+ model="distilgpt2"
13
  )
14
 
15
+ # ---------- IMAGE MODEL (FAST + LIGHT) ----------
16
  pipe = AutoPipelineForText2Image.from_pretrained(
17
  "stabilityai/sdxl-turbo",
18
  torch_dtype=torch.float16 if device == "cuda" else torch.float32
19
  )
20
  pipe = pipe.to(device)
21
 
22
+ # ---------- SIMPLE AGENT ----------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  def agent(user_input):
24
+ # simple rule-based routing (stable)
25
+ if any(word in user_input.lower() for word in ["draw", "image", "generate", "picture"]):
26
+ image = pipe(
27
+ user_input,
28
+ num_inference_steps=2,
29
+ guidance_scale=0.0
30
+ ).images[0]
31
+ return "πŸ–ΌοΈ Image generated", image
32
  else:
33
+ result = text_model(user_input, max_length=100)
34
+ return result[0]["generated_text"], None
35
 
36
+ # ---------- CHAT FUNCTION ----------
37
  def chat(user_input, history):
38
  text, image = agent(user_input)
39
+
40
+ history = history + [
41
+ {"role": "user", "content": user_input},
42
+ {"role": "assistant", "content": text}
43
+ ]
44
+
45
  return history, image
46
 
47
+ # ---------- UI ----------
48
  with gr.Blocks() as demo:
49
  gr.Markdown("# πŸ€– Agentic AI (Text + Image)")
50
 
51
+ chatbot = gr.Chatbot(type="messages")
52
  image_output = gr.Image(label="Generated Image")
53
 
54
  with gr.Row():
55
+ inp = gr.Textbox(placeholder="Ask or generate image...")
56
  btn = gr.Button("Send")
57
 
58
  state = gr.State([])
59
 
60
+ btn.click(chat, [inp, state], [chatbot, image_output]).then(
61
+ lambda: "", None, inp
62
+ )
63
 
64
  demo.launch()