Spaces:
Sleeping
Sleeping
add whisper api
Browse files
app.py
CHANGED
@@ -5,6 +5,10 @@ from sentence_transformers import SentenceTransformer
|
|
5 |
import faiss
|
6 |
import numpy as np
|
7 |
import gradio as gr
|
|
|
|
|
|
|
|
|
8 |
|
9 |
def load_or_create_model_and_embeddings(model_name, data_file, output_dir):
|
10 |
model_path = os.path.join(output_dir, 'saved_model')
|
@@ -63,6 +67,20 @@ def search_and_format(query):
|
|
63 |
formatted_results += f" 情資: {result['text']}\n"
|
64 |
formatted_results += f" 命名實體: {', '.join(result['entity_groups'])}\n\n"
|
65 |
return formatted_results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
# 示例問題
|
67 |
example_queries = [
|
68 |
"Tell me about recent cyber attacks from Russia",
|
@@ -89,13 +107,14 @@ custom_css = """
|
|
89 |
# 創建Gradio界面
|
90 |
with gr.Blocks(css=custom_css) as iface:
|
91 |
gr.Markdown("# AskCTI")
|
92 |
-
gr.Markdown("
|
93 |
|
94 |
with gr.Row(equal_height=True):
|
95 |
with gr.Column(scale=1, min_width=300):
|
96 |
-
query_input = gr.Textbox(lines=3, label="")
|
97 |
-
|
98 |
-
|
|
|
99 |
|
100 |
gr.Markdown("### 範例查詢")
|
101 |
for i in range(0, len(example_queries), 2):
|
@@ -107,10 +126,11 @@ with gr.Blocks(css=custom_css) as iface:
|
|
107 |
)
|
108 |
|
109 |
with gr.Column(scale=2):
|
110 |
-
output = gr.Textbox(lines=20, label="")
|
|
|
111 |
|
112 |
submit_btn.click(search_and_format, inputs=[query_input], outputs=[output])
|
113 |
-
|
114 |
|
115 |
# 啟動Gradio界面
|
116 |
iface.launch()
|
|
|
5 |
import faiss
|
6 |
import numpy as np
|
7 |
import gradio as gr
|
8 |
+
import openai
|
9 |
+
|
10 |
+
# 設置OpenAI API密鑰
|
11 |
+
openai.api_key = 'sk-zK6OrDxP5DvDdAQqnR_nEuUL3UrZf_4W7qvYj1uphjT3BlbkFJdmZAxlxUCFv92NnnMwSB15FhpmiDZSfG2QPueobSQA'
|
12 |
|
13 |
def load_or_create_model_and_embeddings(model_name, data_file, output_dir):
|
14 |
model_path = os.path.join(output_dir, 'saved_model')
|
|
|
67 |
formatted_results += f" 情資: {result['text']}\n"
|
68 |
formatted_results += f" 命名實體: {', '.join(result['entity_groups'])}\n\n"
|
69 |
return formatted_results
|
70 |
+
|
71 |
+
def transcribe_audio(audio):
|
72 |
+
try:
|
73 |
+
# 將音頻文件上傳到Whisper API
|
74 |
+
with open(audio, "rb") as audio_file:
|
75 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
76 |
+
return transcript.text
|
77 |
+
except Exception as e:
|
78 |
+
return f"轉錄時發生錯誤: {str(e)}"
|
79 |
+
|
80 |
+
def audio_to_search(audio):
|
81 |
+
transcription = transcribe_audio(audio)
|
82 |
+
return search_and_format(transcription), transcription
|
83 |
+
|
84 |
# 示例問題
|
85 |
example_queries = [
|
86 |
"Tell me about recent cyber attacks from Russia",
|
|
|
107 |
# 創建Gradio界面
|
108 |
with gr.Blocks(css=custom_css) as iface:
|
109 |
gr.Markdown("# AskCTI")
|
110 |
+
gr.Markdown("輸入查詢或使用語音輸入以搜索相關威脅情報,將顯示前3個最相關的結果,包括實體組。")
|
111 |
|
112 |
with gr.Row(equal_height=True):
|
113 |
with gr.Column(scale=1, min_width=300):
|
114 |
+
query_input = gr.Textbox(lines=3, label="文字查詢")
|
115 |
+
with gr.Row():
|
116 |
+
submit_btn = gr.Button("查詢")
|
117 |
+
audio_input = gr.Audio(source="microphone", type="filepath", label="語音輸入")
|
118 |
|
119 |
gr.Markdown("### 範例查詢")
|
120 |
for i in range(0, len(example_queries), 2):
|
|
|
126 |
)
|
127 |
|
128 |
with gr.Column(scale=2):
|
129 |
+
output = gr.Textbox(lines=20, label="搜索結果")
|
130 |
+
transcription_output = gr.Textbox(lines=3, label="語音轉錄結果")
|
131 |
|
132 |
submit_btn.click(search_and_format, inputs=[query_input], outputs=[output])
|
133 |
+
audio_input.change(audio_to_search, inputs=[audio_input], outputs=[output, transcription_output])
|
134 |
|
135 |
# 啟動Gradio界面
|
136 |
iface.launch()
|