Waheeb2001 commited on
Commit
7dae0f6
·
verified ·
1 Parent(s): a83bfef

Rename app.py to main.py

Browse files
Files changed (2) hide show
  1. app.py +0 -60
  2. main.py +51 -0
app.py DELETED
@@ -1,60 +0,0 @@
1
- from ctransformers import AutoModelForCausalLM
2
- import gradio as gr
3
-
4
- greety = """
5
- Follow us [Gathnex](https://medium.com/@gathnex), [linkedin](https://www.linkedin.com/company/gathnex/) and [Github](https://github.com/gathnexadmin) for more update on Genrative AI, LLM,etc. A special thanks to the Gathnex team members who made a significant contribution to this project.
6
- """
7
-
8
- llm = AutoModelForCausalLM.from_pretrained("zephyr-7b-beta.Q4_K_S.gguf",
9
- model_type='mistral',
10
- max_new_tokens = 1096,
11
- threads = 3,
12
- )
13
-
14
- def stream(prompt, UL):
15
- system_prompt = 'You are a helpful AI assistant'
16
- E_INST = "</s>"
17
- user, assistant = "<|user|>", "<|assistant|>"
18
- prompt = f"{system_prompt}{E_INST}\n{user}\n{prompt.strip()}{E_INST}\n{assistant}\n"
19
- return llm(prompt)
20
-
21
- css = """
22
- h1 {
23
- text-align: center;
24
- }
25
- #duplicate-button {
26
- margin: auto;
27
- color: white;
28
- background: #1565c0;
29
- border-radius: 100vh;
30
- }
31
- .contain {
32
- max-width: 900px;
33
- margin: auto;
34
- padding-top: 1.5rem;
35
- }
36
- """
37
-
38
- chat_interface = gr.ChatInterface(
39
- fn=stream,
40
- #additional_inputs_accordion_name = "Credentials",
41
- #additional_inputs=[
42
- # gr.Textbox(label="OpenAI Key", lines=1),
43
- # gr.Textbox(label="Linkedin Access Token", lines=1),
44
- #],
45
- stop_btn=None,
46
- examples=[
47
- ["explain Large language model"],
48
- ["what is quantum computing"]
49
- ],
50
- )
51
-
52
- with gr.Blocks(css=css) as demo:
53
- gr.HTML("<h1><center>Gathnex Free LLM Deployment Space<h1><center>")
54
- gr.HTML("<h3><center><a href='https://medium.com/@gathnex'>Gathnex AI</a>💬<h3><center>")
55
- gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
56
- chat_interface.render()
57
- gr.Markdown(greety)
58
-
59
- if __name__ == "__main__":
60
- demo.queue(max_size=10).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
main.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ctransformers import AutoModelForCausalLM
2
+ from gradio import Chatbot, Interface
3
+ import gradio as gr
4
+
5
+ # Load the GGUF model
6
+ llm = AutoModelForCausalLM.from_pretrained(
7
+ "zephyr-7b-beta.Q4_K_S.gguf",
8
+ model_type="mistral",
9
+ max_new_tokens=1096,
10
+ threads=3
11
+ )
12
+
13
+ # Format prompt with system message and chat history
14
+ def format_prompt(message, chat_history):
15
+ system_prompt = "Below is an instruction that describes a task. Write a response that appropriately completes the request."
16
+ E_INST = "</s>"
17
+ user, assistant = "<|user|>", "<|assistant|>"
18
+ prompt = f"{system_prompt}{E_INST}\n"
19
+ for user_msg, bot_msg in chat_history:
20
+ prompt += f"{user}\n{user_msg}{E_INST}\n{assistant}\n{bot_msg}{E_INST}\n"
21
+ prompt += f"{user}\n{message}{E_INST}\n{assistant}\n"
22
+ return prompt
23
+
24
+ # Define chatbot function
25
+ def respond(message, chat_history):
26
+ formatted_prompt = format_prompt(message, chat_history)
27
+ response = llm(formatted_prompt)
28
+ chat_history.append((message, response))
29
+ return chat_history, chat_history
30
+
31
+ # Create Gradio Chatbot UI
32
+ chatbot = Chatbot(
33
+ bubble_full_width=False,
34
+ height=500
35
+ )
36
+
37
+ # Launch interface
38
+ with gr.Blocks() as demo:
39
+ gr.Markdown("## Zephyr LLM Chat Interface")
40
+ chatbot = gr.Chatbot()
41
+ msg = gr.Textbox(label="Your Message")
42
+ clear = gr.Button("Clear Chat")
43
+
44
+ state = gr.State([])
45
+
46
+ msg.submit(respond, [msg, state], [chatbot, state])
47
+ clear.click(lambda: ([], []), None, [chatbot, state])
48
+
49
+ # Launch Gradio app
50
+ if __name__ == "__main__":
51
+ demo.launch()