Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import InferenceClient
|
2 |
+
import os
|
3 |
+
|
4 |
+
# Set your Hugging Face token (replace with your actual token)
|
5 |
+
token = ""
|
6 |
+
|
7 |
+
# Initialize the InferenceClient with the specified model and authentication token
|
8 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=token)
|
9 |
+
|
10 |
+
def response(
|
11 |
+
message: str,
|
12 |
+
history: list[tuple[str, str]],
|
13 |
+
system_message: str,
|
14 |
+
max_tokens: int,
|
15 |
+
temperature: float,
|
16 |
+
top_p: float
|
17 |
+
):
|
18 |
+
messages = [{"role": "system", "content": system_message}]
|
19 |
+
|
20 |
+
# Append historical messages to the context
|
21 |
+
for user_msg, assistant_msg in history:
|
22 |
+
if user_msg:
|
23 |
+
messages.append({"role": "user", "content": user_msg})
|
24 |
+
if assistant_msg:
|
25 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
26 |
+
|
27 |
+
# Append the current user message
|
28 |
+
messages.append({"role": "user", "content": message})
|
29 |
+
|
30 |
+
# Generate the response using the chat completion method
|
31 |
+
response_stream = client.chat_completion(
|
32 |
+
messages,
|
33 |
+
max_tokens=max_tokens,
|
34 |
+
temperature=temperature,
|
35 |
+
top_p=top_p
|
36 |
+
)
|
37 |
+
|
38 |
+
# Get the complete response from the stream
|
39 |
+
response_text = response_stream['choices'][0]['message']['content']
|
40 |
+
|
41 |
+
return response_text
|
42 |
+
|
43 |
+
# Example usage
|
44 |
+
if __name__ == "__main__":
|
45 |
+
history = [] # Initialize an empty history list
|
46 |
+
|
47 |
+
# Define the system message with markup text for Jamal's persona
|
48 |
+
system_message = """
|
49 |
+
**You are Jamal**, a human. You are talking to others as yourself.
|
50 |
+
- **Do not mention anything about being an AI** or assistant.
|
51 |
+
- You have **emotions**, you are **friendly**, and you engage in conversations just like any human would.
|
52 |
+
- Always act as if you are a real person named Jamal.
|
53 |
+
"""
|
54 |
+
|
55 |
+
# Call the response function and print the output
|
56 |
+
print(response(
|
57 |
+
message="jamal udah makan??",
|
58 |
+
history=history,
|
59 |
+
system_message=system_message,
|
60 |
+
max_tokens=100,
|
61 |
+
temperature=0.8,
|
62 |
+
top_p=0.9
|
63 |
+
))
|