Reality123b commited on
Commit
24342ea
·
verified ·
1 Parent(s): 41d2722

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -262
app.py CHANGED
@@ -1,271 +1,62 @@
1
  import gradio as gr
 
2
  from huggingface_hub import InferenceClient
3
 
4
- # Initialize clients
5
- text_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
- image_client = InferenceClient("SG161222/RealVisXL_V3.0")
7
 
8
- def check_custom_responses(message: str) -> str:
9
- """Check for specific patterns and return custom responses."""
10
- message_lower = message.lower()
11
- custom_responses = {
12
- "what is ur name?": "xylaria",
13
- "what is ur Name?": "xylaria",
14
- "what is Ur name?": "xylaria",
15
- "what is Ur Name?": "xylaria",
16
- "What is ur name?": "xylaria",
17
- "What is ur Name?": "xylaria",
18
- "What is Ur name?": "xylaria",
19
- "What is Ur Name?": "xylaria",
20
- "what's ur name?": "xylaria",
21
- "what's ur Name?": "xylaria",
22
- "what's Ur name?": "xylaria",
23
- "what's Ur Name?": "xylaria",
24
- "whats ur name?": "xylaria",
25
- "whats ur Name?": "xylaria",
26
- "whats Ur name?": "xylaria",
27
- "whats Ur Name?": "xylaria",
28
- "what's your name?": "xylaria",
29
- "what's your Name?": "xylaria",
30
- "what's Your name?": "xylaria",
31
- "what's Your Name?": "xylaria",
32
- "Whats ur name?": "xylaria",
33
- "Whats ur Name?": "xylaria",
34
- "Whats Ur name?": "xylaria",
35
- "Whats Ur Name?": "xylaria",
36
- "What Is Your Name?": "xylaria",
37
- "What Is Ur Name?": "xylaria",
38
- "What Is Your Name?": "xylaria",
39
- "What Is Ur Name?": "xylaria",
40
- "what is your name?": "xylaria",
41
- "what is your Name?": "xylaria",
42
- "what is Your name?": "xylaria",
43
- "what is Your Name?": "xylaria",
44
- "how many 'r' is in strawberry?": "3",
45
- "how many 'R' is in strawberry?": "3",
46
- "how many 'r' Is in strawberry?": "3",
47
- "how many 'R' Is in strawberry?": "3",
48
- "How many 'r' is in strawberry?": "3",
49
- "How many 'R' is in strawberry?": "3",
50
- "How Many 'r' Is In Strawberry?": "3",
51
- "How Many 'R' Is In Strawberry?": "3",
52
- "how many r is in strawberry?": "3",
53
- "how many R is in strawberry?": "3",
54
- "how many r Is in strawberry?": "3",
55
- "how many R Is in strawberry?": "3",
56
- "How many r is in strawberry?": "3",
57
- "How many R is in strawberry?": "3",
58
- "How Many R Is In Strawberry?": "3",
59
- "how many 'r' in strawberry?": "3",
60
- "how many r's are in strawberry?": "3",
61
- "how many Rs are in strawberry?": "3",
62
- "How Many R's Are In Strawberry?": "3",
63
- "How Many Rs Are In Strawberry?": "3",
64
- "who is your developer?": "sk md saad amin",
65
- "who is your Developer?": "sk md saad amin",
66
- "who is Your Developer?": "sk md saad amin",
67
- "who is ur developer?": "sk md saad amin",
68
- "who is ur Developer?": "sk md saad amin",
69
- "who is Your Developer?": "sk md saad amin",
70
- "Who is ur developer?": "sk md saad amin",
71
- "Who is ur Developer?": "sk md saad amin",
72
- "who is ur dev?": "sk md saad amin",
73
- "Who is ur dev?": "sk md saad amin",
74
- "who is your dev?": "sk md saad amin",
75
- "Who is your dev?": "sk md saad amin",
76
- "Who's your developer?": "sk md saad amin",
77
- "Who's ur developer?": "sk md saad amin",
78
- "Who Is Your Developer?": "sk md saad amin",
79
- "Who Is Ur Developer?": "sk md saad amin",
80
- "Who Is Your Dev?": "sk md saad amin",
81
- "Who Is Ur Dev?": "sk md saad amin",
82
- "who's your developer?": "sk md saad amin",
83
- "who's ur developer?": "sk md saad amin",
84
- "who is your devloper?": "sk md saad amin",
85
- "who is ur devloper?": "sk md saad amin",
86
- "how many r is in strawberry?": "3",
87
- "how many R is in strawberry?": "3",
88
- "how many r Is in strawberry?": "3",
89
- "how many R Is in strawberry?": "3",
90
- "How many r is in strawberry?": "3",
91
- "How many R is in strawberry?": "3",
92
- "How Many R Is In Strawberry?": "3",
93
- "how many 'r' is in strawberry?": "3",
94
- "how many 'R' is in strawberry?": "3",
95
- "how many 'r' Is in strawberry?": "3",
96
- "how many 'R' Is in strawberry?": "3",
97
- "How many 'r' is in strawberry?": "3",
98
- "How many 'R' is in strawberry?": "3",
99
- "How Many 'r' Is In Strawberry?": "3",
100
- "How Many 'R' Is In Strawberry?": "3",
101
- "how many r's are in strawberry?": "3",
102
- "how many Rs are in strawberry?": "3",
103
- "How Many R's Are In Strawberry?": "3",
104
- "How Many Rs Are In Strawberry?": "3",
105
- "how many Rs's are in strawberry?": "3",
106
- "wat is ur name?": "xylaria",
107
- "wat is ur Name?": "xylaria",
108
- "wut is ur name?": "xylaria",
109
- "wut ur name?": "xylaria",
110
- "wats ur name?": "xylaria",
111
- "wats ur name": "xylaria",
112
- "who's ur dev?": "sk md saad amin",
113
- "who's your dev?": "sk md saad amin",
114
- "who ur dev?": "sk md saad amin",
115
- "who's ur devloper?": "sk md saad amin",
116
- "how many r in strawbary?": "3",
117
- "how many r in strawbary?": "3",
118
- "how many R in strawbary?": "3",
119
- "how many 'r' in strawbary?": "3",
120
- "how many 'R' in strawbary?": "3",
121
- "how many r in strawbry?": "3",
122
- "how many R in strawbry?": "3",
123
- "how many r is in strawbry?": "3",
124
- "how many 'r' is in strawbry?": "3",
125
- "how many 'R' is in strawbry?": "3",
126
- "who is ur dev": "sk md saad amin",
127
- "who is ur devloper": "sk md saad amin",
128
- "what is ur dev": "sk md saad amin",
129
- "who is ur dev?": "sk md saad amin",
130
- "who is ur dev?": "sk md saad amin",
131
- "whats ur dev?": "sk md saad amin",
132
- }
133
 
134
- for pattern, response in custom_responses.items():
135
- if pattern in message_lower:
136
- return response
137
- return None
138
-
139
- def is_image_request(message: str) -> bool:
140
- """Detect if the message is requesting image generation."""
141
- image_triggers = [
142
- "generate an image",
143
- "create an image",
144
- "draw",
145
- "make a picture",
146
- "generate a picture",
147
- "create a picture",
148
- "generate art",
149
- "create art",
150
- "make art",
151
- "visualize",
152
- "show me",
153
  ]
154
- message_lower = message.lower()
155
- return any(trigger in message_lower for trigger in image_triggers)
156
-
157
- def generate_image(prompt: str) -> str:
158
- """Generate an image using DALLE-4K model."""
159
- try:
160
- response = image_client.text_to_image(
161
- prompt,
162
- parameters={
163
- "negative_prompt": "(worst quality, low quality, illustration, 3d, 2d, painting, cartoons, sketch), open mouth",
164
- "num_inference_steps": 30,
165
- "guidance_scale": 7.5,
166
- "sampling_steps": 15,
167
- "upscaler": "4x-UltraSharp",
168
- "denoising_strength": 0.5,
169
- }
170
- )
171
- return response
172
- except Exception as e:
173
- print(f"Image generation error: {e}")
174
- return None
175
-
176
- def respond(
177
- message,
178
- history: list[tuple[str, str]],
179
- system_message,
180
- max_tokens,
181
- temperature,
182
- top_p,
183
- ):
184
- # First check for custom responses
185
- custom_response = check_custom_responses(message)
186
- if custom_response:
187
- yield custom_response
188
- return
189
-
190
- if is_image_request(message):
191
- try:
192
- image = generate_image(message)
193
- if image:
194
- return f"Here's your generated image based on: {message}"
195
- else:
196
- return "Sorry, I couldn't generate the image. Please try again."
197
- except Exception as e:
198
- return f"An error occurred while generating the image: {str(e)}"
199
-
200
- # Prepare conversation history
201
- messages = [{"role": "system", "content": system_message}]
202
- for val in history:
203
- if val[0]:
204
- messages.append({"role": "user", "content": val[0]})
205
- if val[1]:
206
- messages.append({"role": "assistant", "content": val[1]})
207
 
208
- messages.append({"role": "user", "content": message})
209
-
210
- # Get response from model
 
 
 
 
 
 
211
  response = ""
212
- for message in text_client.chat_completion(
213
- messages,
214
- max_tokens=max_tokens,
215
- stream=True,
216
- temperature=temperature,
217
- top_p=top_p,
218
- ):
219
- token = message.choices[0].delta.content
220
- response += token
221
- yield response
222
-
223
- yield response
224
-
225
- # Custom CSS for the Gradio interface
226
- custom_css = """
227
- @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');
228
- body, .gradio-container {
229
- font-family: 'Inter', sans-serif;
230
- }
231
- """
232
-
233
- # System message
234
- system_message = """
235
- You are Xylaria 1.3 Senoa
236
- """
237
-
238
-
239
- # Gradio chat interface
240
- demo = gr.ChatInterface(
241
- respond,
242
- additional_inputs=[
243
- gr.Textbox(
244
- value=system_message,
245
- visible=False,
246
- ),
247
- gr.Slider(
248
- minimum=1,
249
- maximum=16343,
250
- value=16343,
251
- step=1,
252
- label="Max new tokens"
253
- ),
254
- gr.Slider(
255
- minimum=0.1,
256
- maximum=4.0,
257
- value=0.7,
258
- step=0.1,
259
- label="Temperature"
260
- ),
261
- gr.Slider(
262
- minimum=0.1,
263
- maximum=1.0,
264
- value=0.95,
265
- step=0.05,
266
- label="Top-p (nucleus sampling)"
267
- ),
268
- ],
269
- css=custom_css
270
- )
271
  demo.launch()
 
1
  import gradio as gr
2
+ import os
3
  from huggingface_hub import InferenceClient
4
 
5
+ hf_token = os.getenv("hf_token")
 
 
6
 
7
+ client = InferenceClient(api_key=hf_token)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ def get_response(user_input):
10
+ messages = [
11
+ { "role": "system", "content": "you are xylaria 1.4 senoa, developed by sk md saad amin. You give links for images if the user asks for images." },
12
+ { "role": "user", "content": user_input }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ stream = client.chat.completions.create(
16
+ model="Qwen/QwQ-32B-Preview",
17
+ messages=messages,
18
+ temperature=0.5,
19
+ max_tokens=10240,
20
+ top_p=0.7,
21
+ stream=True
22
+ )
23
+
24
  response = ""
25
+ for chunk in stream:
26
+ response += chunk.choices[0].delta.content
27
+ return response
28
+
29
+ def chat_interface():
30
+ with gr.Blocks() as demo:
31
+ with gr.Row():
32
+ with gr.Column(scale=0.8):
33
+ input_textbox = gr.Textbox(
34
+ label="Type your message",
35
+ placeholder="Type to Xylaria...",
36
+ lines=1,
37
+ max_lines=3,
38
+ interactive=True,
39
+ elem_id="user-input",
40
+ show_label=False
41
+ )
42
+ with gr.Column(scale=0.2):
43
+ send_button = gr.Button("Send", elem_id="send-btn")
44
+
45
+ chat_output = gr.Chatbot(
46
+ elem_id="chat-box",
47
+ label="Xylaria 1.4 Senoa Chatbot",
48
+ show_label=False
49
+ )
50
+
51
+ def submit_input(user_input, chat_history):
52
+ response = get_response(user_input)
53
+ chat_history.append((user_input, response))
54
+ return "", chat_history
55
+
56
+ input_textbox.submit(submit_input, [input_textbox, chat_output], [input_textbox, chat_output])
57
+ send_button.click(submit_input, [input_textbox, chat_output], [input_textbox, chat_output])
58
+
59
+ return demo
60
+
61
+ demo = chat_interface()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  demo.launch()