Binarybardakshat commited on
Commit
8c230d7
·
verified ·
1 Parent(s): 60d0165

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from huggingface_hub import InferenceClient
3
+ import gradio as gr
4
+
5
+ # Load Hugging Face token from .env
6
+ hf_token = os.getenv("HF_TOKEN")
7
+
8
+ # Define available models
9
+ models = {
10
+ "Llama-3.3-70B-Instruct": "meta-llama/llama-3.3-70B-instruct",
11
+ "Kuno-K1-Llama-3.2-3b":"VinkuraAI/Kuno-K1-Llama-3.2-3b",
12
+ }
13
+
14
+ # Initialize the InferenceClient with a selected model
15
+ def get_inference_client(selected_model):
16
+ return InferenceClient(
17
+ models[selected_model],
18
+ token=hf_token,
19
+ )
20
+
21
+ # Function to get a response from the chatbot
22
+ def get_response(user_input, history, selected_model, system_prompt, temperature, max_tokens, top_p):
23
+ client = get_inference_client(selected_model)
24
+
25
+ # messaages
26
+ messages = []
27
+
28
+ # Add system message, if not empty
29
+ if (len(system_prompt)) > 0:
30
+ messages = [{"role": "system", "content": system_prompt}]
31
+
32
+ # Include previous conversation history
33
+ for h in history:
34
+ messages.append({"role": h['role'], "content": h['content']})
35
+
36
+ # Add the current user input to the messages
37
+ messages.append({"role": "user", "content": user_input})
38
+
39
+ # Get response from the model
40
+ response = client.chat_completion(
41
+ messages,
42
+ max_tokens=max_tokens,
43
+ temperature=temperature,
44
+ top_p=top_p,
45
+ )
46
+
47
+ bot_response = response.choices[0].message.content
48
+ history.append({"role": "user", "content": user_input})
49
+ history.append({"role": "assistant", "content": bot_response})
50
+
51
+ return history
52
+
53
+ # Gradio interface
54
+ with gr.Blocks() as demo:
55
+ with gr.Row():
56
+ with gr.Column(scale=2):
57
+ # Set the type to 'messages' to avoid the deprecation warning
58
+ chatbot = gr.Chatbot(type="messages")
59
+ with gr.Row():
60
+ user_input = gr.Textbox(show_label=False, placeholder="Enter your message...")
61
+ send_button = gr.Button("Send")
62
+ with gr.Column(scale=1):
63
+ with gr.Accordion("Settings", open=False):
64
+ # Model selection
65
+ selected_model = gr.Dropdown(choices=list(models.keys()), label="Select Model", value="Llama-3.3-70B-Instruct")
66
+
67
+ # Chat settings
68
+ system_prompt = gr.Textbox(value="You are a friendly and open-minded chatbot.", label="System Prompt (Optional)", lines=5)
69
+ temperature = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=0.7, label="Temperature")
70
+ max_tokens = gr.Slider(minimum=10, maximum=8192, step=10, value=250, label="Max Tokens")
71
+ top_p = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=0.9, label="Top-p")
72
+
73
+ # Chatbot interaction
74
+ def submit_message(user_input, history, selected_model, system_prompt, temperature, max_tokens, top_p):
75
+ # Get updated history including user input and bot response
76
+ history = get_response(user_input, history, selected_model, system_prompt, temperature, max_tokens, top_p)
77
+ return "", history
78
+
79
+ # Set up the send button click functionality
80
+ send_button.click(
81
+ submit_message,
82
+ [user_input, chatbot, selected_model, system_prompt, temperature, max_tokens, top_p],
83
+ [user_input, chatbot]
84
+ )
85
+
86
+ # Trigger sending message when Enter key is pressed
87
+ user_input.submit(
88
+ submit_message,
89
+ [user_input, chatbot, selected_model, system_prompt, temperature, max_tokens, top_p],
90
+ [user_input, chatbot]
91
+ )
92
+
93
+ # Launch the Gradio interface
94
+ demo.launch()