Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import list_models
|
| 3 |
|
| 4 |
-
|
| 5 |
-
def transcribe_audio(audio, api_key):
|
| 6 |
-
if audio is None:
|
| 7 |
-
return ""
|
| 8 |
-
|
| 9 |
-
client = groq.Client(api_key=api_key)
|
| 10 |
-
|
| 11 |
-
# Convert audio to the format expected by the model
|
| 12 |
-
# The model supports mp3, mp4, mpeg, mpga, m4a, wav, and webm file types
|
| 13 |
-
audio_data = audio[1] # Get the numpy array from the tuple
|
| 14 |
-
buffer = io.BytesIO()
|
| 15 |
-
sf.write(buffer, audio_data, audio[0], format='mp3')
|
| 16 |
-
buffer.seek(0)
|
| 17 |
-
|
| 18 |
-
bytes_audio = io.BytesIO()
|
| 19 |
-
np.save(bytes_audio, audio_data)
|
| 20 |
-
bytes_audio.seek(0)
|
| 21 |
-
|
| 22 |
-
try:
|
| 23 |
-
# Use Distil-Whisper English powered by Groq for transcription
|
| 24 |
-
completion = client.audio.transcriptions.create(
|
| 25 |
-
#model="distil-whisper-large-v3-en",
|
| 26 |
-
model="whisper-large-v3-turbo",
|
| 27 |
-
file=("audio.mp3", buffer),
|
| 28 |
-
response_format="text"
|
| 29 |
-
)
|
| 30 |
-
return completion
|
| 31 |
-
except Exception as e:
|
| 32 |
-
return f"エラー: {str(e)}"
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def generate_response(transcription, api_key):
|
| 36 |
-
if not transcription:
|
| 37 |
-
return "トランスクリプトが利用できません。もう一度話してみてください。"
|
| 38 |
-
|
| 39 |
-
#client = groq.Client(api_key=api_key)
|
| 40 |
-
#co = cohere.ClientV2(api_key="RSX1N2Ei09hmdJQGcicCxQvkGsvwWT7gQbuYwPhI")
|
| 41 |
-
|
| 42 |
-
try:
|
| 43 |
-
url = 'http://www.ryhintl.com/crewai/autogen?qry='+transcription
|
| 44 |
-
res = requests.get(url)
|
| 45 |
-
|
| 46 |
-
# Extract content of Professional_Assistant_Agent
|
| 47 |
-
data = res.content.decode("utf-8")
|
| 48 |
-
data = data.replace("null","None")
|
| 49 |
-
datas = eval(data)
|
| 50 |
-
|
| 51 |
-
basic_content = [entry["content"] for entry in datas["chat_history"] if entry["name"] == "Basic_Assistant_Agent"]
|
| 52 |
-
basic_result = ', '.join([str(x) for x in basic_content])
|
| 53 |
-
|
| 54 |
-
professional_content = [entry["content"] for entry in datas["chat_history"] if entry["name"] == "Professional_Assistant_Agent"]
|
| 55 |
-
professional_result = ', '.join([str(x) for x in professional_content])
|
| 56 |
-
#combined_list = basic_content + professional_content
|
| 57 |
-
|
| 58 |
-
final_result = "Basic_Assistant: "+basic_result+"\n\n\nProfessional_Assistant: "+professional_result
|
| 59 |
-
|
| 60 |
-
return final_result
|
| 61 |
-
except Exception as e:
|
| 62 |
-
return f"エラー: {str(e)}"
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def process_audio(audio, api_key, prompt):
|
| 66 |
-
if not api_key:
|
| 67 |
-
return "Please enter your Groq API key.", "API key is required."
|
| 68 |
-
|
| 69 |
-
if not prompt == "":
|
| 70 |
-
transcription = prompt
|
| 71 |
-
response = generate_response(transcription, api_key)
|
| 72 |
-
return transcription, response
|
| 73 |
-
else:
|
| 74 |
-
transcription = transcribe_audio(audio, api_key)
|
| 75 |
-
response = generate_response(transcription, api_key)
|
| 76 |
-
return transcription, response
|
| 77 |
-
|
| 78 |
def hello(profile: gr.OAuthProfile | None) -> str:
|
| 79 |
# ^ expect a gr.OAuthProfile object as input to get the user's profile
|
| 80 |
# if the user is not logged in, profile will be None
|
|
@@ -112,42 +38,4 @@ with gr.Blocks() as demo:
|
|
| 112 |
demo.load(hello, inputs=None, outputs=m1)
|
| 113 |
demo.load(list_private_models, inputs=None, outputs=m2)
|
| 114 |
|
| 115 |
-
gr.Markdown("# 🎙️ VOICE AGENTIC RAG")
|
| 116 |
-
|
| 117 |
-
api_key_input = gr.Textbox(type="password", label="Groq API Keyを入力してください。", value="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", visible=False)
|
| 118 |
-
|
| 119 |
-
with gr.Row():
|
| 120 |
-
audio_input = gr.Audio(label="音声プロンプト", type="numpy")
|
| 121 |
-
|
| 122 |
-
with gr.Row():
|
| 123 |
-
user_input = gr.Textbox(label="プロンプト", type="text")
|
| 124 |
-
|
| 125 |
-
with gr.Row():
|
| 126 |
-
transcription_output = gr.Textbox(label="トランスクリプション")
|
| 127 |
-
response_output = gr.Textbox(label="AIアシスタントの応答")
|
| 128 |
-
|
| 129 |
-
submit_button = gr.Button("プロセス", variant="primary")
|
| 130 |
-
|
| 131 |
-
# Add the Groq badge
|
| 132 |
-
gr.HTML("""
|
| 133 |
-
<div id="groq-badge">
|
| 134 |
-
<div style="color: #f55036; font-weight: bold;">POWERED BY EPRAG</div>
|
| 135 |
-
</div>
|
| 136 |
-
""")
|
| 137 |
-
|
| 138 |
-
submit_button.click(
|
| 139 |
-
process_audio,
|
| 140 |
-
inputs=[audio_input, api_key_input, user_input],
|
| 141 |
-
outputs=[transcription_output, response_output]
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
gr.Markdown("""
|
| 145 |
-
## 使い方:
|
| 146 |
-
1. マイクのアイコンをクリックしてメッセージを入力するかプロンプトのプロンプトを入力してください。
|
| 147 |
-
2. 音声入力する場合、マイクのアイコンをクリックしてメッセージを話してください。 サポートされている音声ファイルを提供することもできます。サポートされているオーディオ・ファイルには、mp3、mp4、mpeg、mpga、m4a、wav、webmなどがあります。
|
| 148 |
-
3. [プロセス] ボタンをクリックしてスピーチを文字に起こし、AGENTIC RAG アシスタントからの応答を生成します。
|
| 149 |
-
4. 文字起こしとAIアシスタントの応答がそれぞれのテキスト・ボックスに表示されます。
|
| 150 |
-
|
| 151 |
-
""")
|
| 152 |
-
|
| 153 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import list_models
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
def hello(profile: gr.OAuthProfile | None) -> str:
|
| 5 |
# ^ expect a gr.OAuthProfile object as input to get the user's profile
|
| 6 |
# if the user is not logged in, profile will be None
|
|
|
|
| 38 |
demo.load(hello, inputs=None, outputs=m1)
|
| 39 |
demo.load(list_private_models, inputs=None, outputs=m2)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
demo.launch()
|