Spaces:
Runtime error
Runtime error
Adding application and requirements files
Browse files- app.py +106 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
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 |
+
{
|
19 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
20 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
21 |
+
},
|
22 |
+
{
|
23 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
24 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
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),
|
47 |
+
f"Message: {message}",
|
48 |
+
"Category: ",
|
49 |
+
]
|
50 |
+
|
51 |
+
response = model.generate_content(prompt_parts)
|
52 |
+
|
53 |
+
json_response = json.loads(
|
54 |
+
response.text[response.text.find('{'):response.text.rfind('}') + 1]
|
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 |
+
<center><h1>Multi-language Text Classifier using Gemini Pro</h1></center> \
|
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 |
+
"""
|
69 |
+
)
|
70 |
+
with gr.Row():
|
71 |
+
with gr.Column():
|
72 |
+
with gr.Row():
|
73 |
+
categories = gr.Textbox(label="Categories", placeholder="Input the list of categories as comma separated words")
|
74 |
+
with gr.Row():
|
75 |
+
message = gr.Textbox(label="Message", placeholder="Enter Message")
|
76 |
+
with gr.Row():
|
77 |
+
clr_btn = gr.Button(value="Clear", variant="secondary")
|
78 |
+
csf_btn = gr.Button(value="Classify")
|
79 |
+
with gr.Column():
|
80 |
+
lbl_output = gr.Label(label="Prediction")
|
81 |
+
|
82 |
+
clr_btn.click(
|
83 |
+
fn=clear_inputs_and_outputs,
|
84 |
+
inputs=[],
|
85 |
+
outputs=[categories, message, lbl_output],
|
86 |
+
)
|
87 |
+
csf_btn.click(
|
88 |
+
fn=classify_msg,
|
89 |
+
inputs=[categories, message],
|
90 |
+
outputs=[lbl_output],
|
91 |
+
)
|
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,
|
102 |
+
fn=classify_msg,
|
103 |
+
cache_examples=True,
|
104 |
+
)
|
105 |
+
|
106 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
google-generativeai
|