Update app.py
Browse files
app.py
CHANGED
@@ -4,17 +4,17 @@ import os
|
|
4 |
|
5 |
token = os.getenv("HF_TOKEN")
|
6 |
|
7 |
-
#
|
8 |
def generate_text(prompt, model_choice, max_tokens, *other_params):
|
9 |
-
#
|
10 |
model_urls = {
|
11 |
"GPT-3.5": "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1",
|
12 |
"GPT-4": "https://api-inference.huggingface.co/models/ai-forever/ruGPT-4"
|
13 |
}
|
14 |
-
#
|
15 |
api_url = model_urls[model_choice]
|
16 |
|
17 |
-
#
|
18 |
headers = {
|
19 |
"Authorization": f"Bearer {token}"
|
20 |
}
|
@@ -24,39 +24,39 @@ def generate_text(prompt, model_choice, max_tokens, *other_params):
|
|
24 |
"options": {"use_cache": False}
|
25 |
}
|
26 |
|
27 |
-
#
|
28 |
response = requests.post(api_url, headers=headers, json=payload)
|
29 |
if response.status_code == 200:
|
30 |
-
#
|
31 |
return response.json()[0]['generated_text']
|
32 |
else:
|
33 |
-
#
|
34 |
return "Error: " + response.text
|
35 |
|
36 |
-
#
|
37 |
with gr.Blocks() as demo:
|
38 |
-
with gr.Tab("
|
39 |
with gr.Row():
|
40 |
-
prompt = gr.Textbox(label="Prompt", lines=3, placeholder="
|
41 |
-
model_choice = gr.Radio(["Mixtral-8x7B", "GPT-4"], label="
|
42 |
|
43 |
-
with gr.Tab("
|
44 |
with gr.Row():
|
45 |
-
max_tokens = gr.Slider(100, 5000, step=1, label="
|
46 |
-
#
|
47 |
|
48 |
with gr.Row():
|
49 |
-
generate_btn = gr.Button("
|
50 |
|
51 |
with gr.Row():
|
52 |
-
output_text = gr.Textbox(label="
|
53 |
|
54 |
-
#
|
55 |
generate_btn.click(
|
56 |
fn=generate_text,
|
57 |
inputs=[prompt, model_choice, max_tokens],
|
58 |
outputs=output_text
|
59 |
)
|
60 |
|
61 |
-
#
|
62 |
demo.launch()
|
|
|
4 |
|
5 |
token = os.getenv("HF_TOKEN")
|
6 |
|
7 |
+
# Function to generate text
|
8 |
def generate_text(prompt, model_choice, max_tokens, *other_params):
|
9 |
+
# Model name matching с URL
|
10 |
model_urls = {
|
11 |
"GPT-3.5": "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1",
|
12 |
"GPT-4": "https://api-inference.huggingface.co/models/ai-forever/ruGPT-4"
|
13 |
}
|
14 |
+
# Selecting a URL depending on the selected model
|
15 |
api_url = model_urls[model_choice]
|
16 |
|
17 |
+
# Preparing data for a request
|
18 |
headers = {
|
19 |
"Authorization": f"Bearer {token}"
|
20 |
}
|
|
|
24 |
"options": {"use_cache": False}
|
25 |
}
|
26 |
|
27 |
+
# Sending a request to API
|
28 |
response = requests.post(api_url, headers=headers, json=payload)
|
29 |
if response.status_code == 200:
|
30 |
+
# Returning the generated text
|
31 |
return response.json()[0]['generated_text']
|
32 |
else:
|
33 |
+
# Returning an error message
|
34 |
return "Error: " + response.text
|
35 |
|
36 |
+
# Creating an interface using G Blocks
|
37 |
with gr.Blocks() as demo:
|
38 |
+
with gr.Tab("Basic settings"):
|
39 |
with gr.Row():
|
40 |
+
prompt = gr.Textbox(label="Prompt", lines=3, placeholder="Enter text...")
|
41 |
+
model_choice = gr.Radio(["Mixtral-8x7B", "GPT-4"], label="Model selection", value="GPT-3.5")
|
42 |
|
43 |
+
with gr.Tab("Advanced settings"):
|
44 |
with gr.Row():
|
45 |
+
max_tokens = gr.Slider(100, 5000, step=1, label="Maximum tokens")
|
46 |
+
# Here you can add other parameters for the text generation API
|
47 |
|
48 |
with gr.Row():
|
49 |
+
generate_btn = gr.Button("Generation")
|
50 |
|
51 |
with gr.Row():
|
52 |
+
output_text = gr.Textbox(label="Answer", placeholder="The generated text will be here...")
|
53 |
|
54 |
+
# Setting up a callback function for the button
|
55 |
generate_btn.click(
|
56 |
fn=generate_text,
|
57 |
inputs=[prompt, model_choice, max_tokens],
|
58 |
outputs=output_text
|
59 |
)
|
60 |
|
61 |
+
# Launching the interface
|
62 |
demo.launch()
|