Spaces:
Paused
Paused
Upload folder using huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,12 +1,19 @@
|
|
| 1 |
---
|
| 2 |
-
title: Integrated
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Integrated Text Box
|
| 3 |
+
emoji: 📝
|
| 4 |
+
colorFrom: purple
|
| 5 |
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.31.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
short_description: Talk or type to ANY LLM!
|
| 12 |
+
tags: [webrtc, websocket, gradio, secret|HF_TOKEN]
|
| 13 |
---
|
| 14 |
|
| 15 |
+
# Integrated Textbox
|
| 16 |
+
|
| 17 |
+
Talk or type to ANY LLM!
|
| 18 |
+
|
| 19 |
+
|
app.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# dependencies = [
|
| 3 |
+
# "fastrtc[vad, stt]==0.0.26.rc1",
|
| 4 |
+
# "openai",
|
| 5 |
+
# ]
|
| 6 |
+
# ///
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
import gradio as gr
|
| 10 |
+
import huggingface_hub
|
| 11 |
+
from fastrtc import (
|
| 12 |
+
AdditionalOutputs,
|
| 13 |
+
ReplyOnPause,
|
| 14 |
+
WebRTC,
|
| 15 |
+
WebRTCData,
|
| 16 |
+
WebRTCError,
|
| 17 |
+
get_hf_turn_credentials,
|
| 18 |
+
get_stt_model,
|
| 19 |
+
)
|
| 20 |
+
from gradio.utils import get_space
|
| 21 |
+
from openai import OpenAI
|
| 22 |
+
|
| 23 |
+
stt_model = get_stt_model()
|
| 24 |
+
|
| 25 |
+
conversations = {}
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def response(
|
| 29 |
+
data: WebRTCData,
|
| 30 |
+
conversation: list[dict],
|
| 31 |
+
token: str | None = None,
|
| 32 |
+
model: str = "meta-llama/Llama-3.2-3B-Instruct",
|
| 33 |
+
provider: str = "sambanova",
|
| 34 |
+
):
|
| 35 |
+
print("conversation before", conversation)
|
| 36 |
+
if not provider.startswith("http") and not token:
|
| 37 |
+
raise WebRTCError("Please add your HF token.")
|
| 38 |
+
|
| 39 |
+
if data.audio is not None and data.audio[1].size > 0:
|
| 40 |
+
user_audio_text = stt_model.stt(data.audio)
|
| 41 |
+
conversation.append({"role": "user", "content": user_audio_text})
|
| 42 |
+
else:
|
| 43 |
+
conversation.append({"role": "user", "content": data.textbox})
|
| 44 |
+
|
| 45 |
+
yield AdditionalOutputs(conversation)
|
| 46 |
+
|
| 47 |
+
if provider.startswith("http"):
|
| 48 |
+
client = OpenAI(base_url=provider, api_key="ollama")
|
| 49 |
+
else:
|
| 50 |
+
client = huggingface_hub.InferenceClient(
|
| 51 |
+
api_key=token,
|
| 52 |
+
provider=provider, # type: ignore
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
request = client.chat.completions.create(
|
| 56 |
+
model=model,
|
| 57 |
+
messages=conversation, # type: ignore
|
| 58 |
+
temperature=1,
|
| 59 |
+
top_p=0.1,
|
| 60 |
+
)
|
| 61 |
+
response = {"role": "assistant", "content": request.choices[0].message.content}
|
| 62 |
+
|
| 63 |
+
conversation.append(response)
|
| 64 |
+
print("conversation after", conversation)
|
| 65 |
+
yield AdditionalOutputs(conversation)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
css = """
|
| 69 |
+
footer {
|
| 70 |
+
display: none !important;
|
| 71 |
+
}
|
| 72 |
+
"""
|
| 73 |
+
|
| 74 |
+
providers = [
|
| 75 |
+
"black-forest-labs",
|
| 76 |
+
"cerebras",
|
| 77 |
+
"cohere",
|
| 78 |
+
"fal-ai",
|
| 79 |
+
"fireworks-ai",
|
| 80 |
+
"hf-inference",
|
| 81 |
+
"hyperbolic",
|
| 82 |
+
"nebius",
|
| 83 |
+
"novita",
|
| 84 |
+
"openai",
|
| 85 |
+
"replicate",
|
| 86 |
+
"sambanova",
|
| 87 |
+
"together",
|
| 88 |
+
]
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def hide_token(provider: str):
|
| 92 |
+
if provider.startswith("http"):
|
| 93 |
+
return gr.Textbox(visible=False)
|
| 94 |
+
return gr.skip()
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
with gr.Blocks(css=css) as demo:
|
| 98 |
+
gr.HTML(
|
| 99 |
+
"""
|
| 100 |
+
<h1 style='text-align: center; display: flex; align-items: center; justify-content: center;'>
|
| 101 |
+
<img src="https://huggingface.co/datasets/freddyaboulton/bucket/resolve/main/AV_Huggy.png" alt="Streaming Huggy" style="height: 50px; margin-right: 10px"> FastRTC Chat
|
| 102 |
+
</h1>
|
| 103 |
+
"""
|
| 104 |
+
)
|
| 105 |
+
with gr.Sidebar():
|
| 106 |
+
token = gr.Textbox(
|
| 107 |
+
placeholder="Place your HF token here", type="password", label="HF Token"
|
| 108 |
+
)
|
| 109 |
+
model = gr.Dropdown(
|
| 110 |
+
choices=["meta-llama/Llama-3.2-3B-Instruct"],
|
| 111 |
+
allow_custom_value=True,
|
| 112 |
+
label="Model",
|
| 113 |
+
)
|
| 114 |
+
provider = gr.Dropdown(
|
| 115 |
+
label="Provider",
|
| 116 |
+
choices=providers,
|
| 117 |
+
value="sambanova",
|
| 118 |
+
info="Select a hf-compatible provider or type the url of your server, e.g. http://127.0.0.1:11434/v1 for ollama",
|
| 119 |
+
allow_custom_value=True,
|
| 120 |
+
)
|
| 121 |
+
provider.change(hide_token, inputs=[provider], outputs=[token])
|
| 122 |
+
cb = gr.Chatbot(type="messages", height=600)
|
| 123 |
+
webrtc = WebRTC(
|
| 124 |
+
modality="audio",
|
| 125 |
+
mode="send",
|
| 126 |
+
variant="textbox",
|
| 127 |
+
rtc_configuration=get_hf_turn_credentials if get_space() else None,
|
| 128 |
+
server_rtc_configuration=get_hf_turn_credentials(ttl=3_600 * 24 * 30)
|
| 129 |
+
if get_space()
|
| 130 |
+
else None,
|
| 131 |
+
)
|
| 132 |
+
webrtc.stream(
|
| 133 |
+
ReplyOnPause(response), # type: ignore
|
| 134 |
+
inputs=[webrtc, cb, token, model, provider],
|
| 135 |
+
outputs=[cb],
|
| 136 |
+
concurrency_limit=100,
|
| 137 |
+
)
|
| 138 |
+
webrtc.on_additional_outputs(
|
| 139 |
+
lambda old, new: new, inputs=[cb], outputs=[cb], concurrency_limit=100
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
if __name__ == "__main__":
|
| 143 |
+
demo.launch(server_port=6980)
|