Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,86 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
""
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
""
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
```python
|
2 |
import gradio as gr
|
3 |
+
import random
|
4 |
+
import time
|
5 |
+
def chatbot(message, history, system_prompt):
|
6 |
+
# Simple response generation based on the system prompt and user message
|
7 |
+
if not system_prompt:
|
8 |
+
system_prompt = "I am a helpful AI assistant."
|
9 |
+
# Simulate typing with a stream
|
10 |
+
for i in range(1, 11):
|
11 |
+
thinking_time = random.uniform(0.1, 0.3)
|
12 |
+
time.sleep(thinking_time)
|
13 |
+
# Generate response gradually based on system prompt
|
14 |
+
if "friendly" in system_prompt.lower():
|
15 |
+
response_parts = [
|
16 |
+
"Hi there! ",
|
17 |
+
"I'd be happy to help you with that. ",
|
18 |
+
f"You asked about '{message}'. ",
|
19 |
+
"Based on what I know, I think I can provide some guidance on this topic."
|
20 |
+
]
|
21 |
+
elif "expert" in system_prompt.lower():
|
22 |
+
response_parts = [
|
23 |
+
"Based on my expertise, ",
|
24 |
+
f"regarding '{message}', ",
|
25 |
+
"I can provide you with a detailed analysis. ",
|
26 |
+
"Here's what you should know about this subject from a professional perspective."
|
27 |
+
]
|
28 |
+
elif "funny" in system_prompt.lower():
|
29 |
+
response_parts = [
|
30 |
+
"Well, well, well! ",
|
31 |
+
f"So you want to know about '{message}'? ",
|
32 |
+
"That's quite an interesting question! ",
|
33 |
+
"Let me share my thoughts with a touch of humor."
|
34 |
+
]
|
35 |
+
else:
|
36 |
+
response_parts = [
|
37 |
+
"Thank you for your question. ",
|
38 |
+
f"Regarding '{message}', ",
|
39 |
+
"I can provide the following information: ",
|
40 |
+
"Please let me know if you need any clarification or have follow-up questions."
|
41 |
+
]
|
42 |
+
partial_response = "".join(response_parts[:int(i * len(response_parts) / 10)])
|
43 |
+
yield partial_response
|
44 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
45 |
+
gr.Markdown("# 🤖 Custom AI Chatbot")
|
46 |
+
gr.Markdown("Configure your chatbot's personality and knowledge using the system prompt.")
|
47 |
+
with gr.Row():
|
48 |
+
with gr.Column(scale=2):
|
49 |
+
chatbot = gr.ChatInterface(
|
50 |
+
fn=chatbot,
|
51 |
+
additional_inputs=[
|
52 |
+
gr.Textbox(
|
53 |
+
placeholder="Enter system prompt here...",
|
54 |
+
label="System Prompt",
|
55 |
+
info="Define your chatbot's personality, knowledge, and style",
|
56 |
+
lines=5
|
57 |
+
)
|
58 |
+
],
|
59 |
+
examples=[
|
60 |
+
["What's the weather like today?"],
|
61 |
+
["Can you tell me about quantum physics?"],
|
62 |
+
["Write a short poem about nature."]
|
63 |
+
],
|
64 |
+
title="Chat with Your Custom AI",
|
65 |
+
)
|
66 |
+
with gr.Column(scale=1):
|
67 |
+
gr.Markdown("## System Prompt Examples")
|
68 |
+
system_prompt_examples = gr.Examples(
|
69 |
+
examples=[
|
70 |
+
"You are a friendly assistant who speaks casually and uses simple language.",
|
71 |
+
"You are an expert in technology with deep knowledge of AI, machine learning, and computer science.",
|
72 |
+
"You are a funny comedian who responds with humor and wit.",
|
73 |
+
"You are a professional business consultant providing concise, valuable advice.",
|
74 |
+
"You are a supportive tutor who explains complex concepts in simple terms."
|
75 |
+
],
|
76 |
+
inputs=chatbot.additional_inputs[0],
|
77 |
+
label="Click on an example to use it"
|
78 |
+
)
|
79 |
+
gr.Markdown("""
|
80 |
+
### Tips for System Prompts:
|
81 |
+
- Define personality traits
|
82 |
+
- Specify knowledge domains
|
83 |
+
- Set the tone and speaking style
|
84 |
+
- Include specific instructions
|
85 |
+
- Add any constraints or limitations
|
86 |
+
""")
|