Spaces:
Sleeping
Sleeping
@panditu1 commited on
Commit Β·
8312f3f
1
Parent(s): 806ab88
klflkd
Browse files3 unmodified lines45678971189101112131415161517181920212218192324212223242525262728293027282930313233343136373839323334353637383940414242434444454647483 unmodified lines52535455565758545960616256636465596667616869636470717273743 unmodified lines78798074818283848586878878899091922 unmodified lines95969787983 unmodified linesimport os
# --- Model and Client Setup ---# Locally hosted model setup# Using gpt2, a small, open-source model suitable for local executionlocal_translator = pipeline("text-generation", model="openai-community/gpt2")
# API-based client setup (using a token from a secret)# β
Locally hosted model (better than GPT-2 for instruction following)# If you want something very small & fast, use distilgpt2# If you have GPU, you can switch to a small instruct-tuned model for better resultslocal_translator = pipeline("text-generation", model="distilgpt2")
# β
API client setup (with token)api_token = os.environ.get("HF")api_client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=api_token)
# --- The Single Prediction Function ---# --- Single Prediction Function ---def get_translation(simple_text, model_choice): if not simple_text.strip(): return "β οΈ Please enter a sentence."
if model_choice == "Locally Hosted": # Logic for the locally hosted model # Using a few-shot prompt to guide the text generation # Few-shot prompt to guide GPT-like model prompt = ( f"Rephrase the following sentence into an elegant, formal, and slightly archaic English style, as if spoken by a monarch. Do not use modern slang or emojis.\n\n" f"Simple: Hello Everyone! Lets go for a ride\n" f"Elegant: Greetings, all. I propose we embark upon a most agreeable journey.\n\n" f"Simple: {simple_text}\n" f"Elegant:" "Rephrase the following sentence into an elegant, formal, and slightly archaic English style, " "as if spoken by a monarch. Do not use modern slang or emojis.\n\n" "Simple: Hello Everyone! Lets go for a ride\n" "Elegant: Greetings, all. I propose we embark upon a most agreeable journey.\n\n" f"Simple: {simple_text}\nElegant:" ) # Generate the translated text. The 'text-generation' pipeline will continue the prompt. generated_text = local_translator( prompt, max_length=150, num_return_sequences=1, pad_token_id=50256 # This is needed for GPT2 )[0]['generated_text']
# Clean up the output to only return the "Elegant" part # This is necessary because text-generation pipelines return the full prompt + completion output = generated_text.split("Elegant:")[-1].strip() return output generated = local_translator( prompt, max_new_tokens=60, pad_token_id=50256 )[0]["generated_text"]
# β
Strip the original prompt and return only the continuation output = generated[len(prompt):].strip() return output if output else "(No meaningful output from local model)"
elif model_choice == "API-Based": # Logic for the API-based model prompt = ( f"Rephrase the sentence into elegant, formal, and archaic English. Sentence: '{simple_text}'" f"Rephrase the sentence into elegant, formal, and slightly archaic English. " f"Sentence: '{simple_text}'" ) try: response = api_client.text_generation(3 unmodified lines temperature=0.7, return_full_text=False ) # β
Handle response object if isinstance(response, list): return response[0].generated_text.strip() elif hasattr(response, "generated_text"): return response.strip() return response.generated_text.strip() else: return str(response) except Exception as e: return f"An error occurred with the API call: {e}" return f"β API Error: {e}"
else: return "Please select a model version." return "β οΈ Please select a model version."
# --- Gradio Interface with gr.Blocks() ---# --- Gradio Interface ---with gr.Blocks(title="Elegant English") as demo: gr.Markdown("# Elegant English: Choose Your Translator") gr.Markdown("Select a model to translate simple English into an elegant, royal style.") gr.Markdown("# π Elegant English Translator") gr.Markdown("Turn everyday speech into elegant, royal English.")
with gr.Row(): model_dropdown = gr.Dropdown(3 unmodified lines )
with gr.Row(): input_textbox = gr.Textbox(lines=2, placeholder="Enter a simple sentence...", label="Simple English") input_textbox = gr.Textbox( lines=2, placeholder="Enter a simple sentence...", label="Simple English" ) output_textbox = gr.Textbox(label="Elegant English Translation")
with gr.Row(): submit_btn = gr.Button("Translate") submit_btn = gr.Button("Translate", variant="primary")
submit_btn.click( fn=get_translation,2 unmodified lines )
if __name__ == "__main__": demo.launch() demo.launch()app.py+46-35