WSLINMSAI commited on
Commit
b2693c6
·
verified ·
1 Parent(s): 2925a06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -28
app.py CHANGED
@@ -1,55 +1,68 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- # Initialize the Hugging Face pipeline with a more advanced model
5
- # Replace "EleutherAI/gpt-neo-2.7B" with other models like "mosaicml/mpt-7b-chat" or "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5"
6
- generation_pipeline = pipeline(
7
- "text-generation",
8
- model="EleutherAI/gpt-neo-2.7B",
9
- device=-1 # Use CPU explicitly
10
- )
11
 
12
  def dental_chatbot_response(message, history):
13
  """
14
- Responds to user queries with a focus on dental terminology.
15
- - Dynamically generates responses using an advanced LLM.
16
- - Designed to address dental-related questions or provide general responses.
17
  """
18
- print(f"User Input: {message}")
19
- print(f"Chat History: {history}")
20
-
21
- # Add a prompt to guide the LLM's focus on dental terminology
22
  prompt = (
23
- f"You are a highly knowledgeable and friendly dental expert chatbot. "
24
- f"Provide detailed and accurate explanations of dental terms, procedures, and treatments. "
25
- f"If the query is not dental-related, respond helpfully and informatively.\n\n"
26
- f"User: {message}\n\n"
27
- f"Chatbot:"
28
  )
29
 
30
- # Generate a response using the LLM
31
- generated = generation_pipeline(
32
  prompt,
33
- max_length=200, # Increase max_length for more detailed responses
34
  num_return_sequences=1,
35
  do_sample=True,
36
- top_p=0.9, # Nucleus sampling for diverse responses
37
  top_k=50 # Top-k sampling for quality control
38
  )
39
 
40
- # Extract the chatbot's response
41
- ai_response = generated[0]["generated_text"].split("Chatbot:")[1].strip()
42
- print(f"Dental Chatbot Response: {ai_response}")
 
 
 
43
  return ai_response
44
 
45
- # Gradio ChatInterface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  demo = gr.ChatInterface(
47
  fn=dental_chatbot_response,
48
  title="Advanced Dental Terminology Chatbot",
49
  description=(
50
  "Ask me anything about dental terms, procedures, and treatments! "
51
  "This chatbot is powered by an advanced LLM for detailed and accurate answers."
52
- )
 
53
  )
54
 
55
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import functools
4
 
5
+ # Cache the pipeline so it loads only once
6
+ @functools.lru_cache(maxsize=1)
7
+ def load_pipeline():
8
+ return pipeline("text-generation", model="EleutherAI/gpt-neo-125M", device=-1)
 
 
 
9
 
10
  def dental_chatbot_response(message, history):
11
  """
12
+ Generates responses focused on dental terminology.
 
 
13
  """
 
 
 
 
14
  prompt = (
15
+ "You are a highly knowledgeable and friendly dental expert chatbot. "
16
+ "Provide detailed and accurate explanations of dental terms, procedures, and treatments. "
17
+ "If the query is not dental-related, respond helpfully and informatively.\n\n"
18
+ f"User: {message}\n\nChatbot:"
 
19
  )
20
 
21
+ gen_pipe = load_pipeline()
22
+ generated = gen_pipe(
23
  prompt,
24
+ max_length=200, # Adjust for desired detail vs. speed
25
  num_return_sequences=1,
26
  do_sample=True,
27
+ top_p=0.9, # Nucleus sampling for diverse outputs
28
  top_k=50 # Top-k sampling for quality control
29
  )
30
 
31
+ generated_text = generated[0]["generated_text"]
32
+ # Safely extract the chatbot's response
33
+ if "Chatbot:" in generated_text:
34
+ ai_response = generated_text.split("Chatbot:")[1].strip()
35
+ else:
36
+ ai_response = generated_text.strip()
37
  return ai_response
38
 
39
+ # Custom CSS to enhance the interface look
40
+ custom_css = """
41
+ body {
42
+ background: #f8f9fa;
43
+ font-family: Arial, sans-serif;
44
+ }
45
+ .gradio-container {
46
+ background: #ffffff;
47
+ border-radius: 10px;
48
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
49
+ margin: 20px;
50
+ padding: 20px;
51
+ }
52
+ .gradio-title, .gradio-description {
53
+ text-align: center;
54
+ }
55
+ """
56
+
57
+ # Define the Gradio ChatInterface with the custom styling
58
  demo = gr.ChatInterface(
59
  fn=dental_chatbot_response,
60
  title="Advanced Dental Terminology Chatbot",
61
  description=(
62
  "Ask me anything about dental terms, procedures, and treatments! "
63
  "This chatbot is powered by an advanced LLM for detailed and accurate answers."
64
+ ),
65
+ css=custom_css
66
  )
67
 
68
  if __name__ == "__main__":