Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
from datetime import datetime, timezone
|
| 4 |
|
| 5 |
-
# Initialize the model
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def get_timestamp():
|
| 9 |
"""Get current UTC datetime in specified format"""
|
| 10 |
-
return datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%
|
| 11 |
|
| 12 |
def format_system_info():
|
| 13 |
"""Format system information header"""
|
|
@@ -23,31 +25,29 @@ def chat(message, history):
|
|
| 23 |
# Add system information
|
| 24 |
system_info = format_system_info()
|
| 25 |
|
| 26 |
-
# Format the
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
except Exception as e:
|
| 46 |
-
return [(message, f"Error: {str(e)}")]
|
| 47 |
|
| 48 |
# Create custom theme
|
| 49 |
theme = gr.themes.Default().set(
|
| 50 |
-
body_background_fill="#f0f8ff",
|
| 51 |
block_background_fill="#ffffff",
|
| 52 |
block_border_width="1px",
|
| 53 |
block_border_color="#2c3e50",
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
from datetime import datetime, timezone
|
| 4 |
|
| 5 |
+
# Initialize the model and tokenizer
|
| 6 |
+
model_id = "CohereLabs/c4ai-command-a-03-2025"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 9 |
|
| 10 |
def get_timestamp():
|
| 11 |
"""Get current UTC datetime in specified format"""
|
| 12 |
+
return datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%SS')
|
| 13 |
|
| 14 |
def format_system_info():
|
| 15 |
"""Format system information header"""
|
|
|
|
| 25 |
# Add system information
|
| 26 |
system_info = format_system_info()
|
| 27 |
|
| 28 |
+
# Format messages with the c4ai-command-a-03-2025 chat template
|
| 29 |
+
messages = [{"role": "user", "content": message}]
|
| 30 |
+
input_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True)
|
| 31 |
+
|
| 32 |
+
# Generate response
|
| 33 |
+
gen_tokens = model.generate(
|
| 34 |
+
input_ids,
|
| 35 |
+
max_new_tokens=100,
|
| 36 |
+
do_sample=True,
|
| 37 |
+
temperature=0.3,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Decode response
|
| 41 |
+
gen_text = tokenizer.decode(gen_tokens[0])
|
| 42 |
+
|
| 43 |
+
# Format the full response with system info
|
| 44 |
+
formatted_response = f"{system_info}\n{gen_text}"
|
| 45 |
+
history.append((message, formatted_response))
|
| 46 |
+
return history
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Create custom theme
|
| 49 |
theme = gr.themes.Default().set(
|
| 50 |
+
body_background_fill="#f0f8ff",
|
| 51 |
block_background_fill="#ffffff",
|
| 52 |
block_border_width="1px",
|
| 53 |
block_border_color="#2c3e50",
|