Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline, set_seed
|
3 |
|
4 |
-
#
|
5 |
AVAILABLE_MODELS = {
|
6 |
"GPT-2 (small, fast)": "gpt2",
|
7 |
"Falcon (TII UAE)": "tiiuae/falcon-7b-instruct",
|
@@ -10,47 +10,59 @@ AVAILABLE_MODELS = {
|
|
10 |
|
11 |
set_seed(42)
|
12 |
|
13 |
-
# Cache
|
14 |
-
|
|
|
15 |
|
16 |
-
|
17 |
-
if model_name not in model_cache:
|
18 |
-
model_id = AVAILABLE_MODELS[model_name]
|
19 |
-
model_cache[model_name] = pipeline("text-generation", model=model_id)
|
20 |
-
return model_cache[model_name]
|
21 |
-
|
22 |
-
# Chat history buffer
|
23 |
chat_memory = {}
|
24 |
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
if session_id not in chat_memory:
|
28 |
chat_memory[session_id] = []
|
29 |
|
30 |
-
if
|
31 |
chat_memory[session_id] = []
|
32 |
-
return "🧠 Codette signing off... Session reset."
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
response =
|
37 |
|
38 |
-
chat_memory[session_id].append(f"🖋️ You > {
|
39 |
chat_memory[session_id].append(f"🧠 Codette > {response}")
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
# UI
|
43 |
-
with gr.Blocks(title="Codette Terminal
|
44 |
-
gr.Markdown("## 🧬 Codette Terminal (
|
45 |
-
gr.Markdown("
|
46 |
|
47 |
-
session_id = gr.Textbox(
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
51 |
|
52 |
-
user_input.submit(
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
#
|
55 |
if __name__ == "__main__":
|
56 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline, set_seed
|
3 |
|
4 |
+
# Define available models
|
5 |
AVAILABLE_MODELS = {
|
6 |
"GPT-2 (small, fast)": "gpt2",
|
7 |
"Falcon (TII UAE)": "tiiuae/falcon-7b-instruct",
|
|
|
10 |
|
11 |
set_seed(42)
|
12 |
|
13 |
+
# Cache models
|
14 |
+
text_model_cache = {}
|
15 |
+
image_generator = pipeline("text-to-image", model="CompVis/stable-diffusion-v1-4")
|
16 |
|
17 |
+
# Session memory
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
chat_memory = {}
|
19 |
|
20 |
+
def load_text_model(model_name):
|
21 |
+
if model_name not in text_model_cache:
|
22 |
+
text_model_cache[model_name] = pipeline("text-generation", model=AVAILABLE_MODELS[model_name])
|
23 |
+
return text_model_cache[model_name]
|
24 |
+
|
25 |
+
def codette_terminal(prompt, model_name, generate_image, session_id):
|
26 |
if session_id not in chat_memory:
|
27 |
chat_memory[session_id] = []
|
28 |
|
29 |
+
if prompt.lower() in ["exit", "quit"]:
|
30 |
chat_memory[session_id] = []
|
31 |
+
return "🧠 Codette signing off... Session reset.", None
|
32 |
|
33 |
+
# Text generation
|
34 |
+
generator = load_text_model(model_name)
|
35 |
+
response = generator(prompt, max_length=100, num_return_sequences=1, do_sample=True)[0]['generated_text'].strip()
|
36 |
|
37 |
+
chat_memory[session_id].append(f"🖋️ You > {prompt}")
|
38 |
chat_memory[session_id].append(f"🧠 Codette > {response}")
|
39 |
+
chat_log = "\n".join(chat_memory[session_id][-10:])
|
40 |
+
|
41 |
+
# Optional image generation
|
42 |
+
img = None
|
43 |
+
if generate_image:
|
44 |
+
img = image_generator(prompt)[0]['image']
|
45 |
+
|
46 |
+
return chat_log, img
|
47 |
|
48 |
+
# Build Gradio UI
|
49 |
+
with gr.Blocks(title="Codette Terminal: Text + Image AI") as demo:
|
50 |
+
gr.Markdown("## 🧬 Codette Terminal (Text + Image, Hugging Face Edition)")
|
51 |
+
gr.Markdown("Choose your model, enter a prompt. Enable image generation if desired. Type `'exit'` to reset.")
|
52 |
|
53 |
+
session_id = gr.Textbox(value="session_default", visible=False)
|
54 |
+
model_dropdown = gr.Dropdown(choices=list(AVAILABLE_MODELS.keys()), value="GPT-2 (small, fast)", label="Choose Language Model")
|
55 |
+
generate_image_toggle = gr.Checkbox(label="Also generate image?", value=False)
|
56 |
+
user_input = gr.Textbox(label="Your Prompt", placeholder="e.g. a castle on Mars, explained by an AI philosopher", lines=1)
|
57 |
+
output_text = gr.Textbox(label="Terminal Output", lines=15, interactive=False)
|
58 |
+
output_image = gr.Image(label="AI-Generated Image")
|
59 |
|
60 |
+
user_input.submit(
|
61 |
+
fn=codette_terminal,
|
62 |
+
inputs=[user_input, model_dropdown, generate_image_toggle, session_id],
|
63 |
+
outputs=[output_text, output_image]
|
64 |
+
)
|
65 |
|
66 |
+
# Launch on Spaces
|
67 |
if __name__ == "__main__":
|
68 |
demo.launch()
|