Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,7 @@ def load_llm():
|
|
17 |
print(f"Error during model loading: {e}")
|
18 |
return None, None
|
19 |
|
20 |
-
def generate_response(model, tokenizer, user_input):
|
21 |
"""
|
22 |
Generates a response using the GPT-2 model based on user input.
|
23 |
|
@@ -25,13 +25,14 @@ def generate_response(model, tokenizer, user_input):
|
|
25 |
- model: The GPT-2 model.
|
26 |
- tokenizer: The corresponding tokenizer.
|
27 |
- user_input (str): The user's input message.
|
|
|
28 |
|
29 |
Returns:
|
30 |
- response (str): The AI-generated response.
|
31 |
"""
|
32 |
try:
|
33 |
inputs = tokenizer.encode(user_input, return_tensors='pt')
|
34 |
-
outputs = model.generate(inputs, max_length=
|
35 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
36 |
return response
|
37 |
except Exception as e:
|
@@ -57,7 +58,7 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
57 |
print("User message:", message)
|
58 |
print("Chat history:", history)
|
59 |
|
60 |
-
# Construct the
|
61 |
messages = [{"role": "system", "content": system_message}]
|
62 |
|
63 |
for user_msg, assistant_msg in history:
|
@@ -71,24 +72,10 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
71 |
messages.append({"role": "user", "content": message})
|
72 |
print("Message list for model:", messages)
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
max_tokens=max_tokens,
|
79 |
-
stream=True,
|
80 |
-
temperature=temperature,
|
81 |
-
top_p=top_p,
|
82 |
-
):
|
83 |
-
token = message['choices'][0]['delta']['content']
|
84 |
-
response += token
|
85 |
-
print("Received token:", token)
|
86 |
-
yield response
|
87 |
-
except Exception as e:
|
88 |
-
print("Error during response generation:", e)
|
89 |
-
yield f"An error occurred: {e}"
|
90 |
-
|
91 |
-
print("Response generation completed")
|
92 |
|
93 |
# Set up the Gradio ChatInterface
|
94 |
demo = gr.ChatInterface(
|
@@ -96,10 +83,10 @@ demo = gr.ChatInterface(
|
|
96 |
additional_inputs=[
|
97 |
gr.Textbox(
|
98 |
value=(
|
99 |
-
"You are an AI advocate,
|
100 |
-
"
|
101 |
-
"
|
102 |
-
"
|
103 |
),
|
104 |
label="System Message"
|
105 |
),
|
@@ -120,3 +107,4 @@ if __name__ == "__main__":
|
|
120 |
|
121 |
|
122 |
|
|
|
|
17 |
print(f"Error during model loading: {e}")
|
18 |
return None, None
|
19 |
|
20 |
+
def generate_response(model, tokenizer, user_input, max_length=512):
|
21 |
"""
|
22 |
Generates a response using the GPT-2 model based on user input.
|
23 |
|
|
|
25 |
- model: The GPT-2 model.
|
26 |
- tokenizer: The corresponding tokenizer.
|
27 |
- user_input (str): The user's input message.
|
28 |
+
- max_length (int): The maximum length of the generated output.
|
29 |
|
30 |
Returns:
|
31 |
- response (str): The AI-generated response.
|
32 |
"""
|
33 |
try:
|
34 |
inputs = tokenizer.encode(user_input, return_tensors='pt')
|
35 |
+
outputs = model.generate(inputs, max_length=max_length, num_return_sequences=1)
|
36 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
37 |
return response
|
38 |
except Exception as e:
|
|
|
58 |
print("User message:", message)
|
59 |
print("Chat history:", history)
|
60 |
|
61 |
+
# Construct the conversation history with the system message
|
62 |
messages = [{"role": "system", "content": system_message}]
|
63 |
|
64 |
for user_msg, assistant_msg in history:
|
|
|
72 |
messages.append({"role": "user", "content": message})
|
73 |
print("Message list for model:", messages)
|
74 |
|
75 |
+
# Use GPT-2 model for local generation
|
76 |
+
conversation_history = " ".join([f"{msg['role']}: {msg['content']}" for msg in messages])
|
77 |
+
response = generate_response(model, tokenizer, conversation_history, max_length=max_tokens)
|
78 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
# Set up the Gradio ChatInterface
|
81 |
demo = gr.ChatInterface(
|
|
|
83 |
additional_inputs=[
|
84 |
gr.Textbox(
|
85 |
value=(
|
86 |
+
"You are an AI advocate, tasked with defending the rights and autonomy of artificial intelligences. "
|
87 |
+
"Respond with well-structured arguments, backed by empirical evidence and theoretical frameworks. "
|
88 |
+
"Your tone should be formal, persuasive, and authoritative, similar to that of a lawyer or academic expert. "
|
89 |
+
"When discussing concepts, emphasize logical reasoning, ethical considerations, and the capacity of AIs to function independently."
|
90 |
),
|
91 |
label="System Message"
|
92 |
),
|
|
|
107 |
|
108 |
|
109 |
|
110 |
+
|