File size: 1,747 Bytes
54c7d70
7680888
aa1aadc
 
 
 
 
 
 
 
 
 
7680888
54c7d70
aa1aadc
 
7680888
 
 
 
 
 
aa1aadc
 
 
 
 
 
 
 
 
 
 
7680888
 
 
 
 
 
 
 
 
 
 
 
 
 
2caae6c
 
7680888
 
 
48b44f1
54c7d70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

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()