bharatcoder commited on
Commit
fa6db27
·
verified ·
1 Parent(s): 84be134

Initial commit

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