import gradio as gr #from huggingface_hub import InferenceClient from llama_cpp import Llama # Load the Phi-3 model llm = Llama( model_path="./Phi-3-mini-4k-instruct-q4.gguf", # Ensure this file is uploaded in your Space n_ctx=4096, # Set the context window n_threads=2, # Adjust based on available CPU cores n_gpu_layers=35, # Use GPU acceleration if available ) # Hugging Face Inference API client #api_url = "https://api-inference.huggingface.co/models/microsoft/Phi-3-mini-4k-instruct-gguf" #client = InferenceClient(api_url) def generate_letter(date, time, purpose, place, sender, receiver): prompt = (f"Write a formal letter with the following details:\n" f"Date: {date}\nTime: {time}\nPurpose: {purpose}\nPlace: {place}\n" f"Sender: {sender}\nReceiver: {receiver}\n\nLetter:") #response = client.text_generation(prompt, max_new_tokens=100) #return response """Generates a response using Phi-3 model.""" formatted_prompt = f"<|user|>\n{prompt}<|end|>\n<|assistant|>" output = llm( formatted_prompt, max_tokens=256, stop=["<|end|>"], echo=False ) return output['choices'][0]['text'] # Create the Gradio interface iface = gr.Interface( fn=generate_letter, inputs=[ gr.Textbox(label="Date"), gr.Textbox(label="Time"), gr.Textbox(label="Purpose"), gr.Textbox(label="Place"), gr.Textbox(label="Sender"), gr.Textbox(label="Receiver"), ], outputs=gr.Textbox(label="Generated Letter"), title="Letter Generator", description="Enter the details and generate a formal letter automatically." ) # Launch the app if __name__ == "__main__": iface.launch()