Spaces:
Sleeping
Sleeping
@panditu1 commited on
Commit ·
806ab88
1
Parent(s): 667230c
kdsflk
Browse files12345678910111213141516171819202122232425262728293031323334353637383940415 unmodified lines47484950515253import gradio as grfrom transformers import pipelinefrom huggingface_hub import InferenceClientimport os # <-- Import the os library# --- Model and Client Setup ---# Locally hosted model setuplocal_translator = pipeline("text2text-generation", model="t5-small")# API-based client setup (using a token from a secret)api_token = os.environ.get("HF_TOKEN") # <-- Get the token from the environmentif not api_token:# Handle the case where the token is not set (e.g., local testing)api_token = "your_placeholder_token_for_local_testing"api_client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=api_token)# --- The Single Prediction Function ---def get_translation(simple_text, model_choice):if model_choice == "Locally Hosted":# ... (your existing local model logic) ...prompt = f"Translate '{simple_text}' into elegant English."regal_text = local_translator(prompt, max_length=100, num_beams=4)[0]['generated_text']return regal_textelif model_choice == "API-Based":# Logic for the API-based modelprompt = (f"Rephrase the sentence into elegant, formal, and archaic English. Sentence: '{simple_text}'")response = api_client.text_generation(prompt=prompt, max_new_tokens=150)return response.strip()else:return "Please select a model version."# --- Gradio Interface with gr.Blocks() ---with gr.Blocks(title="Regal Rhetoric") as demo:gr.Markdown("# Regal Rhetoric: Choose Your Translator")gr.Markdown("Select a model to translate simple English into an elegant, royal style.")with gr.Row():5 unmodified lineswith gr.Row():input_textbox = gr.Textbox(lines=2, placeholder="Enter a simple sentence...", label="Simple English")output_textbox = gr.Textbox(label="Regal English Translation")with gr.Row():submit_btn = gr.Button("Translate")1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465665 unmodified lines72737475767778import gradio as grfrom transformers import pipelinefrom huggingface_hub import InferenceClientimport 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)api_token = os.environ.get("HF")api_client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=api_token)# --- The Single Prediction Function ---def get_translation(simple_text, model_choice):if model_choice == "Locally Hosted":# Logic for the locally hosted model# Using a few-shot prompt to guide the text generationprompt = (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:")# 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 + completionoutput = generated_text.split("Elegant:")[-1].strip()return outputelif model_choice == "API-Based":# Logic for the API-based modelprompt = (f"Rephrase the sentence into elegant, formal, and archaic English. 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)return response.strip()except Exception as e:return f"An error occurred with the API call: {e}"else:return "Please select a model version."# --- Gradio Interface with gr.Blocks() ---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.")with gr.Row():5 unmodified lineswith gr.Row():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")
app.py+40-15