Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
import tempfile
|
4 |
import imageio
|
5 |
import torch
|
6 |
from transformers import pipeline
|
7 |
from diffusers import DiffusionPipeline
|
8 |
|
9 |
-
# ----------
|
10 |
AVAILABLE_MODELS = {
|
11 |
"GPT-2 (small, fast)": "gpt2",
|
12 |
"Falcon (TII UAE)": "tiiuae/falcon-7b-instruct",
|
@@ -17,59 +16,87 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
17 |
text_model_cache = {}
|
18 |
chat_memory = {}
|
19 |
|
20 |
-
# ---------- Load
|
21 |
try:
|
22 |
-
image_generator = DiffusionPipeline.from_pretrained(
|
|
|
|
|
|
|
23 |
image_generator.to(device)
|
24 |
image_enabled = True
|
25 |
except Exception as e:
|
26 |
-
print(f"[Image Load Error]: {e}")
|
27 |
image_generator = None
|
28 |
image_enabled = False
|
29 |
|
|
|
30 |
try:
|
31 |
-
video_pipeline = DiffusionPipeline.from_pretrained(
|
|
|
|
|
|
|
32 |
video_pipeline.to(device)
|
33 |
video_enabled = True
|
34 |
except Exception as e:
|
35 |
-
print(f"[Video Load Error]: {e}")
|
36 |
video_pipeline = None
|
37 |
video_enabled = False
|
38 |
|
39 |
-
# ----------
|
40 |
def codette_terminal(prompt, model_name, generate_image, generate_video, session_id):
|
41 |
if session_id not in chat_memory:
|
42 |
chat_memory[session_id] = []
|
43 |
|
44 |
if prompt.lower() in ["exit", "quit"]:
|
45 |
chat_memory[session_id] = []
|
46 |
-
|
|
|
47 |
|
48 |
-
# Load text model if not
|
49 |
if model_name not in text_model_cache:
|
50 |
try:
|
51 |
-
text_model_cache[model_name] = pipeline(
|
|
|
|
|
|
|
|
|
52 |
except Exception as e:
|
53 |
-
|
|
|
54 |
|
55 |
generator = text_model_cache[model_name]
|
|
|
|
|
56 |
try:
|
57 |
-
output = generator(prompt, max_length=100, do_sample=True, num_return_sequences=1)
|
58 |
-
response = output[0]['generated_text'].strip()
|
59 |
except Exception as e:
|
60 |
-
|
|
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
chat_memory[session_id].append(f"ποΈ You > {prompt}")
|
63 |
-
chat_memory[session_id].append(f"π§ Codette > {
|
64 |
-
chat_log = "\n".join(chat_memory[session_id][-10:])
|
65 |
|
|
|
66 |
img = None
|
67 |
if generate_image and image_enabled:
|
68 |
try:
|
69 |
img = image_generator(prompt).images[0]
|
70 |
except Exception as e:
|
71 |
-
|
72 |
|
|
|
73 |
vid = None
|
74 |
if generate_video and video_enabled:
|
75 |
try:
|
@@ -78,13 +105,15 @@ def codette_terminal(prompt, model_name, generate_image, generate_video, session
|
|
78 |
imageio.mimsave(temp_video_path, video_frames, fps=8)
|
79 |
vid = temp_video_path
|
80 |
except Exception as e:
|
81 |
-
|
|
|
|
|
82 |
|
83 |
-
|
|
|
|
|
|
|
84 |
|
85 |
-
# ---------- Gradio App ----------
|
86 |
-
with gr.Blocks(title="Codette Terminal (Hugging Face Edition)") as demo:
|
87 |
-
gr.Markdown("## 𧬠Codette Terminal\nA text + image + video AI powered by Hugging Face + Gradio. Type `'exit'` to reset the session.")
|
88 |
session_id = gr.Textbox(value="session_default", visible=False)
|
89 |
model_dropdown = gr.Dropdown(choices=list(AVAILABLE_MODELS.keys()), value="GPT-2 (small, fast)", label="Choose a Language Model")
|
90 |
generate_image_toggle = gr.Checkbox(label="Also generate image?", value=False, interactive=image_enabled)
|
@@ -97,8 +126,12 @@ with gr.Blocks(title="Codette Terminal (Hugging Face Edition)") as demo:
|
|
97 |
user_input.submit(
|
98 |
codette_terminal,
|
99 |
inputs=[user_input, model_dropdown, generate_image_toggle, generate_video_toggle, session_id],
|
100 |
-
outputs=[output_text, output_image, output_video]
|
|
|
|
|
|
|
101 |
)
|
102 |
|
|
|
103 |
if __name__ == "__main__":
|
104 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import tempfile
|
3 |
import imageio
|
4 |
import torch
|
5 |
from transformers import pipeline
|
6 |
from diffusers import DiffusionPipeline
|
7 |
|
8 |
+
# ---------- Configuration ----------
|
9 |
AVAILABLE_MODELS = {
|
10 |
"GPT-2 (small, fast)": "gpt2",
|
11 |
"Falcon (TII UAE)": "tiiuae/falcon-7b-instruct",
|
|
|
16 |
text_model_cache = {}
|
17 |
chat_memory = {}
|
18 |
|
19 |
+
# ---------- Load Image Generator ----------
|
20 |
try:
|
21 |
+
image_generator = DiffusionPipeline.from_pretrained(
|
22 |
+
"runwayml/stable-diffusion-v1-5",
|
23 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
24 |
+
)
|
25 |
image_generator.to(device)
|
26 |
image_enabled = True
|
27 |
except Exception as e:
|
28 |
+
print(f"[Image Model Load Error]: {e}")
|
29 |
image_generator = None
|
30 |
image_enabled = False
|
31 |
|
32 |
+
# ---------- Load Video Generator ----------
|
33 |
try:
|
34 |
+
video_pipeline = DiffusionPipeline.from_pretrained(
|
35 |
+
"damo-vilab/text-to-video-ms-1.7b",
|
36 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
37 |
+
)
|
38 |
video_pipeline.to(device)
|
39 |
video_enabled = True
|
40 |
except Exception as e:
|
41 |
+
print(f"[Video Model Load Error]: {e}")
|
42 |
video_pipeline = None
|
43 |
video_enabled = False
|
44 |
|
45 |
+
# ---------- Streamed Response Generator ----------
|
46 |
def codette_terminal(prompt, model_name, generate_image, generate_video, session_id):
|
47 |
if session_id not in chat_memory:
|
48 |
chat_memory[session_id] = []
|
49 |
|
50 |
if prompt.lower() in ["exit", "quit"]:
|
51 |
chat_memory[session_id] = []
|
52 |
+
yield "π§ Codette signing off... Session reset.", None, None
|
53 |
+
return
|
54 |
|
55 |
+
# Load text model if not already loaded
|
56 |
if model_name not in text_model_cache:
|
57 |
try:
|
58 |
+
text_model_cache[model_name] = pipeline(
|
59 |
+
"text-generation",
|
60 |
+
model=AVAILABLE_MODELS[model_name],
|
61 |
+
device=0 if device == "cuda" else -1
|
62 |
+
)
|
63 |
except Exception as e:
|
64 |
+
yield f"[Text model error]: {e}", None, None
|
65 |
+
return
|
66 |
|
67 |
generator = text_model_cache[model_name]
|
68 |
+
|
69 |
+
# Generate response
|
70 |
try:
|
71 |
+
output = generator(prompt, max_length=100, do_sample=True, num_return_sequences=1)[0]['generated_text'].strip()
|
|
|
72 |
except Exception as e:
|
73 |
+
yield f"[Text generation error]: {e}", None, None
|
74 |
+
return
|
75 |
|
76 |
+
# Stream the output character by character
|
77 |
+
response_so_far = ""
|
78 |
+
for char in output:
|
79 |
+
response_so_far += char
|
80 |
+
temp_log = chat_memory[session_id][:]
|
81 |
+
temp_log.append(f"ποΈ You > {prompt}")
|
82 |
+
temp_log.append(f"π§ Codette > {response_so_far}")
|
83 |
+
yield "\n".join(temp_log[-10:]), None, None
|
84 |
+
import time
|
85 |
+
time.sleep(0.01)
|
86 |
+
|
87 |
+
# Finalize chat memory
|
88 |
chat_memory[session_id].append(f"ποΈ You > {prompt}")
|
89 |
+
chat_memory[session_id].append(f"π§ Codette > {output}")
|
|
|
90 |
|
91 |
+
# Image Generation
|
92 |
img = None
|
93 |
if generate_image and image_enabled:
|
94 |
try:
|
95 |
img = image_generator(prompt).images[0]
|
96 |
except Exception as e:
|
97 |
+
response_so_far += f"\n[Image error]: {e}"
|
98 |
|
99 |
+
# Video Generation
|
100 |
vid = None
|
101 |
if generate_video and video_enabled:
|
102 |
try:
|
|
|
105 |
imageio.mimsave(temp_video_path, video_frames, fps=8)
|
106 |
vid = temp_video_path
|
107 |
except Exception as e:
|
108 |
+
response_so_far += f"\n[Video error]: {e}"
|
109 |
+
|
110 |
+
yield "\n".join(chat_memory[session_id][-10:]), img, vid
|
111 |
|
112 |
+
# ---------- Gradio UI ----------
|
113 |
+
with gr.Blocks(title="𧬠Codette Terminal β Streamed AI Chat") as demo:
|
114 |
+
gr.Markdown("## 𧬠Codette Terminal (Chat + Image + Video)")
|
115 |
+
gr.Markdown("Type a prompt and select your model. Enable image or video generation if you like. Type `'exit'` to reset.")
|
116 |
|
|
|
|
|
|
|
117 |
session_id = gr.Textbox(value="session_default", visible=False)
|
118 |
model_dropdown = gr.Dropdown(choices=list(AVAILABLE_MODELS.keys()), value="GPT-2 (small, fast)", label="Choose a Language Model")
|
119 |
generate_image_toggle = gr.Checkbox(label="Also generate image?", value=False, interactive=image_enabled)
|
|
|
126 |
user_input.submit(
|
127 |
codette_terminal,
|
128 |
inputs=[user_input, model_dropdown, generate_image_toggle, generate_video_toggle, session_id],
|
129 |
+
outputs=[output_text, output_image, output_video],
|
130 |
+
concurrency_limit=1,
|
131 |
+
queue=True,
|
132 |
+
show_progress=True
|
133 |
)
|
134 |
|
135 |
+
# ---------- Launch ----------
|
136 |
if __name__ == "__main__":
|
137 |
demo.launch()
|