_H='custom' _G='primary' _F='e.g., business, technology, sports, entertainment' _E='Custom Labels (for custom classification)' _D='Classification Type:' _C='sentiment' _B='Spam' _A='Sentiment' import os,gradio as gr from openai import OpenAI API_KEY=os.environ['API_KEY'] client=OpenAI(base_url='https://openrouter.ai/api/v1',api_key=API_KEY) def classify_text(text,classification_type=_C,custom_labels=''): "\n Classify text using OpenRouter's GPT-OSS-20B model\n ";E='content';D='role';B=classification_type;A=text if not A.strip():return'Please enter some text to classify.' if B==_A:C=f"Classify the sentiment of the following text as Positive, Negative, or Neutral. Only respond with one word: Positive, Negative, or Neutral.\n\nText: {A}" elif B==_B:C=f"Classify whether the following text is Spam or Not Spam. Only respond with: Spam or Not Spam.\n\nText: {A}" try:F=client.chat.completions.create(model='openai/gpt-oss-20b',messages=[{D:'system',E:'You are a text classification assistant. Provide concise, accurate classifications.'},{D:'user',E:C}],max_tokens=50,temperature=.1,extra_headers={'Authorization':f"Bearer {API_KEY}",'HTTP-Referer':'https://your-app-url.com','X-Title':''});G=F.choices[0].message.content.strip();return f"Classification Result: {G}" except Exception as H:return f"Error: {str(H)}" def batch_classify(file,classification_type=_C,custom_labels=''): '\n Classify multiple texts from uploaded file\n ' if file is None:return'Please upload a text file.' try: with open(file.name,'r',encoding='utf-8')as C:D=C.readlines() B=[] for(E,A)in enumerate(D[:10],1): A=A.strip() if A:F=classify_text(A,classification_type,custom_labels);B.append(f"{E}. **Text:** {A}\n **Result:** {F}\n") return'\n'.join(B)if B else'No text found in file.' except Exception as G:return f"Error processing file: {str(G)}" with gr.Blocks(title='',theme=gr.themes.Default(primary_hue='sky'))as demo: with gr.Tabs(): with gr.Tab('Single Text'): with gr.Row(): with gr.Column(scale=2):text_input=gr.Textbox(label='',placeholder='Enter text to classify...',lines=4);classification_type=gr.Radio(choices=[_A,_B],value=_A,label=_D);custom_labels=gr.Textbox(label=_E,placeholder=_F,visible=False);classify_btn=gr.Button('Classify Text',variant=_G) with gr.Column(scale=2):single_output=gr.Markdown(value='') def toggle_custom_labels(choice):return gr.update(visible=choice==_H) classification_type.change(toggle_custom_labels,inputs=[classification_type],outputs=[custom_labels]);classify_btn.click(classify_text,inputs=[text_input,classification_type,custom_labels],outputs=[single_output]) with gr.Tab('Batch Classification'): with gr.Row(): with gr.Column(scale=2):gr.Markdown('Upload a text or csv file:');file_input=gr.File(label='Upload File',file_types=['.txt','.csv']);batch_classification_type=gr.Radio(choices=[_A,_B],value=_A,label=_D);batch_custom_labels=gr.Textbox(label=_E,placeholder=_F,visible=False);batch_classify_btn=gr.Button('🔍 Classify Batch',variant=_G) with gr.Column(scale=2):batch_output=gr.Markdown(value='') def toggle_batch_custom_labels(choice):return gr.update(visible=choice==_H) batch_classification_type.change(toggle_batch_custom_labels,inputs=[batch_classification_type],outputs=[batch_custom_labels]);batch_classify_btn.click(batch_classify,inputs=[file_input,batch_classification_type,batch_custom_labels],outputs=[batch_output]) if __name__=='__main__':demo.launch(server_name='0.0.0.0',server_port=7860,share=True,show_error=True)