freddyaboulton HF staff commited on
Commit
0cf4b8d
·
verified ·
1 Parent(s): 3d9332b

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +20 -11
app.py CHANGED
@@ -7,6 +7,7 @@ import gradio as gr
7
  import numpy as np
8
  from dotenv import load_dotenv
9
  from elevenlabs import ElevenLabs
 
10
  from fastapi.responses import HTMLResponse, StreamingResponse
11
  from fastrtc import (
12
  AdditionalOutputs,
@@ -15,7 +16,7 @@ from fastrtc import (
15
  get_tts_model,
16
  get_twilio_turn_credentials,
17
  )
18
- from fastrtc.utils import aggregate_bytes_to_16bit, audio_to_bytes
19
  from gradio.utils import get_space
20
  from groq import Groq
21
  from pydantic import BaseModel
@@ -44,6 +45,7 @@ def response(
44
  ).text
45
  print("prompt", prompt)
46
  chatbot.append({"role": "user", "content": prompt})
 
47
  messages.append({"role": "user", "content": prompt})
48
  response = claude_client.messages.create(
49
  model="claude-3-5-haiku-20241022",
@@ -56,7 +58,6 @@ def response(
56
  if getattr(block, "type", None) == "text"
57
  )
58
  chatbot.append({"role": "assistant", "content": response_text})
59
- yield AdditionalOutputs(chatbot)
60
  import time
61
 
62
  start = time.time()
@@ -66,6 +67,7 @@ def response(
66
  print("chunk", i, time.time() - start)
67
  yield chunk
68
  print("finished tts", time.time() - start)
 
69
 
70
 
71
  chatbot = gr.Chatbot(type="messages")
@@ -91,7 +93,11 @@ class InputData(BaseModel):
91
  chatbot: list[Message]
92
 
93
 
94
- @stream.get("/")
 
 
 
 
95
  async def _():
96
  rtc_config = get_twilio_turn_credentials() if get_space() else None
97
  html_content = (curr_dir / "index.html").read_text()
@@ -99,19 +105,20 @@ async def _():
99
  return HTMLResponse(content=html_content, status_code=200)
100
 
101
 
102
- @stream.post("/input_hook")
103
  async def _(body: InputData):
104
  stream.set_input(body.webrtc_id, body.model_dump()["chatbot"])
105
  return {"status": "ok"}
106
 
107
 
108
- @stream.get("/outputs")
109
  def _(webrtc_id: str):
110
  async def output_stream():
111
  async for output in stream.output_stream(webrtc_id):
112
  chatbot = output.args[0]
113
- yield f"event: output\ndata: {json.dumps(chatbot[-2])}\n\n"
114
- yield f"event: output\ndata: {json.dumps(chatbot[-1])}\n\n"
 
115
 
116
  return StreamingResponse(output_stream(), media_type="text/event-stream")
117
 
@@ -119,9 +126,11 @@ def _(webrtc_id: str):
119
  if __name__ == "__main__":
120
  import os
121
 
122
- if not os.getenv("PHONE"):
 
 
 
 
123
  import uvicorn
124
 
125
- s = uvicorn.run(stream, host="0.0.0.0", port=7860)
126
- else:
127
- stream.fastphone(host="0.0.0.0", port=7860)
 
7
  import numpy as np
8
  from dotenv import load_dotenv
9
  from elevenlabs import ElevenLabs
10
+ from fastapi import FastAPI
11
  from fastapi.responses import HTMLResponse, StreamingResponse
12
  from fastrtc import (
13
  AdditionalOutputs,
 
16
  get_tts_model,
17
  get_twilio_turn_credentials,
18
  )
19
+ from fastrtc.utils import audio_to_bytes
20
  from gradio.utils import get_space
21
  from groq import Groq
22
  from pydantic import BaseModel
 
45
  ).text
46
  print("prompt", prompt)
47
  chatbot.append({"role": "user", "content": prompt})
48
+ yield AdditionalOutputs(chatbot)
49
  messages.append({"role": "user", "content": prompt})
50
  response = claude_client.messages.create(
51
  model="claude-3-5-haiku-20241022",
 
58
  if getattr(block, "type", None) == "text"
59
  )
60
  chatbot.append({"role": "assistant", "content": response_text})
 
61
  import time
62
 
63
  start = time.time()
 
67
  print("chunk", i, time.time() - start)
68
  yield chunk
69
  print("finished tts", time.time() - start)
70
+ yield AdditionalOutputs(chatbot)
71
 
72
 
73
  chatbot = gr.Chatbot(type="messages")
 
93
  chatbot: list[Message]
94
 
95
 
96
+ app = FastAPI()
97
+ stream.mount(app)
98
+
99
+
100
+ @app.get("/")
101
  async def _():
102
  rtc_config = get_twilio_turn_credentials() if get_space() else None
103
  html_content = (curr_dir / "index.html").read_text()
 
105
  return HTMLResponse(content=html_content, status_code=200)
106
 
107
 
108
+ @app.post("/input_hook")
109
  async def _(body: InputData):
110
  stream.set_input(body.webrtc_id, body.model_dump()["chatbot"])
111
  return {"status": "ok"}
112
 
113
 
114
+ @app.get("/outputs")
115
  def _(webrtc_id: str):
116
  async def output_stream():
117
  async for output in stream.output_stream(webrtc_id):
118
  chatbot = output.args[0]
119
+ if len(chatbot) > 1:
120
+ yield f"event: output\ndata: {json.dumps(chatbot[-2])}\n\n"
121
+ yield f"event: output\ndata: {json.dumps(chatbot[-1])}\n\n"
122
 
123
  return StreamingResponse(output_stream(), media_type="text/event-stream")
124
 
 
126
  if __name__ == "__main__":
127
  import os
128
 
129
+ if (mode := os.getenv("MODE")) == "UI":
130
+ stream.ui.launch(server_port=7860)
131
+ elif mode == "PHONE":
132
+ stream.fastphone(host="0.0.0.0", port=7860)
133
+ else:
134
  import uvicorn
135
 
136
+ uvicorn.run(app, host="0.0.0.0", port=7860)