Shankarm08 commited on
Commit
8fdb8a0
Β·
verified Β·
1 Parent(s): 39172b7
Files changed (1) hide show
  1. app.py +32 -11
app.py CHANGED
@@ -1,31 +1,52 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
 
3
 
4
- # -------- MODEL --------
5
  text_model = pipeline("text-generation", model="distilgpt2")
6
 
 
 
 
 
 
 
 
7
  # -------- FUNCTION --------
8
  def respond(user_input, history):
9
- result = text_model(user_input, max_length=80)[0]["generated_text"]
 
 
 
 
 
10
 
11
- history = history + [
12
- {"role": "user", "content": user_input},
13
- {"role": "assistant", "content": result}
14
- ]
15
 
16
- return history, history
 
 
 
17
 
18
  # -------- UI --------
19
  with gr.Blocks() as demo:
20
- gr.Markdown("# Simple AI Chat (Fixed)")
 
 
 
21
 
22
- chatbot = gr.Chatbot(type="messages") # βœ… IMPORTANT
23
  state = gr.State([])
24
 
25
  with gr.Row():
26
- inp = gr.Textbox(placeholder="Type something...")
27
  btn = gr.Button("Send")
28
 
29
- btn.click(respond, [inp, state], [chatbot, state])
 
 
 
 
30
 
31
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ from diffusers import AutoPipelineForText2Image
4
+ import torch
5
 
6
+ # -------- TEXT MODEL --------
7
  text_model = pipeline("text-generation", model="distilgpt2")
8
 
9
+ # -------- IMAGE MODEL (LIGHT) --------
10
+ pipe = AutoPipelineForText2Image.from_pretrained(
11
+ "stabilityai/sd-turbo",
12
+ torch_dtype=torch.float32
13
+ )
14
+ pipe = pipe.to("cpu")
15
+
16
  # -------- FUNCTION --------
17
  def respond(user_input, history):
18
+ if any(word in user_input.lower() for word in ["draw", "image", "generate", "picture"]):
19
+ image = pipe(
20
+ user_input,
21
+ num_inference_steps=1,
22
+ guidance_scale=0.0
23
+ ).images[0]
24
 
25
+ history.append((user_input, "πŸ–ΌοΈ Image generated"))
26
+ return history, history, image
 
 
27
 
28
+ else:
29
+ result = text_model(user_input, max_length=80)[0]["generated_text"]
30
+ history.append((user_input, result))
31
+ return history, history, None
32
 
33
  # -------- UI --------
34
  with gr.Blocks() as demo:
35
+ gr.Markdown("# AI Chat + Image (Stable Version)")
36
+
37
+ chatbot = gr.Chatbot()
38
+ image_output = gr.Image()
39
 
 
40
  state = gr.State([])
41
 
42
  with gr.Row():
43
+ inp = gr.Textbox(placeholder="Ask or generate image...")
44
  btn = gr.Button("Send")
45
 
46
+ btn.click(
47
+ respond,
48
+ [inp, state],
49
+ [chatbot, state, image_output]
50
+ )
51
 
52
  demo.launch()