File size: 2,150 Bytes
c09e9ae
e513d6a
c09e9ae
28a2cfa
 
 
 
 
 
 
e513d6a
c09e9ae
28a2cfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e513d6a
28a2cfa
 
 
e513d6a
28a2cfa
 
e513d6a
 
28a2cfa
 
 
e513d6a
28a2cfa
 
 
 
e513d6a
28a2cfa
 
 
 
e513d6a
28a2cfa
e513d6a
28a2cfa
e513d6a
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gradio as gr
from transformers import pipeline, set_seed

# Preload models into a dictionary for selection
AVAILABLE_MODELS = {
    "GPT-2 (small, fast)": "gpt2",
    "Falcon (TII UAE)": "tiiuae/falcon-7b-instruct",
    "Mistral (OpenAccess)": "mistralai/Mistral-7B-v0.1"
}

set_seed(42)

# Cache loaded models to avoid reloading
model_cache = {}

def load_model(model_name):
    if model_name not in model_cache:
        model_id = AVAILABLE_MODELS[model_name]
        model_cache[model_name] = pipeline("text-generation", model=model_id)
    return model_cache[model_name]

# Chat history buffer
chat_memory = {}

# Terminal response function
def codette_terminal(user_input, model_name, session_id):
    if session_id not in chat_memory:
        chat_memory[session_id] = []

    if user_input.lower() in ['exit', 'quit']:
        chat_memory[session_id] = []
        return "🧠 Codette signing off... Session reset."

    generator = load_model(model_name)
    output = generator(user_input, max_length=100, num_return_sequences=1, do_sample=True)
    response = output[0]['generated_text'].strip()

    chat_memory[session_id].append(f"🖋️ You > {user_input}")
    chat_memory[session_id].append(f"🧠 Codette > {response}")
    return "\n".join(chat_memory[session_id][-10:])  # limit memory output

# UI
with gr.Blocks(title="Codette Terminal - Multi-Model AI") as demo:
    gr.Markdown("## 🧬 Codette Terminal (Multi-Model, Hugging Face Edition)")
    gr.Markdown("Type something below. Use the dropdown to switch models. Type `'exit'` to reset session.")

    session_id = gr.Textbox(label="Session ID (hidden)", value="session_default", visible=False)
    model_select = gr.Dropdown(choices=list(AVAILABLE_MODELS.keys()), value="GPT-2 (small, fast)", label="Choose AI Model")
    user_input = gr.Textbox(label="Your Prompt", placeholder="Ask me anything...")
    output_box = gr.Textbox(label="Terminal Output", lines=20, interactive=False)

    user_input.submit(fn=codette_terminal, inputs=[user_input, model_select, session_id], outputs=output_box)

# For Hugging Face Spaces
if __name__ == "__main__":
    demo.launch()