Aditya0619 commited on
Commit
3f6e910
·
verified ·
1 Parent(s): eb468a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -57
app.py CHANGED
@@ -1,60 +1,79 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
- # Load model directly
4
- pipe = pipeline("text-generation", model="Aditya0619/Medbot")
5
-
6
- def respond(
7
- message,
8
- history: list[tuple[str, str]],
9
- system_message,
10
- max_tokens,
11
- temperature,
12
- top_p,
13
- ):
14
- messages = [{"role": "system", "content": system_message}]
15
-
16
- for val in history:
17
- if val[0]:
18
- messages.append({"role": "user", "content": val[0]})
19
- if val[1]:
20
- messages.append({"role": "assistant", "content": val[1]})
21
-
22
- messages.append({"role": "user", "content": message})
23
-
24
- response = ""
25
-
26
- for message in client.chat_completion(
27
- messages,
28
- max_tokens=max_tokens,
29
- stream=True,
30
  temperature=temperature,
31
  top_p=top_p,
32
- ):
33
- token = message.choices[0].delta.content
34
-
35
- response += token
36
- yield response
37
-
38
-
39
- """
40
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
41
- """
42
- demo = gr.ChatInterface(
43
- respond,
44
- additional_inputs=[
45
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
46
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
47
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
48
- gr.Slider(
49
- minimum=0.1,
50
- maximum=1.0,
51
- value=0.95,
52
- step=0.05,
53
- label="Top-p (nucleus sampling)",
54
- ),
55
- ],
56
- )
57
-
58
-
59
- if __name__ == "__main__":
60
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline, Conversation
3
+
4
+ # Initialize the model pipeline (Hugging Face conversational model)
5
+ chatbot_pipeline = pipeline("text-generation", model="Aditya0619/Medbot")
6
+
7
+ # Define the bot's response function
8
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
9
+ # If no conversation history, initialize an empty one
10
+ if history is None:
11
+ history = []
12
+
13
+ # Build the conversation object with past messages
14
+ conversation = Conversation()
15
+ for user_input, bot_response in history:
16
+ conversation.add_user_input(user_input)
17
+ conversation.append_response(bot_response)
18
+
19
+ # Add the latest user message
20
+ conversation.add_user_input(message)
21
+
22
+ # Generate a response using the chatbot pipeline
23
+ result = chatbot_pipeline(
24
+ conversation,
25
+ max_length=max_tokens,
 
 
 
 
26
  temperature=temperature,
27
  top_p=top_p,
28
+ pad_token_id=50256 # Avoid padding errors for some models like GPT-2 variants
29
+ )
30
+
31
+ # Get the latest response from the model
32
+ bot_response = result.generated_responses[-1]
33
+
34
+ # Update the history with the new exchange
35
+ history.append((message, bot_response))
36
+ return history, history
37
+
38
+ # Define the UI components and layout
39
+ with gr.Blocks() as demo:
40
+ # Title and description
41
+ gr.Markdown("# 🤖 AI Chatbot with Memory\nThis chatbot remembers your previous messages.")
42
+
43
+ # Input fields for system message and settings
44
+ system_message = gr.Textbox(
45
+ label="System Message (Optional)",
46
+ placeholder="e.g., You are a helpful assistant."
47
+ )
48
+ max_tokens = gr.Slider(
49
+ label="Max Tokens", minimum=50, maximum=500, value=250, step=10
50
+ )
51
+ temperature = gr.Slider(
52
+ label="Temperature", minimum=0.0, maximum=1.0, value=0.7, step=0.1
53
+ )
54
+ top_p = gr.Slider(
55
+ label="Top P", minimum=0.0, maximum=1.0, value=0.9, step=0.1
56
+ )
57
+
58
+ # Chatbot interface
59
+ chatbot = gr.Chatbot(label="Chat with AI")
60
+ user_input = gr.Textbox(label="Your Message", placeholder="Type a message...")
61
+
62
+ # Hidden state to store conversation history
63
+ state = gr.State([])
64
+
65
+ # Submit button to trigger the response
66
+ submit = gr.Button("Send")
67
+
68
+ # Link the input and chatbot response function
69
+ submit.click(
70
+ respond,
71
+ inputs=[user_input, state, system_message, max_tokens, temperature, top_p],
72
+ outputs=[chatbot, state]
73
+ )
74
+
75
+ # Display an initial greeting message
76
+ demo.load(lambda: [("Hi! How can I assist you today?", "")], outputs=chatbot)
77
+
78
+ # Launch the Gradio app
79
+ demo.launch()