syubraj commited on
Commit
4363929
·
verified ·
1 Parent(s): 539dbce

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. .gradio/certificate.pem +31 -0
  2. README.md +25 -0
  3. app.py +27 -10
.gradio/certificate.pem ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
3
+ TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
4
+ cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
5
+ WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
6
+ ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
7
+ MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
8
+ h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
9
+ 0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
10
+ A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW
11
+ T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH
12
+ B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC
13
+ B5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0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l9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
30
+ emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
31
+ -----END CERTIFICATE-----
README.md CHANGED
@@ -7,3 +7,28 @@ app_file: app.py
7
  sdk: gradio
8
  sdk_version: 5.15.0
9
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  sdk: gradio
8
  sdk_version: 5.15.0
9
  ---
10
+ # MedicalChatbot-Phi3.5
11
+
12
+ ## Getting Started
13
+ 1. Create virtual environment
14
+ ```python
15
+ python -m venv venv
16
+ ```
17
+
18
+ 2. Activate virtual environment
19
+ ```python
20
+ source venv/bin/activate
21
+ ```
22
+
23
+ 3. Install Required packages
24
+ ```python
25
+ pip install -r requirements.txt
26
+ ```
27
+
28
+ ## Run Gradio App
29
+ ```python
30
+ python app.py
31
+ ```
32
+
33
+ ## Sample Output
34
+ ![image](https://github.com/user-attachments/assets/705a89d2-02f3-4398-8f99-5d4aefb5f745)
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from transformers import AutoTokenizer, AutoModelForCausalLM
2
  import torch
3
  import gradio as gr
4
  import spaces
@@ -15,10 +15,12 @@ try:
15
  except Exception as e:
16
  raise ValueError(f"Error loading Model and Tokenizer: {e}")
17
 
18
- @spaces.GPU
 
19
  def generate_response(user_query: str, system_message: str = None, max_length: int = 1024) -> str:
20
  """
21
  Generates a response based on the given user query.
 
22
  :param user_query: The user's input message.
23
  :param system_message: Custom system instruction (optional, defaults to medical assistant).
24
  :param max_length: Max tokens to generate.
@@ -26,23 +28,36 @@ def generate_response(user_query: str, system_message: str = None, max_length: i
26
  """
27
  if not user_query.strip():
28
  return "Error: User query cannot be empty."
29
-
30
  if system_message is None:
31
  system_message = ("You are a trusted AI-powered medical assistant. "
32
  "Analyze patient queries carefully and provide accurate, professional, and empathetic responses. "
33
  "Prioritize patient safety, adhere to medical best practices, and recommend consulting a healthcare provider when necessary.")
 
 
 
 
 
 
 
 
 
34
 
35
- prompt = f"<|system|> {system_message} <|end|> <|user|> {user_query} <|end|> <|assistant|>"
 
 
 
 
 
36
  try:
37
- inputs = tokenizer(prompt, return_tensors="pt").to(device)
38
- outputs = model.generate(**inputs, max_length=max_length)
39
- # Decode response
40
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
41
- return response.split("<|assistant|>")[-1].strip().split("<|end|>")[0].strip()
42
  except Exception as e:
43
  return f"Error generating response: {e}"
44
 
45
  # Gradio Interface
 
 
46
  def chat_interface(user_query, system_message=None):
47
  response = generate_response(user_query, system_message)
48
  return response
@@ -60,4 +75,6 @@ with gr.Blocks() as demo:
60
 
61
  submit_button.click(chat_interface, inputs=[user_query, system_message], outputs=output)
62
 
63
- demo.launch()
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
2
  import torch
3
  import gradio as gr
4
  import spaces
 
15
  except Exception as e:
16
  raise ValueError(f"Error loading Model and Tokenizer: {e}")
17
 
18
+ @torch.inference_mode()
19
+ @spaces.GPU(duration=120)
20
  def generate_response(user_query: str, system_message: str = None, max_length: int = 1024) -> str:
21
  """
22
  Generates a response based on the given user query.
23
+
24
  :param user_query: The user's input message.
25
  :param system_message: Custom system instruction (optional, defaults to medical assistant).
26
  :param max_length: Max tokens to generate.
 
28
  """
29
  if not user_query.strip():
30
  return "Error: User query cannot be empty."
31
+
32
  if system_message is None:
33
  system_message = ("You are a trusted AI-powered medical assistant. "
34
  "Analyze patient queries carefully and provide accurate, professional, and empathetic responses. "
35
  "Prioritize patient safety, adhere to medical best practices, and recommend consulting a healthcare provider when necessary.")
36
+
37
+ messages = [
38
+ {"role": "system", "content": system_message},
39
+ {'role': "user", "content": user_query}
40
+ ]
41
+
42
+ pipe = pipeline("text-generation",
43
+ model=model,
44
+ tokenizer=tokenizer)
45
 
46
+ generation_args = {
47
+ "max_new_tokens": max_length,
48
+ "return_full_text": False,
49
+ "temperature": 0.0,
50
+ "do_sample": False,
51
+ }
52
  try:
53
+ output = pipe(messages, **generation_args)
54
+ return(output[0]['generated_text'])
 
 
 
55
  except Exception as e:
56
  return f"Error generating response: {e}"
57
 
58
  # Gradio Interface
59
+ @torch.inference_mode()
60
+ @spaces.GPU(duration=120)
61
  def chat_interface(user_query, system_message=None):
62
  response = generate_response(user_query, system_message)
63
  return response
 
75
 
76
  submit_button.click(chat_interface, inputs=[user_query, system_message], outputs=output)
77
 
78
+ demo.launch(share=True)
79
+
80
+