Reality123b commited on
Commit
936b915
·
verified ·
1 Parent(s): 56babd1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +271 -0
app.py ADDED
@@ -0,0 +1,271 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()