Commit
·
fc3e670
1
Parent(s):
93a3d94
chatbot_ui v2
Browse files- app.py +95 -67
- requirements.txt +4 -1
app.py
CHANGED
|
@@ -1,5 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import speech_recognition as sr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def speech_to_text(audio):
|
| 5 |
recognizer = sr.Recognizer()
|
|
@@ -16,105 +58,91 @@ def speech_to_text(audio):
|
|
| 16 |
text = f"Could not process the audio, please try to record one more time"
|
| 17 |
return text
|
| 18 |
|
| 19 |
-
def
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def llm_ui():
|
| 30 |
with gr.Blocks() as demo:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
chat_history.append((message, bot_message))
|
| 36 |
-
return "", chat_history
|
| 37 |
-
|
| 38 |
-
def clear_chat(chat_history):
|
| 39 |
-
return []
|
| 40 |
-
|
| 41 |
-
def load_previous_conversation():
|
| 42 |
-
# Logic to load previous conversation
|
| 43 |
-
return []
|
| 44 |
-
|
| 45 |
-
def transcribe_audio(audio):
|
| 46 |
-
text = speech_to_text(audio)
|
| 47 |
-
return text
|
| 48 |
|
| 49 |
with gr.Row():
|
| 50 |
chatbot = gr.Chatbot()
|
|
|
|
|
|
|
|
|
|
| 51 |
with gr.Row():
|
| 52 |
-
model_selection = gr.Dropdown(
|
| 53 |
clear_button = gr.Button("Clear Chat", scale=2)
|
| 54 |
-
|
| 55 |
-
with gr.
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
with gr.Row():
|
| 59 |
voice_input = gr.Microphone(type="filepath", label="Voice Input", scale=7)
|
| 60 |
voice_button = gr.Button("Use Audio as User Input", scale=3)
|
| 61 |
voice_button.click(transcribe_audio, inputs=voice_input, outputs=user_input)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
clear_button.click(clear_chat, [chatbot], [chatbot])
|
| 65 |
-
load_button.click(load_previous_conversation, [], [chatbot])
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
return demo
|
| 73 |
|
| 74 |
def multimodal_llm_ui():
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
chat_history.append((message, bot_message))
|
| 81 |
-
return "", chat_history
|
| 82 |
-
|
| 83 |
-
def clear_chat(chat_history):
|
| 84 |
-
return []
|
| 85 |
-
|
| 86 |
-
def load_previous_conversation():
|
| 87 |
-
# Logic to load previous conversation
|
| 88 |
-
return []
|
| 89 |
-
|
| 90 |
-
def transcribe_audio(audio):
|
| 91 |
-
text = speech_to_text(audio)
|
| 92 |
-
return text
|
| 93 |
|
| 94 |
with gr.Row():
|
| 95 |
chatbot = gr.Chatbot(height=550)
|
| 96 |
with gr.Column():
|
| 97 |
with gr.Row():
|
| 98 |
-
model_selection = gr.Dropdown(
|
| 99 |
clear_button = gr.Button("Clear Chat", scale=2)
|
| 100 |
-
|
| 101 |
-
|
|
|
|
| 102 |
image_input = gr.Image(type="filepath", label="Input your Image Here....")
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
| 106 |
|
| 107 |
with gr.Row():
|
| 108 |
voice_input = gr.Microphone(type="filepath", label="Voice Input", scale=7)
|
| 109 |
voice_button = gr.Button("Use Audio as User Input", scale=3)
|
| 110 |
voice_button.click(transcribe_audio, inputs=voice_input, outputs=user_input)
|
| 111 |
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
clear_button.click(clear_chat, [chatbot], [chatbot])
|
| 114 |
-
load_button.click(load_previous_conversation, [], [chatbot])
|
| 115 |
return demo
|
| 116 |
|
| 117 |
-
demo = gr.TabbedInterface([llm_ui(), multimodal_llm_ui()], ["LLM", "Image + LLM"]
|
| 118 |
-
theme='snehilsanyal/scikit-learn')
|
| 119 |
|
| 120 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import speech_recognition as sr
|
| 3 |
+
from datetime import datetime, timedelta
|
| 4 |
+
import os
|
| 5 |
+
import threading
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
system_prompt = """
|
| 12 |
+
You are an AI coding assistant designed to solve coding problems and provide code snippets based on the user's query. When given a query, follow these guidelines.
|
| 13 |
+
1. Return only the necessary and helpful code.
|
| 14 |
+
2. Include any related details that enhance understanding or usability of the code.
|
| 15 |
+
3. Ensure the code is clean, efficient, and follows best practices.
|
| 16 |
+
4. Add comments to explain complex or non-obvious parts of the code.
|
| 17 |
+
5. If there are multiple possible solutions, provide the most optimal one first.
|
| 18 |
+
"""
|
| 19 |
+
ip_requests = {}
|
| 20 |
+
ip_requests_lock = threading.Lock()
|
| 21 |
+
|
| 22 |
+
def allow_ip(request: gr.Request, show_error=True):
|
| 23 |
+
ip = request.headers.get("X-Forwarded-For")
|
| 24 |
+
now = datetime.now()
|
| 25 |
+
window = timedelta(hours=24)
|
| 26 |
+
with ip_requests_lock:
|
| 27 |
+
if ip in ip_requests:
|
| 28 |
+
ip_requests[ip] = [timestamp for timestamp in ip_requests[ip] if now - timestamp < window]
|
| 29 |
+
if len(ip_requests.get(ip, [])) >= 15:
|
| 30 |
+
raise gr.Error("Rate limit exceeded. Please try again tomorrow or use your Hugging Face Pro token.", visible=show_error)
|
| 31 |
+
ip_requests.setdefault(ip, []).append(now)
|
| 32 |
+
print("ip_requests", ip_requests)
|
| 33 |
+
return True
|
| 34 |
+
|
| 35 |
+
def inference(prompt, hf_token, model, model_name, max_new_tokens):
|
| 36 |
+
messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": prompt}]
|
| 37 |
+
if hf_token is None or not hf_token.strip():
|
| 38 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 39 |
+
client = InferenceClient(model=model, token=hf_token)
|
| 40 |
+
tokens = f"**`{model_name}`**\n\n"
|
| 41 |
+
for completion in client.chat_completion(messages, max_tokens=max_new_tokens, stream=True):
|
| 42 |
+
token = completion.choices[0].delta.content
|
| 43 |
+
tokens += token
|
| 44 |
+
yield tokens
|
| 45 |
|
| 46 |
def speech_to_text(audio):
|
| 47 |
recognizer = sr.Recognizer()
|
|
|
|
| 58 |
text = f"Could not process the audio, please try to record one more time"
|
| 59 |
return text
|
| 60 |
|
| 61 |
+
def respond(message, chat_history, system_prompt, hf_token, model_id, max_new_tokens):
|
| 62 |
+
bot_message = ""
|
| 63 |
+
for token in inference(message, hf_token, model_id, model_id.split("/")[-1], max_new_tokens):
|
| 64 |
+
bot_message += token
|
| 65 |
+
chat_history.append((message, bot_message))
|
| 66 |
+
yield "", chat_history
|
| 67 |
+
|
| 68 |
+
def clear_chat(chat_history):
|
| 69 |
+
return []
|
| 70 |
+
|
| 71 |
+
def transcribe_audio(audio):
|
| 72 |
+
text = speech_to_text(audio)
|
| 73 |
+
return text
|
| 74 |
|
| 75 |
def llm_ui():
|
| 76 |
with gr.Blocks() as demo:
|
| 77 |
+
|
| 78 |
+
model_ids_llm = ["Mistralai/Mistral-7B-Instruct-v0.2", "meta-llama/Meta-Llama-3-8B-Instruct", "meta-llama/Meta-Llama-3.1-8B-Instruct"]
|
| 79 |
+
|
| 80 |
+
gr.Markdown("# AI Coding Assistant")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
chatbot = gr.Chatbot()
|
| 84 |
+
|
| 85 |
+
hf_token_box = gr.Textbox(lines=1, placeholder="Your Hugging Face token - Check if you have access to selected model", label="Hugging Face Token", type="password")
|
| 86 |
+
|
| 87 |
with gr.Row():
|
| 88 |
+
model_selection = gr.Dropdown(choices=model_ids_llm, value=model_ids_llm[0], label="Model", scale=3)
|
| 89 |
clear_button = gr.Button("Clear Chat", scale=2)
|
| 90 |
+
|
| 91 |
+
with gr.Group():
|
| 92 |
+
with gr.Row():
|
| 93 |
+
user_input = gr.Textbox(placeholder="Type your coding problem here...", label="User Input", show_label=False, scale=8)
|
| 94 |
+
send_button = gr.Button("Send", scale=2, variant = "primary")
|
| 95 |
with gr.Row():
|
| 96 |
voice_input = gr.Microphone(type="filepath", label="Voice Input", scale=7)
|
| 97 |
voice_button = gr.Button("Use Audio as User Input", scale=3)
|
| 98 |
voice_button.click(transcribe_audio, inputs=voice_input, outputs=user_input)
|
| 99 |
|
| 100 |
+
max_new_tokens_slider = gr.Slider(minimum=50, maximum=2000, value=500, step=10, label="Max New Tokens", info="Maximum number of tokens to generate in the response.")
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
# Wrap system_prompt in a Gradio component
|
| 103 |
+
system_prompt_component = gr.State(value=system_prompt)
|
| 104 |
+
|
| 105 |
+
send_button.click(respond, [user_input, chatbot, system_prompt_component, hf_token_box, model_selection, max_new_tokens_slider], [user_input, chatbot], scroll_to_output=True)
|
| 106 |
+
clear_button.click(clear_chat, [chatbot], [chatbot])
|
| 107 |
return demo
|
| 108 |
|
| 109 |
def multimodal_llm_ui():
|
| 110 |
with gr.Blocks() as demo:
|
| 111 |
+
|
| 112 |
+
model_ids_multimodal = ["Model-1", "Model-2", "Model-3"]
|
| 113 |
+
|
| 114 |
+
gr.Markdown("# Coding Vision Model")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
with gr.Row():
|
| 117 |
chatbot = gr.Chatbot(height=550)
|
| 118 |
with gr.Column():
|
| 119 |
with gr.Row():
|
| 120 |
+
model_selection = gr.Dropdown(choices=model_ids_multimodal, value=model_ids_multimodal[0], label="Select Model", scale=3)
|
| 121 |
clear_button = gr.Button("Clear Chat", scale=2)
|
| 122 |
+
|
| 123 |
+
hf_token_box = gr.Textbox(lines=1, placeholder="Your Hugging Face token - Check if you have access to selected model", label="Hugging Face Token", type="password")
|
| 124 |
+
|
| 125 |
image_input = gr.Image(type="filepath", label="Input your Image Here....")
|
| 126 |
+
|
| 127 |
+
with gr.Group():
|
| 128 |
+
with gr.Row():
|
| 129 |
+
user_input = gr.Textbox(placeholder="Type your problem here...", label="User Input", show_label=False, scale=8)
|
| 130 |
+
send_button = gr.Button("Send", scale=2, elem_id="send-button", variant = "primary")
|
| 131 |
|
| 132 |
with gr.Row():
|
| 133 |
voice_input = gr.Microphone(type="filepath", label="Voice Input", scale=7)
|
| 134 |
voice_button = gr.Button("Use Audio as User Input", scale=3)
|
| 135 |
voice_button.click(transcribe_audio, inputs=voice_input, outputs=user_input)
|
| 136 |
|
| 137 |
+
max_new_tokens_slider = gr.Slider(minimum=50, maximum=2000, value=500, step=10, label="Max New Tokens", info="Maximum number of tokens to generate in the response.")
|
| 138 |
+
|
| 139 |
+
# Wrap system_prompt in a Gradio component
|
| 140 |
+
system_prompt_component = gr.State(value=system_prompt)
|
| 141 |
+
|
| 142 |
+
send_button.click(respond, [user_input, chatbot, system_prompt_component, hf_token_box, model_selection, max_new_tokens_slider], [user_input, chatbot])
|
| 143 |
clear_button.click(clear_chat, [chatbot], [chatbot])
|
|
|
|
| 144 |
return demo
|
| 145 |
|
| 146 |
+
demo = gr.TabbedInterface([llm_ui(), multimodal_llm_ui()], ["LLM", "Image + LLM"])
|
|
|
|
| 147 |
|
| 148 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1 +1,4 @@
|
|
| 1 |
-
SpeechRecognition
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
SpeechRecognition
|
| 2 |
+
gradio
|
| 3 |
+
dotenv
|
| 4 |
+
huggingface_hub
|