Spaces:
Runtime error
Runtime error
Adjusted app.py file
Browse files
app.py
CHANGED
@@ -3,44 +3,39 @@ import json
|
|
3 |
import gradio as gr
|
4 |
import google.generativeai as genai
|
5 |
|
6 |
-
GOOGLE_API_KEY = os.environ.get(
|
7 |
genai.configure(api_key=GOOGLE_API_KEY)
|
8 |
|
9 |
# Set up the model
|
10 |
generation_config = {
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
}
|
16 |
|
17 |
safety_settings = [
|
18 |
-
|
19 |
-
"category": "
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
29 |
-
},
|
30 |
-
{
|
31 |
-
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
32 |
-
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
33 |
-
}
|
34 |
]
|
35 |
|
36 |
model = genai.GenerativeModel(
|
37 |
model_name="gemini-pro",
|
38 |
generation_config=generation_config,
|
39 |
-
safety_settings=safety_settings
|
40 |
)
|
41 |
|
42 |
task_description = " You are an SMS (Short Message Service) reader who reads every message that the short message service centre receives and you need to classify each message among the following categories: {}<div>Let the output be a softmax function output giving the probability of message belonging to each category.</div><div>The sum of the probabilities should be 1</div><div>The output must be in JSON format</div>"
|
43 |
|
|
|
44 |
def classify_msg(categories, message):
|
45 |
prompt_parts = [
|
46 |
task_description.format(categories),
|
@@ -51,18 +46,20 @@ def classify_msg(categories, message):
|
|
51 |
response = model.generate_content(prompt_parts)
|
52 |
|
53 |
json_response = json.loads(
|
54 |
-
response.text[response.text.find(
|
55 |
)
|
56 |
|
57 |
return gr.Label(json_response)
|
58 |
|
|
|
59 |
def clear_inputs_and_outputs():
|
60 |
return [None, None, None]
|
61 |
|
|
|
62 |
with gr.Blocks() as demo:
|
63 |
gr.Markdown(
|
64 |
"""
|
65 |
-
|
66 |
This space demos SMS and text in general classification using Gemini Pro<br> \
|
67 |
For the categories, enter a list of words separated by commas<br><br>
|
68 |
"""
|
@@ -70,7 +67,10 @@ For the categories, enter a list of words separated by commas<br><br>
|
|
70 |
with gr.Row():
|
71 |
with gr.Column():
|
72 |
with gr.Row():
|
73 |
-
categories = gr.Textbox(
|
|
|
|
|
|
|
74 |
with gr.Row():
|
75 |
message = gr.Textbox(label="Message", placeholder="Enter Message")
|
76 |
with gr.Row():
|
@@ -92,10 +92,10 @@ For the categories, enter a list of words separated by commas<br><br>
|
|
92 |
|
93 |
gr.Examples(
|
94 |
examples=[
|
95 |
-
[
|
96 |
-
[
|
97 |
-
[
|
98 |
-
[
|
99 |
],
|
100 |
inputs=[categories, message],
|
101 |
outputs=lbl_output,
|
@@ -103,4 +103,4 @@ For the categories, enter a list of words separated by commas<br><br>
|
|
103 |
cache_examples=True,
|
104 |
)
|
105 |
|
106 |
-
demo.launch()
|
|
|
3 |
import gradio as gr
|
4 |
import google.generativeai as genai
|
5 |
|
6 |
+
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
7 |
genai.configure(api_key=GOOGLE_API_KEY)
|
8 |
|
9 |
# Set up the model
|
10 |
generation_config = {
|
11 |
+
"temperature": 0.9,
|
12 |
+
"top_p": 1,
|
13 |
+
"top_k": 1,
|
14 |
+
"max_output_tokens": 2048,
|
15 |
}
|
16 |
|
17 |
safety_settings = [
|
18 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
19 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
20 |
+
{
|
21 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
22 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE",
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
26 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE",
|
27 |
+
},
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
]
|
29 |
|
30 |
model = genai.GenerativeModel(
|
31 |
model_name="gemini-pro",
|
32 |
generation_config=generation_config,
|
33 |
+
safety_settings=safety_settings,
|
34 |
)
|
35 |
|
36 |
task_description = " You are an SMS (Short Message Service) reader who reads every message that the short message service centre receives and you need to classify each message among the following categories: {}<div>Let the output be a softmax function output giving the probability of message belonging to each category.</div><div>The sum of the probabilities should be 1</div><div>The output must be in JSON format</div>"
|
37 |
|
38 |
+
|
39 |
def classify_msg(categories, message):
|
40 |
prompt_parts = [
|
41 |
task_description.format(categories),
|
|
|
46 |
response = model.generate_content(prompt_parts)
|
47 |
|
48 |
json_response = json.loads(
|
49 |
+
response.text[response.text.find("{") : response.text.rfind("}") + 1]
|
50 |
)
|
51 |
|
52 |
return gr.Label(json_response)
|
53 |
|
54 |
+
|
55 |
def clear_inputs_and_outputs():
|
56 |
return [None, None, None]
|
57 |
|
58 |
+
|
59 |
with gr.Blocks() as demo:
|
60 |
gr.Markdown(
|
61 |
"""
|
62 |
+
# Multi-language Text Classifier using Gemini Pro \
|
63 |
This space demos SMS and text in general classification using Gemini Pro<br> \
|
64 |
For the categories, enter a list of words separated by commas<br><br>
|
65 |
"""
|
|
|
67 |
with gr.Row():
|
68 |
with gr.Column():
|
69 |
with gr.Row():
|
70 |
+
categories = gr.Textbox(
|
71 |
+
label="Categories",
|
72 |
+
placeholder="Input the list of categories as comma separated words",
|
73 |
+
)
|
74 |
with gr.Row():
|
75 |
message = gr.Textbox(label="Message", placeholder="Enter Message")
|
76 |
with gr.Row():
|
|
|
92 |
|
93 |
gr.Examples(
|
94 |
examples=[
|
95 |
+
["Normal, Promotional, Urgent", "Will you be passing by?"],
|
96 |
+
["Spam, Ham", "Plus de 300 % de perte de poids pendant le régime."],
|
97 |
+
["Χαρούμενος, Δυστυχισμένος", "Η εξυπηρέτηση σας ήταν απαίσια"],
|
98 |
+
["مهم، أقل أهمية ", "خبر عاجل"],
|
99 |
],
|
100 |
inputs=[categories, message],
|
101 |
outputs=lbl_output,
|
|
|
103 |
cache_examples=True,
|
104 |
)
|
105 |
|
106 |
+
demo.launch(debug=True, share=True)
|