Shankarm08 commited on
Commit
1eb5cb6
Β·
verified Β·
1 Parent(s): 5230b38
Files changed (1) hide show
  1. app.py +22 -20
app.py CHANGED
@@ -4,48 +4,50 @@ from diffusers import AutoPipelineForText2Image
4
  from transformers import pipeline
5
  import json
6
 
7
- # ---------- TEXT MODEL ----------
 
 
 
8
  text_model = pipeline(
9
- "text2text-generation",
10
- model="google/flan-t5-base"
11
  )
12
 
13
- # ---------- IMAGE MODEL (FAST) ----------
14
- device = "cuda" if torch.cuda.is_available() else "cpu"
15
-
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
- # ---------- DECISION MODEL ----------
23
  def decide_action(user_input):
24
  prompt = f"""
25
- Classify the user request.
26
-
27
- Rules:
28
- - If user wants image β†’ return JSON: {{"action": "image"}}
29
- - Otherwise β†’ return JSON: {{"action": "text"}}
30
 
31
  User: {user_input}
32
  """
33
 
34
- response = text_model(prompt, max_length=50)[0]["generated_text"]
35
-
36
  try:
37
- decision = json.loads(response)
 
38
  return decision.get("action", "text")
39
  except:
40
  return "text"
41
 
42
  # ---------- TOOLS ----------
43
  def text_tool(prompt):
44
- response = text_model(prompt, max_length=200)
45
- return response[0]["generated_text"]
46
 
47
  def image_tool(prompt):
48
- image = pipe(prompt, num_inference_steps=2, guidance_scale=0.0).images[0]
 
 
 
 
49
  return image
50
 
51
  # ---------- AGENT ----------
@@ -66,10 +68,10 @@ def chat(user_input, history):
66
  return history, image
67
 
68
  with gr.Blocks() as demo:
69
- gr.Markdown("# πŸ€– Agentic AI (Fast Text + Image)")
70
 
71
  chatbot = gr.Chatbot()
72
- image_output = gr.Image()
73
 
74
  with gr.Row():
75
  inp = gr.Textbox(placeholder="Ask anything or generate image...")
 
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 ----------
 
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...")