elsayan123123 commited on
Commit
5aafbb1
·
unverified ·
0 Parent(s):

initiate the repository

Browse files
Files changed (3) hide show
  1. README.md +14 -0
  2. app.py +144 -0
  3. requirements.txt +1 -0
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Lab2
3
+ emoji: 💬
4
+ colorFrom: yellow
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 5.0.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ short_description: lab2
12
+ ---
13
+
14
+ An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
app.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+
4
+ client = InferenceClient("xiaojingyan/lora_model_r16_merged16")
5
+
6
+
7
+ def respond(
8
+ message,
9
+ history: list[tuple[str, str]],
10
+ system_message,
11
+ max_tokens,
12
+ temperature,
13
+ top_p,
14
+ ):
15
+ messages = [{"role": "system", "content": system_message}]
16
+
17
+ for val in history:
18
+ if val[0]:
19
+ messages.append({"role": "user", "content": val[0]})
20
+ if val[1]:
21
+ messages.append({"role": "assistant", "content": val[1]})
22
+
23
+ messages.append({"role": "user", "content": message})
24
+
25
+ response = ""
26
+
27
+ for message in client.chat_completion(
28
+ messages,
29
+ max_tokens=max_tokens,
30
+ stream=True,
31
+ temperature=temperature,
32
+ top_p=top_p,
33
+ ):
34
+ token = message.choices[0].delta.content
35
+
36
+ response += token
37
+ yield response
38
+
39
+
40
+ def chat_interface():
41
+ with gr.Blocks(css="""
42
+ #send_button {
43
+ background-color: grey;
44
+ color: white;
45
+ border: none;
46
+ padding: 8px 16px;
47
+ font-size: 16px;
48
+ border-radius: 4px;
49
+ cursor: not-allowed;
50
+ }
51
+ #send_button.active {
52
+ background-color: blue;
53
+ cursor: pointer;
54
+ }
55
+ """) as demo:
56
+ gr.Markdown(
57
+ """
58
+ ## 🤖 Chatbot Interface
59
+ Welcome to the enhanced chatbot interface! Customize settings below and interact with the bot in the chat window.
60
+ """
61
+ )
62
+
63
+ with gr.Row():
64
+ with gr.Column(scale=2):
65
+ chat = gr.Chatbot() # Default Chatbot component for user and assistant
66
+ msg = gr.Textbox(
67
+ placeholder="Type your message here...",
68
+ label="Your Message",
69
+ lines=1,
70
+ interactive=True,
71
+ )
72
+ submit = gr.Button("Send", elem_id="send_button")
73
+ typing_indicator = gr.Markdown("") # Placeholder for typing indicator
74
+
75
+ with gr.Column(scale=1):
76
+ gr.Markdown("### Settings")
77
+ system_message = gr.Textbox(
78
+ value="You are a friendly chatbot.",
79
+ label="System Message",
80
+ lines=3,
81
+ )
82
+ max_tokens = gr.Slider(
83
+ minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"
84
+ )
85
+ temperature = gr.Slider(
86
+ minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"
87
+ )
88
+ top_p = gr.Slider(
89
+ minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"
90
+ )
91
+ reset_button = gr.Button("Reset Chat") # Reset button to clear history
92
+
93
+ history = gr.State([]) # Chat history state
94
+
95
+ # Define interaction logic
96
+ def user_input(
97
+ user_message, chat_history, system_msg, max_t, temp, top_p_val
98
+ ):
99
+ if user_message:
100
+ chat_history.append((user_message, None)) # Add user message
101
+ yield chat_history, "", "Assistant is typing..."
102
+ response = respond(
103
+ user_message, chat_history, system_msg, max_t, temp, top_p_val
104
+ )
105
+ for partial_response in response:
106
+ chat_history[-1] = (user_message, partial_response) # Update assistant response
107
+ yield chat_history, "", "Assistant is typing..."
108
+ yield chat_history, "", ""
109
+
110
+ submit.click(
111
+ user_input,
112
+ inputs=[msg, history, system_message, max_tokens, temperature, top_p],
113
+ outputs=[chat, msg, typing_indicator],
114
+ show_progress=True,
115
+ )
116
+
117
+ msg.submit(
118
+ user_input,
119
+ inputs=[msg, history, system_message, max_tokens, temperature, top_p],
120
+ outputs=[chat, msg, typing_indicator],
121
+ show_progress=True,
122
+ )
123
+
124
+ # Change button class dynamically
125
+ def toggle_button_color(text):
126
+ if text.strip():
127
+ return gr.update(elem_classes=["active"])
128
+ else:
129
+ return gr.update(elem_classes=[])
130
+
131
+ msg.change(toggle_button_color, inputs=msg, outputs=submit)
132
+
133
+ # Reset chat
134
+ def reset_chat():
135
+ return [], "", "", [] # Clear chat, message, typing indicator, and history state
136
+
137
+ reset_button.click(reset_chat, inputs=[], outputs=[chat, msg, typing_indicator, history])
138
+
139
+ return demo
140
+
141
+
142
+ if __name__ == "__main__":
143
+ demo = chat_interface()
144
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ huggingface_hub==0.25.2