Spaces:
Sleeping
Sleeping
@panditu1 commited on
Commit Β·
8a2bbb5
1
Parent(s): 18d644f
kdjfskdfdfj
Browse files4 unmodified lines56788910111211121314151617181719202122232425262 unmodified lines29303128323334353233343536373839404142434445434647484950511 unmodified line53545552535455565756575859604 unmodified lines6566676868697071727374757576771 unmodified line798081838283842 unmodified lines878889929091924 unmodified lines9798991031004 unmodified lines
# --- Model Setup ---
# β
Local Instruction-Tuned Model (CPU-friendly, small but good)# 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)api_token = os.environ.get("HF") # set this before running: export HF=your_hf_token# 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 validation if not simple_text.strip(): if 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 lines
if model_choice == "Locally Hosted": try: # Flan-T5 does not use temperature directly, but we can vary outputs with num_beams # For a Seq2Seq model like Flan-T5, do_sample and temperature are standard generation parameters. result = local_translator( prompt, max_new_tokens=100, num_beams=1 if temperature > 0.6 else 4, # more beams = more deterministic do_sample=True if temperature > 0.3 else False, top_p=1.0, temperature=temperature do_sample=True, # enable sampling for creativity temperature=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 client if not api_token: return "β No API token set. Please run:\n\nexport HF=your_hf_token" return "β API token not found. Please set the 'HF' secret in your Space settings." try: # Use the API client to generate text response = api_client.text_generation( prompt=prompt, max_new_tokens=150,1 unmodified line temperature=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) # The InferenceClient returns a string directly if return_full_text is False return 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 line label="Simple English" ) output_textbox = gr.Textbox(label="Elegant English Translation")
with gr.Row(): temperature_slider = gr.Slider( minimum=0.0,2 unmodified lines step=0.1, label="Creativity / Temperature" )
with gr.Row(): submit_btn = gr.Button("Translate", variant="primary")
4 unmodified lines )
if __name__ == "__main__": demo.launch() demo.launch()app.py+18-21