KaiShin1885 commited on
Commit
69c42e0
·
verified ·
1 Parent(s): a95ddf5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -65
app.py CHANGED
@@ -1,66 +1,86 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
-
5
- """
6
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
7
- """
8
- client = InferenceClient("meta-llama/Llama-3.1-8B-Instruct")
9
-
10
-
11
- def respond(
12
- message,
13
- history: list[tuple[str, str]],
14
- system_message,
15
- max_tokens,
16
- temperature,
17
- top_p,
18
- ):
19
-
20
-
21
- messages = [{"role": "system", "content": system_message}]
22
-
23
- for val in history:
24
- if val[0]:
25
- messages.append({"role": "user", "content": val[0]})
26
- if val[1]:
27
- messages.append({"role": "assistant", "content": val[1]})
28
-
29
- messages.append({"role": "user", "content": message})
30
-
31
- response = ""
32
-
33
- for message in client.chat_completion(
34
- messages,
35
- max_tokens=max_tokens,
36
- stream=True,
37
- temperature=temperature,
38
- top_p=top_p,
39
- ):
40
- token = message.choices[0].delta.content
41
-
42
- response += token
43
- yield response
44
-
45
- """
46
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
47
- """
48
- demo = gr.ChatInterface(
49
- respond,
50
- additional_inputs=[
51
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
52
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
53
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
54
- gr.Slider(
55
- minimum=0.1,
56
- maximum=1.0,
57
- value=0.95,
58
- step=0.05,
59
- label="Top-p (nucleus sampling)",
60
- ),
61
- ],
62
- )
63
-
64
-
65
- if __name__ == "__main__":
66
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """)