Spaces:
Sleeping
Sleeping
@panditu1 commited on
Commit Β·
c3e228c
1
Parent(s): 8312f3f
kdflks
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import gradio as grfrom transformers import pipelinefrom huggingface_hub import InferenceClientimport os# --- Model and Client Setup ---# β 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)# --- 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":# Few-shot prompt to guide GPT-like modelprompt = ("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:")generated = local_translator(prompt,max_new_tokens=60,pad_token_id=50256)[0]["generated_text"]# β Strip the original prompt and return only the continuationoutput = generated[len(prompt):].strip()return output if output else "(No meaningful output from local model)"elif model_choice == "API-Based":prompt = (f"Rephrase the sentence into elegant, formal, and slightly archaic English. "f"Sentence: '{simple_text}'")try:response = api_client.text_generation(prompt=prompt,max_new_tokens=150,do_sample=True,temperature=0.7,return_full_text=False)# β Handle response objectif 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}"else:return "β οΈ Please select a model version."# --- Gradio Interface ---with gr.Blocks(title="Elegant English") as demo:gr.Markdown("# π Elegant English Translator")123456789101112131415161718192021222324252627import gradio as grfrom transformers import pipeline# β Use FLAN-T5 for instruction-following taskslocal_translator = pipeline("text2text-generation", model="google/flan-t5-base")def get_translation(simple_text, model_choice):if not simple_text.strip():return "β οΈ Please enter a sentence."if model_choice == "Locally Hosted":prompt = (f"Rephrase this sentence into elegant, formal, and slightly archaic English:\n\n{simple_text}")result = local_translator(prompt, max_new_tokens=128)return result[0]["generated_text"]elif model_choice == "API-Based":return "β API currently not available β set HF token first."else:return "β οΈ Please select a model version."# --- Gradio Interface ---with gr.Blocks(title="Elegant English") as demo:gr.Markdown("# π Elegant English Translator")
app.py+7-50