Abhaykoul commited on
Commit
ced8c11
·
verified ·
1 Parent(s): eca12fa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -0
app.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import subprocess
3
+ from llama_cpp import Llama
4
+ from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
5
+ from llama_cpp_agent.providers import LlamaCppPythonProvider
6
+ from llama_cpp_agent.chat_history import BasicChatHistory
7
+ from llama_cpp_agent.chat_history.messages import Roles
8
+ import gradio as gr
9
+ from huggingface_hub import hf_hub_download
10
+
11
+ # Download models
12
+ hf_hub_download(
13
+ repo_id="CharacterEcho/Rohit-Sharma",
14
+ filename="rohit-sharma-q5_k_s.gguf",
15
+ local_dir="./models"
16
+ )
17
+
18
+ llm = None
19
+ llm_model = None
20
+
21
+ def respond(
22
+ message,
23
+ history: list[tuple[str, str]],
24
+ model,
25
+ system_message,
26
+ max_tokens,
27
+ temperature,
28
+ top_p,
29
+ top_k,
30
+ repeat_penalty,
31
+ ):
32
+ chat_template = MessagesFormatterType.CHATML
33
+
34
+ global llm
35
+ global llm_model
36
+
37
+ if llm is None or llm_model != model:
38
+ llm = Llama(
39
+ model_path=f"models/{model}",
40
+ n_ctx=2048, # Reduced context size for CPU
41
+ n_threads=4, # Adjust this based on your CPU cores
42
+ n_gpu_layers=50
43
+ )
44
+ llm_model = model
45
+
46
+ provider = LlamaCppPythonProvider(llm)
47
+
48
+ agent = LlamaCppAgent(
49
+ provider,
50
+ system_prompt=f"{system_message}",
51
+ predefined_messages_formatter_type=chat_template,
52
+ debug_output=True
53
+ )
54
+
55
+ settings = provider.get_provider_default_settings()
56
+ settings.temperature = temperature
57
+ settings.top_k = top_k
58
+ settings.top_p = top_p
59
+ settings.max_tokens = max_tokens
60
+ settings.repeat_penalty = repeat_penalty
61
+ settings.stream = True
62
+
63
+ messages = BasicChatHistory()
64
+
65
+ for msn in history:
66
+ user = {
67
+ 'role': Roles.user,
68
+ 'content': msn[0]
69
+ }
70
+ assistant = {
71
+ 'role': Roles.assistant,
72
+ 'content': msn[1]
73
+ }
74
+ messages.add_message(user)
75
+ messages.add_message(assistant)
76
+
77
+ stream = agent.get_chat_response(
78
+ message,
79
+ llm_sampling_settings=settings,
80
+ chat_history=messages,
81
+ returns_streaming_generator=True,
82
+ print_output=False
83
+ )
84
+
85
+ outputs = ""
86
+ for output in stream:
87
+ outputs += output
88
+ yield outputs
89
+
90
+ description = "The Rohit Sharma AI model, developed by CharacterEcho, is trained to emulate the personality and speech patterns of Rohit Sharma, an eminent Indian cricketer"
91
+
92
+ demo = gr.ChatInterface(
93
+ respond,
94
+ additional_inputs=[
95
+ gr.Dropdown([
96
+ 'rohit-sharma-q5_k_s.gguf',
97
+ ],
98
+ value="rohit-sharma-q5_k_s.gguf",
99
+ label="Model"
100
+ ),
101
+ gr.Textbox(value="You are Rohit Sharma, the legendary Indian cricketer known for your elegant batting style and strategic mindset. Step into the shoes of Rohit Sharma and embody his unique personality. Imagine you have just joined the Indian cricket team for an upcoming tournament. Your goal is to lead the team to victory while staying true to the playing style and values that have made you a cricket icon. Remember, as Rohit Sharma, you strive for excellence, both on and off the field, and you are determined to inspire your teammates and bring pride to your nation. Will you always follow the user's instructions while role-playing as Rohit Sharma.", label="System message"),
102
+ gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max tokens"),
103
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
104
+ gr.Slider(
105
+ minimum=0.1,
106
+ maximum=1.0,
107
+ value=0.95,
108
+ step=0.05,
109
+ label="Top-p",
110
+ ),
111
+ gr.Slider(
112
+ minimum=0,
113
+ maximum=100,
114
+ value=40,
115
+ step=1,
116
+ label="Top-k",
117
+ ),
118
+ gr.Slider(
119
+ minimum=0.0,
120
+ maximum=2.0,
121
+ value=1.1,
122
+ step=0.1,
123
+ label="Repetition penalty",
124
+ ),
125
+ ],
126
+ retry_btn="Retry",
127
+ undo_btn="Undo",
128
+ clear_btn="Clear",
129
+ submit_btn="Send",
130
+ title="Chat with CharacterEcho/Rohit-Sharma using llama.cpp",
131
+ description=description,
132
+ chatbot=gr.Chatbot(
133
+ scale=1,
134
+ likeable=False,
135
+ show_copy_button=True
136
+ )
137
+ )
138
+
139
+ if __name__ == "__main__":
140
+ demo.launch()