Raiff1982 commited on
Commit
3815eb3
·
verified ·
1 Parent(s): 750e584

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -29
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  from transformers import pipeline, set_seed
3
 
4
- # Preload models into a dictionary for selection
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 loaded models to avoid reloading
14
- model_cache = {}
 
15
 
16
- def load_model(model_name):
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
- # Terminal response function
26
- def codette_terminal(user_input, model_name, session_id):
 
 
 
 
27
  if session_id not in chat_memory:
28
  chat_memory[session_id] = []
29
 
30
- if user_input.lower() in ['exit', 'quit']:
31
  chat_memory[session_id] = []
32
- return "🧠 Codette signing off... Session reset."
33
 
34
- generator = load_model(model_name)
35
- output = generator(user_input, max_length=100, num_return_sequences=1, do_sample=True)
36
- response = output[0]['generated_text'].strip()
37
 
38
- chat_memory[session_id].append(f"🖋️ You > {user_input}")
39
  chat_memory[session_id].append(f"🧠 Codette > {response}")
40
- return "\n".join(chat_memory[session_id][-10:]) # limit memory output
 
 
 
 
 
 
 
41
 
42
- # UI
43
- with gr.Blocks(title="Codette Terminal - Multi-Model AI") as demo:
44
- gr.Markdown("## 🧬 Codette Terminal (Multi-Model, Hugging Face Edition)")
45
- gr.Markdown("Type something below. Use the dropdown to switch models. Type `'exit'` to reset session.")
46
 
47
- session_id = gr.Textbox(label="Session ID (hidden)", value="session_default", visible=False)
48
- model_select = gr.Dropdown(choices=list(AVAILABLE_MODELS.keys()), value="GPT-2 (small, fast)", label="Choose AI Model")
49
- user_input = gr.Textbox(label="Your Prompt", placeholder="Ask me anything...")
50
- output_box = gr.Textbox(label="Terminal Output", lines=20, interactive=False)
 
 
51
 
52
- user_input.submit(fn=codette_terminal, inputs=[user_input, model_select, session_id], outputs=output_box)
 
 
 
 
53
 
54
- # For Hugging Face Spaces
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()