Spaces:
Sleeping
Sleeping
@panditu1 commited on
Commit ·
8921c91
1
Parent(s): b95fff6
local model vs api model
Browse files1234567891011121314151617181920212223242526import gradio as grfrom transformers import pipeline# Load the model directly from the Hugging Face Hubtranslator = pipeline("text2text-generation", model="t5-small")def translate_to_regal_english(simple_text):"""Translates a simple English sentence into a more regal style."""prompt = f"Translate the following simple sentence into elegant, formal, and slightly archaic English: '{simple_text}'"regal_text = translator(prompt, max_length=100, num_beams=4)[0]['generated_text']return regal_text# Create the Gradio interfaceiface = gr.Interface(fn=translate_to_regal_english,inputs=gr.Textbox(lines=2, placeholder="Enter a simple English sentence...", label="Simple English"),outputs=gr.Textbox(label="Regal English Translation"),title="Enhanced and Fine Tuned English",description="A sophisticated tool to translate everyday phrases into elegant, royal rhetoric.")# Launch the Gradio interfaceif __name__ == "__main__":iface.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import gradio as grfrom transformers import pipelinefrom huggingface_hub import InferenceClientimport os# --- 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")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 modelprompt = 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():model_dropdown = gr.Dropdown(["Locally Hosted", "API-Based"],label="Select Model",value="Locally Hosted")with 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")submit_btn.click(fn=get_translation,inputs=[input_textbox, model_dropdown],outputs=output_textbox)if __name__ == "__main__":demo.launch()
app.py+55-22