Rajan Sharma commited on
Commit
68f033a
·
verified ·
1 Parent(s): 7bece4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -40
app.py CHANGED
@@ -2,27 +2,58 @@ import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
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
  token = os.getenv("HUGGING_FACE_HUB_TOKEN")
12
  if not token:
13
  return False, "No token found. Please set HUGGING_FACE_HUB_TOKEN in Space secrets.", None
14
 
15
  login(token=token)
16
 
17
- # Initialize the model and tokenizer with token (not use_auth_token)
18
  model_id = "CohereLabs/c4ai-command-a-03-2025"
19
  tokenizer = AutoTokenizer.from_pretrained(
20
  model_id,
21
- token=token # Updated from use_auth_token
22
  )
23
  model = AutoModelForCausalLM.from_pretrained(
24
  model_id,
25
- token=token # Updated from use_auth_token
26
  )
27
  return True, model, tokenizer
28
  except RepositoryNotFoundError:
@@ -37,34 +68,27 @@ def initialize_model():
37
  except Exception as e:
38
  return False, f"Unexpected error: {str(e)}", None
39
 
40
- # Initialize model and handle potential errors
41
- success, result, tokenizer = initialize_model()
42
- if not success:
43
- print(f"Error initializing model: {result}")
44
- else:
45
- model = result
46
-
47
- def get_timestamp():
48
- """Get current UTC datetime in specified format"""
49
- return datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%SS')
50
-
51
- def format_system_info():
52
- """Format system information header"""
53
- return (
54
- f"Current Date and Time (UTC - YYYY-MM-DD HH:MM:SS formatted): {get_timestamp()}\n"
55
- f"Current User's Login: Raj-VedAI\n"
56
- )
57
 
58
  def chat(message, history):
59
- if not success:
60
- return [(message, f"Error: {result}")]
 
 
 
 
61
 
62
  if history is None:
63
  history = []
64
 
65
  try:
66
- # Add system information
67
- system_info = format_system_info()
 
 
 
68
 
69
  # Format messages with the c4ai-command-a-03-2025 chat template
70
  messages = [{"role": "user", "content": message}]
@@ -82,23 +106,35 @@ def chat(message, history):
82
  gen_text = tokenizer.decode(gen_tokens[0])
83
 
84
  # Format the full response with system info
85
- formatted_response = f"{system_info}\n{gen_text}"
86
  history.append((message, formatted_response))
87
  return history
88
  except Exception as e:
89
- return [(message, f"Error during chat: {str(e)}")]
90
 
91
- # Create the Gradio interface - removed retry_on_error
92
- demo = gr.ChatInterface(
93
- fn=chat,
94
- title="Medical Decision Support AI",
95
- description="""A medical decision support system that provides healthcare-related information and guidance.
96
- Current UTC Time: """ + get_timestamp(),
97
- examples=[
98
- "What are the symptoms of hypertension?",
99
- "What are common drug interactions with aspirin?",
100
- "What are the warning signs of diabetes?",
101
- ]
102
- )
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  demo.launch()
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  from datetime import datetime, timezone
4
  import os
5
+ from huggingface_hub import login, HfApi
6
  from huggingface_hub.utils import RepositoryNotFoundError, HfHubHTTPError
7
+ import requests
8
+
9
+ def get_timestamp():
10
+ """Get current UTC datetime in specified format"""
11
+ return datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%SS')
12
+
13
+ def format_system_info():
14
+ """Format system information header"""
15
+ return (
16
+ f"Current Date and Time (UTC - YYYY-MM-DD HH:MM:SS formatted): {get_timestamp()}\n"
17
+ f"Current User's Login: Raj-VedAI\n"
18
+ )
19
+
20
+ def verify_model_access():
21
+ system_info = format_system_info()
22
+ try:
23
+ token = os.getenv("HUGGING_FACE_HUB_TOKEN")
24
+ if not token:
25
+ return False, f"{system_info}Status: No token found"
26
+
27
+ # Method 1: Direct API check
28
+ api = HfApi(token=token)
29
+ try:
30
+ model_info = api.model_info("CohereLabs/c4ai-command-a-03-2025")
31
+ return True, f"{system_info}Status: ✅ Access granted\nModel: CohereLabs/c4ai-command-a-03-2025"
32
+ except Exception as e:
33
+ if "403" in str(e):
34
+ return False, f"{system_info}Status: ❌ Access denied\nPlease request access at https://huggingface.co/CohereLabs/c4ai-command-a-03-2025"
35
+ return False, f"{system_info}Status: ❌ Error\nDetails: {str(e)}"
36
+
37
+ except Exception as e:
38
+ return False, f"{system_info}Status: ❌ Unexpected error\nDetails: {str(e)}"
39
 
40
  def initialize_model():
41
  try:
 
42
  token = os.getenv("HUGGING_FACE_HUB_TOKEN")
43
  if not token:
44
  return False, "No token found. Please set HUGGING_FACE_HUB_TOKEN in Space secrets.", None
45
 
46
  login(token=token)
47
 
48
+ # Initialize the model and tokenizer with token
49
  model_id = "CohereLabs/c4ai-command-a-03-2025"
50
  tokenizer = AutoTokenizer.from_pretrained(
51
  model_id,
52
+ token=token
53
  )
54
  model = AutoModelForCausalLM.from_pretrained(
55
  model_id,
56
+ token=token
57
  )
58
  return True, model, tokenizer
59
  except RepositoryNotFoundError:
 
68
  except Exception as e:
69
  return False, f"Unexpected error: {str(e)}", None
70
 
71
+ def check_access_status():
72
+ access_granted, message = verify_model_access()
73
+ return message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  def chat(message, history):
76
+ system_info = format_system_info()
77
+
78
+ # Verify access before proceeding
79
+ access_granted, status_message = verify_model_access()
80
+ if not access_granted:
81
+ return [(message, f"{system_info}Error: {status_message}")]
82
 
83
  if history is None:
84
  history = []
85
 
86
  try:
87
+ # Initialize model if not already done
88
+ success, result, tokenizer = initialize_model()
89
+ if not success:
90
+ return [(message, f"{system_info}Error: {result}")]
91
+ model = result
92
 
93
  # Format messages with the c4ai-command-a-03-2025 chat template
94
  messages = [{"role": "user", "content": message}]
 
106
  gen_text = tokenizer.decode(gen_tokens[0])
107
 
108
  # Format the full response with system info
109
+ formatted_response = f"{system_info}{gen_text}"
110
  history.append((message, formatted_response))
111
  return history
112
  except Exception as e:
113
+ return [(message, f"{system_info}Error during chat: {str(e)}")]
114
 
115
+ # Create the Gradio interface with both chat and status check
116
+ with gr.Blocks(theme=gr.themes.Default()) as demo:
117
+ gr.Markdown(f"# Medical Decision Support AI\n{format_system_info()}")
118
+
119
+ with gr.Row():
120
+ status_btn = gr.Button("Check Access Status")
121
+ status_output = gr.Textbox(label="Access Status", lines=6)
122
+
123
+ chat_interface = gr.ChatInterface(
124
+ fn=chat,
125
+ description="A medical decision support system that provides healthcare-related information and guidance.",
126
+ examples=[
127
+ "What are the symptoms of hypertension?",
128
+ "What are common drug interactions with aspirin?",
129
+ "What are the warning signs of diabetes?",
130
+ ]
131
+ )
132
+
133
+ status_btn.click(check_access_status, outputs=status_output)
134
+
135
+ # Perform initial access check
136
+ access_granted, status_message = verify_model_access()
137
+ if not access_granted:
138
+ gr.Warning(status_message)
139
 
140
  demo.launch()