Rajan Sharma commited on
Commit
e88005b
·
verified ·
1 Parent(s): 063d139

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -44
app.py CHANGED
@@ -3,20 +3,43 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  from datetime import datetime, timezone
4
  import os
5
  from huggingface_hub import login
 
6
 
7
- # Login to Hugging Face Hub
8
- login(token=os.getenv("HUGGING_FACE_HUB_TOKEN"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- # Initialize the model and tokenizer with auth
11
- model_id = "CohereLabs/c4ai-command-a-03-2025"
12
- tokenizer = AutoTokenizer.from_pretrained(
13
- model_id,
14
- use_auth_token=True # Add authentication
15
- )
16
- model = AutoModelForCausalLM.from_pretrained(
17
- model_id,
18
- use_auth_token=True # Add authentication
19
- )
20
 
21
  def get_timestamp():
22
  """Get current UTC datetime in specified format"""
@@ -30,40 +53,37 @@ def format_system_info():
30
  )
31
 
32
  def chat(message, history):
 
 
 
33
  if history is None:
34
  history = []
35
 
36
- # Add system information
37
- system_info = format_system_info()
38
-
39
- # Format messages with the c4ai-command-a-03-2025 chat template
40
- messages = [{"role": "user", "content": message}]
41
- input_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True)
42
-
43
- # Generate response
44
- gen_tokens = model.generate(
45
- input_ids,
46
- max_new_tokens=100,
47
- do_sample=True,
48
- temperature=0.3,
49
- )
50
-
51
- # Decode response
52
- gen_text = tokenizer.decode(gen_tokens[0])
53
-
54
- # Format the full response with system info
55
- formatted_response = f"{system_info}\n{gen_text}"
56
- history.append((message, formatted_response))
57
- return history
58
-
59
- # Create custom theme
60
- theme = gr.themes.Default().set(
61
- body_background_fill="#f0f8ff",
62
- block_background_fill="#ffffff",
63
- block_border_width="1px",
64
- block_border_color="#2c3e50",
65
- block_radius="10px"
66
- )
67
 
68
  # Create the Gradio interface
69
  demo = gr.ChatInterface(
@@ -71,7 +91,6 @@ demo = gr.ChatInterface(
71
  title="Medical Decision Support AI",
72
  description="""A medical decision support system that provides healthcare-related information and guidance.
73
  Current UTC Time: """ + get_timestamp(),
74
- theme=theme,
75
  examples=[
76
  "What are the symptoms of hypertension?",
77
  "What are common drug interactions with aspirin?",
 
3
  from datetime import datetime, timezone
4
  import os
5
  from huggingface_hub import login
6
+ from huggingface_hub.utils import RepositoryNotFoundError, HfHubHTTPError
7
 
8
+ def initialize_model():
9
+ try:
10
+ # Login to Hugging Face Hub
11
+ login(token=os.getenv("HUGGING_FACE_HUB_TOKEN"))
12
+
13
+ # Initialize the model and tokenizer with auth
14
+ model_id = "CohereLabs/c4ai-command-a-03-2025"
15
+ tokenizer = AutoTokenizer.from_pretrained(
16
+ model_id,
17
+ use_auth_token=True
18
+ )
19
+ model = AutoModelForCausalLM.from_pretrained(
20
+ model_id,
21
+ use_auth_token=True
22
+ )
23
+ return True, model, tokenizer
24
+ except RepositoryNotFoundError:
25
+ return False, "Model repository not found. Please check the model ID.", None
26
+ except HfHubHTTPError as e:
27
+ if e.response.status_code == 401:
28
+ return False, "Authentication failed. Please check your token permissions.", None
29
+ elif e.response.status_code == 403:
30
+ return False, "Access denied. Please ensure you have access to this model.", None
31
+ else:
32
+ return False, f"An error occurred: {str(e)}", None
33
+ except Exception as e:
34
+ return False, f"Unexpected error: {str(e)}", None
35
 
36
+ # Initialize model and handle potential errors
37
+ success, result, tokenizer = initialize_model()
38
+ if not success:
39
+ print(f"Error initializing model: {result}")
40
+ # You might want to raise an exception here or handle the error differently
41
+ else:
42
+ model = result
 
 
 
43
 
44
  def get_timestamp():
45
  """Get current UTC datetime in specified format"""
 
53
  )
54
 
55
  def chat(message, history):
56
+ if not success:
57
+ return [(message, f"Error: {result}")]
58
+
59
  if history is None:
60
  history = []
61
 
62
+ try:
63
+ # Add system information
64
+ system_info = format_system_info()
65
+
66
+ # Format messages with the c4ai-command-a-03-2025 chat template
67
+ messages = [{"role": "user", "content": message}]
68
+ input_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True)
69
+
70
+ # Generate response
71
+ gen_tokens = model.generate(
72
+ input_ids,
73
+ max_new_tokens=100,
74
+ do_sample=True,
75
+ temperature=0.3,
76
+ )
77
+
78
+ # Decode response
79
+ gen_text = tokenizer.decode(gen_tokens[0])
80
+
81
+ # Format the full response with system info
82
+ formatted_response = f"{system_info}\n{gen_text}"
83
+ history.append((message, formatted_response))
84
+ return history
85
+ except Exception as e:
86
+ return [(message, f"Error during chat: {str(e)}")]
 
 
 
 
 
 
87
 
88
  # Create the Gradio interface
89
  demo = gr.ChatInterface(
 
91
  title="Medical Decision Support AI",
92
  description="""A medical decision support system that provides healthcare-related information and guidance.
93
  Current UTC Time: """ + get_timestamp(),
 
94
  examples=[
95
  "What are the symptoms of hypertension?",
96
  "What are common drug interactions with aspirin?",