emizemani commited on
Commit
26f89b1
·
verified ·
1 Parent(s): 880feff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -58
app.py CHANGED
@@ -1,63 +1,82 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("emizemani/editlyai-gguf/editlyai.gguf")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
  )
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  if __name__ == "__main__":
63
- demo.launch()
 
1
+ import os
2
  import gradio as gr
3
+ from huggingface_hub import hf_hub_download
4
+ from llama_cpp import Llama
5
+ from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
6
+ from llama_cpp_agent.providers import LlamaCppPythonProvider
7
+ from llama_cpp_agent.chat_history import BasicChatHistory
8
+ from llama_cpp_agent.chat_history.messages import Roles
9
+
10
+ # Set the repository ID and the filename for the model
11
+ repo_id = "emizemani/editlyai-gguf"
12
+ model_filename = "editlyai.gguf"
13
+
14
+ # Path to store the downloaded model
15
+ model_path = f"./models/{model_filename}"
16
+
17
+ # Download the model if it's not already downloaded
18
+ if not os.path.exists(model_path):
19
+ hf_hub_download(
20
+ repo_id=repo_id,
21
+ filename=model_filename,
22
+ local_dir="./models"
23
+ )
24
+
25
+ # Initialize the Llama model
26
+ llm = Llama(
27
+ model_path=model_path,
28
+ flash_attn=True,
29
+ n_gpu_layers=81, # Adjust based on your model's requirements
30
+ n_batch=1024,
31
+ n_ctx=8192,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  )
33
 
34
+ def respond(message, system_message):
35
+ # Define the provider and agent for interaction
36
+ provider = LlamaCppPythonProvider(llm)
37
+ agent = LlamaCppAgent(
38
+ provider,
39
+ system_prompt=system_message,
40
+ predefined_messages_formatter_type=MessagesFormatterType.GEMMA_2,
41
+ debug_output=True
42
+ )
43
+
44
+ # Default settings for the model interaction
45
+ settings = provider.get_provider_default_settings()
46
+ messages = BasicChatHistory()
47
+ messages.add_message({'role': Roles.system, 'content': system_message})
48
+ messages.add_message({'role': Roles.user, 'content': message})
49
+
50
+ # Get responses as a stream from the model
51
+ stream = agent.get_chat_response(
52
+ message,
53
+ llm_sampling_settings=settings,
54
+ chat_history=messages,
55
+ returns_streaming_generator=True,
56
+ print_output=False
57
+ )
58
+
59
+ output = ""
60
+ for data in stream:
61
+ output += data
62
+ yield output
63
+
64
+ # Define a comprehensive system prompt for Editly AI
65
+ system_prompt = "Hello, I am Editly AI, your intelligent text editing assistant. " \
66
+ "I specialize in correcting grammar, enhancing clarity, and refining the style of your text. " \
67
+ "Please type the text you want edited, and I will provide suggestions to improve it."
68
+
69
+ # Create the Gradio interface
70
+ demo = gr.Interface(
71
+ fn=respond,
72
+ inputs=[
73
+ gr.Textbox(label="Enter your text here", placeholder="Type here..."),
74
+ gr.Textbox(default=system_prompt, label="System message", visible=False),
75
+ ],
76
+ outputs="text",
77
+ title="Editly AI - Text Editing Assistant",
78
+ description="Interact with Editly AI to refine and enhance your text."
79
+ )
80
 
81
  if __name__ == "__main__":
82
+ demo.launch()