Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,26 @@
|
|
1 |
import os
|
2 |
import random
|
|
|
3 |
import gradio as gr
|
4 |
from groq import Groq
|
5 |
|
6 |
-
client = Groq(
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
def create_history_messages(history):
|
11 |
history_messages = [{"role": "user", "content": m[0]} for m in history]
|
12 |
history_messages.extend([{"role": "assistant", "content": m[1]} for m in history])
|
13 |
return history_messages
|
14 |
|
|
|
15 |
def generate_response(prompt, history, model, temperature, max_tokens, top_p, seed):
|
16 |
messages = create_history_messages(history)
|
17 |
messages.append({"role": "user", "content": prompt})
|
18 |
print(messages)
|
19 |
|
20 |
if seed == 0:
|
21 |
-
seed = random.randint(1,
|
22 |
-
|
23 |
stream = client.chat.completions.create(
|
24 |
messages=messages,
|
25 |
model=model,
|
@@ -40,18 +41,125 @@ def generate_response(prompt, history, model, temperature, max_tokens, top_p, se
|
|
40 |
|
41 |
return response
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import random
|
3 |
+
|
4 |
import gradio as gr
|
5 |
from groq import Groq
|
6 |
|
7 |
+
client = Groq(api_key=os.environ.get("Groq_Api_Key"))
|
8 |
+
|
9 |
+
|
|
|
10 |
def create_history_messages(history):
|
11 |
history_messages = [{"role": "user", "content": m[0]} for m in history]
|
12 |
history_messages.extend([{"role": "assistant", "content": m[1]} for m in history])
|
13 |
return history_messages
|
14 |
|
15 |
+
|
16 |
def generate_response(prompt, history, model, temperature, max_tokens, top_p, seed):
|
17 |
messages = create_history_messages(history)
|
18 |
messages.append({"role": "user", "content": prompt})
|
19 |
print(messages)
|
20 |
|
21 |
if seed == 0:
|
22 |
+
seed = random.randint(1, 2**32-1)
|
23 |
+
|
24 |
stream = client.chat.completions.create(
|
25 |
messages=messages,
|
26 |
model=model,
|
|
|
41 |
|
42 |
return response
|
43 |
|
44 |
+
|
45 |
+
def transcribe_audio(audio_file, prompt, language):
|
46 |
+
with open(audio_file.name, "rb") as file:
|
47 |
+
transcription = client.audio.transcriptions.create(
|
48 |
+
file=(audio_file.name, file.read()),
|
49 |
+
model="whisper-large-v3",
|
50 |
+
prompt=prompt,
|
51 |
+
response_format="json",
|
52 |
+
language=language,
|
53 |
+
temperature=0.0,
|
54 |
+
)
|
55 |
+
return transcription.text
|
56 |
+
|
57 |
+
|
58 |
+
def translate_audio(audio_file, prompt):
|
59 |
+
with open(audio_file.name, "rb") as file:
|
60 |
+
translation = client.audio.translations.create(
|
61 |
+
file=(audio_file.name, file.read()),
|
62 |
+
model="whisper-large-v3",
|
63 |
+
prompt=prompt,
|
64 |
+
response_format="json",
|
65 |
+
temperature=0.0,
|
66 |
+
)
|
67 |
+
return translation.text
|
68 |
+
|
69 |
+
|
70 |
+
with gr.Blocks() as demo:
|
71 |
+
gr.Markdown(
|
72 |
+
"""
|
73 |
+
# Groq API UI
|
74 |
+
Inference by Groq. Hugging Face Space by [Nick088](https://linktr.ee/Nick088)
|
75 |
+
"""
|
76 |
+
)
|
77 |
+
with gr.Tabs():
|
78 |
+
with gr.TabItem("LLMs"):
|
79 |
+
with gr.Row():
|
80 |
+
with gr.Column():
|
81 |
+
model = gr.Dropdown(
|
82 |
+
choices=[
|
83 |
+
"llama3-70b-8192",
|
84 |
+
"llama3-8b-8192",
|
85 |
+
"mixtral-8x7b-32768",
|
86 |
+
"gemma-7b-it",
|
87 |
+
"gemma2-9b-it",
|
88 |
+
],
|
89 |
+
value="llama3-70b-8192",
|
90 |
+
label="Model",
|
91 |
+
)
|
92 |
+
temperature = gr.Slider(
|
93 |
+
minimum=0.0,
|
94 |
+
maximum=1.0,
|
95 |
+
step=0.01,
|
96 |
+
value=0.5,
|
97 |
+
label="Temperature",
|
98 |
+
info="Controls diversity of the generated text. Lower is more deterministic, higher is more creative.",
|
99 |
+
)
|
100 |
+
max_tokens = gr.Slider(
|
101 |
+
minimum=1,
|
102 |
+
maximum=32192,
|
103 |
+
step=1,
|
104 |
+
value=4096,
|
105 |
+
label="Max Tokens",
|
106 |
+
info="The maximum number of tokens that the model can process in a single response.<br>Maximums: 8k for gemma 7b it, gemma2 9b it, llama 7b & 70b, 32k for mixtral 8x7b.",
|
107 |
+
)
|
108 |
+
top_p = gr.Slider(
|
109 |
+
minimum=0.0,
|
110 |
+
maximum=1.0,
|
111 |
+
step=0.01,
|
112 |
+
value=0.5,
|
113 |
+
label="Top P",
|
114 |
+
info="A method of text generation where a model will only consider the most probable next tokens that make up the probability p.",
|
115 |
+
)
|
116 |
+
seed = gr.Number(
|
117 |
+
precision=0, value=42, label="Seed", info="A starting point to initiate generation, use 0 for random"
|
118 |
+
)
|
119 |
+
with gr.Column():
|
120 |
+
chatbot = gr.ChatInterface(
|
121 |
+
fn=generate_response,
|
122 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
123 |
+
additional_inputs=[model, temperature, max_tokens, top_p, seed],
|
124 |
+
)
|
125 |
+
with gr.TabItem("Whisper"):
|
126 |
+
with gr.Tabs():
|
127 |
+
with gr.TabItem("Transcription"):
|
128 |
+
with gr.Row():
|
129 |
+
audio_input = gr.Audio(
|
130 |
+
source="upload", type="filepath", label="Upload Audio"
|
131 |
+
)
|
132 |
+
transcribe_prompt = gr.Textbox(
|
133 |
+
label="Prompt (Optional)",
|
134 |
+
info="Specify any context or spelling corrections.",
|
135 |
+
)
|
136 |
+
language = gr.Dropdown(
|
137 |
+
choices=["en", "es", "fr", "de", "zh", "ja", "ko"], # Add more language codes as needed
|
138 |
+
value="en",
|
139 |
+
label="Language",
|
140 |
+
)
|
141 |
+
transcribe_button = gr.Button("Transcribe")
|
142 |
+
transcription_output = gr.Textbox(label="Transcription")
|
143 |
+
transcribe_button.click(
|
144 |
+
transcribe_audio,
|
145 |
+
inputs=[audio_input, transcribe_prompt, language],
|
146 |
+
outputs=transcription_output,
|
147 |
+
)
|
148 |
+
with gr.TabItem("Translation"):
|
149 |
+
with gr.Row():
|
150 |
+
audio_input_translate = gr.Audio(
|
151 |
+
source="upload", type="filepath", label="Upload Audio"
|
152 |
+
)
|
153 |
+
translate_prompt = gr.Textbox(
|
154 |
+
label="Prompt (Optional)",
|
155 |
+
info="Specify any context or spelling corrections.",
|
156 |
+
)
|
157 |
+
translate_button = gr.Button("Translate")
|
158 |
+
translation_output = gr.Textbox(label="Translation")
|
159 |
+
translate_button.click(
|
160 |
+
translate_audio,
|
161 |
+
inputs=[audio_input_translate, translate_prompt],
|
162 |
+
outputs=translation_output,
|
163 |
+
)
|
164 |
+
|
165 |
+
demo.launch()
|