Spaces:
Sleeping
Sleeping
@panditu1 commited on
Commit Β·
8a2bbb5
1
Parent(s): 18d644f
kdjfskdfdfj
Browse files4 unmodified lines56789101112131415161718192021222 unmodified lines25262728293031323334353637383940414243444546471 unmodified line4950515253545556575859604 unmodified lines65666768697071727374757677781 unmodified line808182838485862 unmodified lines899091929394954 unmodified lines1001011021034 unmodified lines# --- Model Setup ---# β Local Instruction-Tuned Model (CPU-friendly, small but good)local_translator = pipeline("text2text-generation", model="google/flan-t5-base")# β API-based Model (Zephyr 7B - High Quality)api_token = os.environ.get("HF") # set this before running: export HF=your_hf_tokenapi_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=api_token)# --- Single Prediction Function ---def get_translation(simple_text, model_choice, temperature):if not simple_text.strip():return "β οΈ Please enter a sentence."prompt = (f"Rephrase the following sentence into elegant, formal, and slightly archaic English, "f"as if spoken by a monarch. Do not use modern slang or emojis.\n\n"2 unmodified linesif model_choice == "Locally Hosted":try:# Flan-T5 does not use temperature directly, but we can vary outputs with num_beamsresult = local_translator(prompt,max_new_tokens=100,num_beams=1 if temperature > 0.6 else 4, # more beams = more deterministicdo_sample=True if temperature > 0.3 else False,top_p=1.0,temperature=temperature)[0]["generated_text"]return result.strip()except Exception as e:return f"β Local model error: {e}"elif model_choice == "API-Based":if not api_token:return "β No API token set. Please run:\n\nexport HF=your_hf_token"try:response = api_client.text_generation(prompt=prompt,max_new_tokens=150,1 unmodified linetemperature=temperature,return_full_text=False)if isinstance(response, list):return response[0].generated_text.strip()elif hasattr(response, "generated_text"):return response.generated_text.strip()else:return str(response)except Exception as e:return f"β API Error: {e}"4 unmodified lineswith gr.Blocks(title="Elegant English") as demo:gr.Markdown("# π Elegant English Translator")gr.Markdown("Convert casual sentences into elegant, royal-style English.")with gr.Row():model_dropdown = gr.Dropdown(["Locally Hosted", "API-Based"],label="Select Model",value="Locally Hosted")with gr.Row():input_textbox = gr.Textbox(lines=2,1 unmodified linelabel="Simple English")output_textbox = gr.Textbox(label="Elegant English Translation")with gr.Row():temperature_slider = gr.Slider(minimum=0.0,2 unmodified linesstep=0.1,label="Creativity / Temperature")with gr.Row():submit_btn = gr.Button("Translate", variant="primary")4 unmodified lines)if __name__ == "__main__":demo.launch()4 unmodified lines5678910111213141516171819202122232425262 unmodified lines29303132333435363738394041424344454647484950511 unmodified line53545556575859604 unmodified lines656667686970717273747576771 unmodified line7980818283842 unmodified lines8788899091924 unmodified lines9798991004 unmodified lines# --- Model Setup ---# Locally Hosted Model (Flan-T5 is a great choice for CPU, and it's instruction-tuned)local_translator = pipeline("text2text-generation", model="google/flan-t5-base")# API-based Model (Zephyr 7B - High Quality, runs on remote server)# Use the environment variable from your Hugging Face Space secretapi_token = os.environ.get("HF")api_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=api_token)# --- Single Prediction Function ---def get_translation(simple_text, model_choice, temperature):# Input validationif not simple_text or not simple_text.strip():return "β οΈ Please enter a sentence."# A simple but effective prompt for both models.# The more powerful API model will interpret it better.prompt = (f"Rephrase the following sentence into elegant, formal, and slightly archaic English, "f"as if spoken by a monarch. Do not use modern slang or emojis.\n\n"2 unmodified linesif model_choice == "Locally Hosted":try:# For a Seq2Seq model like Flan-T5, do_sample and temperature are standard generation parameters.result = local_translator(prompt,max_new_tokens=100,do_sample=True, # enable sampling for creativitytemperature=temperature # pass the slider value directly)[0]["generated_text"]return result.strip()except Exception as e:return f"β Local model error: {e}"elif model_choice == "API-Based":# Check if the token is available for the API clientif not api_token:return "β API token not found. Please set the 'HF' secret in your Space settings."try:# Use the API client to generate textresponse = api_client.text_generation(prompt=prompt,max_new_tokens=150,1 unmodified linetemperature=temperature,return_full_text=False)# The InferenceClient returns a string directly if return_full_text is Falsereturn response.strip()except Exception as e:return f"β API Error: {e}"4 unmodified lineswith gr.Blocks(title="Elegant English") as demo:gr.Markdown("# π Elegant English Translator")gr.Markdown("Convert casual sentences into elegant, royal-style English.")with gr.Row():model_dropdown = gr.Dropdown(["Locally Hosted", "API-Based"],label="Select Model",value="Locally Hosted")with gr.Row():input_textbox = gr.Textbox(lines=2,1 unmodified linelabel="Simple English")output_textbox = gr.Textbox(label="Elegant English Translation")with gr.Row():temperature_slider = gr.Slider(minimum=0.0,2 unmodified linesstep=0.1,label="Creativity / Temperature")with gr.Row():submit_btn = gr.Button("Translate", variant="primary")4 unmodified lines)if __name__ == "__main__":demo.launch()
app.py+18-21