File size: 4,266 Bytes
5681122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7feadc
 
 
5681122
 
 
b7feadc
5681122
 
 
 
 
 
 
 
 
b7feadc
5681122
 
 
 
 
 
 
 
b7feadc
5681122
 
 
 
 
 
 
 
 
 
b7feadc
5681122
b7feadc
 
5681122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7feadc
5681122
 
 
b7feadc
5681122
b7feadc
 
 
 
5681122
b7feadc
5681122
 
b7feadc
 
5681122
b7feadc
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from transformers import GPT2LMHeadModel, GPT2Tokenizer

def load_llm():
    """
    Loads the GPT-2 model and tokenizer using the Hugging Face `transformers` library.
    """
    try:
        # Load pre-trained model and tokenizer from Hugging Face
        print("Downloading or loading the GPT-2 model and tokenizer...")
        model_name = 'gpt2'
        model = GPT2LMHeadModel.from_pretrained(model_name)
        tokenizer = GPT2Tokenizer.from_pretrained(model_name)
        print("Model and tokenizer successfully loaded!")
        return model, tokenizer
    
    except Exception as e:
        print(f"An error occurred while loading the model: {e}")
        return None, None

def generate_response(model, tokenizer, user_input):
    """
    Generates a response using the GPT-2 model and tokenizer.
    
    Args:
    - model: The loaded GPT-2 model.
    - tokenizer: The tokenizer corresponding to the GPT-2 model.
    - user_input (str): The input question from the user.

    Returns:
    - response (str): The generated response.
    """
    try:
        inputs = tokenizer.encode(user_input, return_tensors='pt')
        outputs = model.generate(inputs, max_length=512, num_return_sequences=1)
        response = tokenizer.decode(outputs[0], skip_special_tokens=True)
        return response
    
    except Exception as e:
        return f"An error occurred during response generation: {e}"

# Load the model and tokenizer
model, tokenizer = load_llm()

if model is None or tokenizer is None:
    print("Model and/or tokenizer loading failed.")
else:
    print("Model and tokenizer are ready for use.")

import gradio as gr
from huggingface_hub import InferenceClient

# Initialize the Hugging Face API client
# The InferenceClient does not take an api_key argument
client = InferenceClient()

def retrieval_QA_chain(llm, prompt, db):
    # Placeholder function - ensure it integrates as needed
    return RetrievalQA.from_chain_type(
        llm=llm,
        chain_type="stuff",
        retriever=db.as_retriever(search_kwargs={'k': 2}),
        return_source_documents=True,
        chain_type_kwargs={'prompt': prompt}
    )

def respond(message, history, system_message, max_tokens, temperature, top_p):
    """
    Handles interaction with the chatbot by sending the conversation history
    and system message to the Hugging Face Inference API.
    """
    print("Starting respond function")
    print("Received message:", message)
    print("Conversation history:", history)

    messages = [{"role": "system", "content": system_message}]
    
    for user_msg, assistant_msg in history:
        if user_msg:
            print("Adding user message to messages:", user_msg)
            messages.append({"role": "user", "content": user_msg})
        if assistant_msg:
            print("Adding assistant message to messages:", assistant_msg)
            messages.append({"role": "assistant", "content": assistant_msg})
    
    messages.append({"role": "user", "content": message})
    print("Final message list for the model:", messages)

    response = ""
    try:
        for message in client.chat_completion(
            messages,
            max_tokens=max_tokens,
            stream=True,
            temperature=temperature,
            top_p=top_p,
        ):
            token = message['choices'][0]['delta']['content']
            response += token
            print("Token received:", token)
            yield response
    except Exception as e:
        print("An error occurred:", e)
        yield f"An error occurred: {e}"

    print("Response generation completed")

# Set up the Gradio ChatInterface
demo = gr.ChatInterface(
    fn=respond,
    additional_inputs=[
        gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
        gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
        gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
        gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
    ],
    title="Human Rights AI Chatbot",
    description="Ask questions about human rights, and get informed, passionate answers!"
)

# Launch the Gradio app
if __name__ == "__main__":
    demo.launch()