Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from transformers import pipeline, set_seed
|
| 4 |
-
from diffusers import DiffusionPipeline
|
| 5 |
import tempfile
|
| 6 |
import imageio
|
| 7 |
|
|
@@ -12,29 +11,29 @@ AVAILABLE_MODELS = {
|
|
| 12 |
"Mistral (OpenAccess)": "mistralai/Mistral-7B-v0.1"
|
| 13 |
}
|
| 14 |
|
| 15 |
-
set_seed(42)
|
| 16 |
-
text_model_cache = {}
|
| 17 |
-
|
| 18 |
# Load text-to-image model using diffusers (correct API)
|
| 19 |
try:
|
|
|
|
| 20 |
image_generator = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
|
| 21 |
image_generator.to("cpu")
|
| 22 |
image_enabled = True
|
| 23 |
-
except
|
| 24 |
image_generator = None
|
| 25 |
image_enabled = False
|
| 26 |
-
print(
|
| 27 |
|
| 28 |
# Load text-to-video model
|
| 29 |
try:
|
|
|
|
| 30 |
video_pipeline = DiffusionPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b")
|
| 31 |
video_pipeline.to("cpu")
|
| 32 |
video_enabled = True
|
| 33 |
-
except
|
| 34 |
video_pipeline = None
|
| 35 |
video_enabled = False
|
| 36 |
-
print(
|
| 37 |
|
|
|
|
| 38 |
chat_memory = {}
|
| 39 |
|
| 40 |
# ---------- Core Function ----------
|
|
@@ -46,15 +45,18 @@ def codette_terminal(prompt, model_name, generate_image, generate_video, session
|
|
| 46 |
chat_memory[session_id] = []
|
| 47 |
return "🧠 Codette signing off... Session reset.", None, None
|
| 48 |
|
|
|
|
| 49 |
if model_name not in text_model_cache:
|
| 50 |
-
text_model_cache[model_name] =
|
| 51 |
generator = text_model_cache[model_name]
|
| 52 |
response = generator(prompt, max_length=100, num_return_sequences=1, do_sample=True)[0]['generated_text'].strip()
|
| 53 |
|
|
|
|
| 54 |
chat_memory[session_id].append(f"🖋️ You > {prompt}")
|
| 55 |
chat_memory[session_id].append(f"🧠 Codette > {response}")
|
| 56 |
chat_log = "\n".join(chat_memory[session_id][-10:])
|
| 57 |
|
|
|
|
| 58 |
img = None
|
| 59 |
if generate_image and image_enabled:
|
| 60 |
try:
|
|
@@ -62,6 +64,7 @@ def codette_terminal(prompt, model_name, generate_image, generate_video, session
|
|
| 62 |
except Exception as e:
|
| 63 |
chat_log += f"\n[Image error]: {e}"
|
| 64 |
|
|
|
|
| 65 |
vid = None
|
| 66 |
if generate_video and video_enabled:
|
| 67 |
try:
|
|
@@ -95,4 +98,4 @@ with gr.Blocks(title="Codette Terminal – Text + Image + Video") as demo:
|
|
| 95 |
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
-
demo.launch()
|
|
|
|
| 1 |
+
# This is a Gradio app that provides a text-based chat interface with optional image and video generation.
|
| 2 |
import gradio as gr
|
| 3 |
+
import numpy as np
|
|
|
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import imageio
|
| 6 |
|
|
|
|
| 11 |
"Mistral (OpenAccess)": "mistralai/Mistral-7B-v0.1"
|
| 12 |
}
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
# Load text-to-image model using diffusers (correct API)
|
| 15 |
try:
|
| 16 |
+
from diffusers import DiffusionPipeline
|
| 17 |
image_generator = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
|
| 18 |
image_generator.to("cpu")
|
| 19 |
image_enabled = True
|
| 20 |
+
except ImportError:
|
| 21 |
image_generator = None
|
| 22 |
image_enabled = False
|
| 23 |
+
print("[Image model error]: diffusers library not found")
|
| 24 |
|
| 25 |
# Load text-to-video model
|
| 26 |
try:
|
| 27 |
+
from diffusers import DiffusionPipeline
|
| 28 |
video_pipeline = DiffusionPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b")
|
| 29 |
video_pipeline.to("cpu")
|
| 30 |
video_enabled = True
|
| 31 |
+
except ImportError:
|
| 32 |
video_pipeline = None
|
| 33 |
video_enabled = False
|
| 34 |
+
print("[Video model error]: diffusers library not found")
|
| 35 |
|
| 36 |
+
text_model_cache = {}
|
| 37 |
chat_memory = {}
|
| 38 |
|
| 39 |
# ---------- Core Function ----------
|
|
|
|
| 45 |
chat_memory[session_id] = []
|
| 46 |
return "🧠 Codette signing off... Session reset.", None, None
|
| 47 |
|
| 48 |
+
# Load the text generation model if it's not already in the cache
|
| 49 |
if model_name not in text_model_cache:
|
| 50 |
+
text_model_cache[model_name] = gr.pipelines.TextGeneration(model=AVAILABLE_MODELS[model_name])
|
| 51 |
generator = text_model_cache[model_name]
|
| 52 |
response = generator(prompt, max_length=100, num_return_sequences=1, do_sample=True)[0]['generated_text'].strip()
|
| 53 |
|
| 54 |
+
# Update the chat log
|
| 55 |
chat_memory[session_id].append(f"🖋️ You > {prompt}")
|
| 56 |
chat_memory[session_id].append(f"🧠 Codette > {response}")
|
| 57 |
chat_log = "\n".join(chat_memory[session_id][-10:])
|
| 58 |
|
| 59 |
+
# Generate image if requested and image generation is enabled
|
| 60 |
img = None
|
| 61 |
if generate_image and image_enabled:
|
| 62 |
try:
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
chat_log += f"\n[Image error]: {e}"
|
| 66 |
|
| 67 |
+
# Generate video if requested and video generation is enabled
|
| 68 |
vid = None
|
| 69 |
if generate_video and video_enabled:
|
| 70 |
try:
|
|
|
|
| 98 |
)
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
| 101 |
+
demo.launch(show_error=True)
|