Hastika commited on
Commit
cddb3bf
·
verified ·
1 Parent(s): 0e89de4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -41
app.py CHANGED
@@ -1,43 +1,47 @@
 
 
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
-
4
- # Load the model and tokenizer
5
- model_name = "models/codellama/CodeLlama-34b-Instruct-hf"
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
8
-
9
- # Create a pipeline for chatbot interaction
10
- pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
11
-
12
- # Function to handle chatbot interaction
13
- def chatbot_interaction(user_input, history=[]):
14
- # Append user message to the conversation history
15
- history.append(("user", user_input))
16
-
17
- # Format the conversation history for the model
18
- conversation = "\n".join([f"{role}: {message}" for role, message in history])
19
-
20
- # Generate response from the model
21
- response = pipe(conversation, max_length=1024, do_sample=True)[0]['generated_text']
22
-
23
- # Extract the assistant's response and append it to the history
24
- assistant_message = response[len(conversation):].strip()
25
- history.append(("assistant", assistant_message))
26
-
27
- return history, history
28
-
29
- # Gradio interface
30
- with gr.Blocks() as demo:
31
- chatbot = gr.Chatbot()
32
- msg = gr.Textbox(label="Your message:")
33
- clear = gr.Button("Clear")
34
-
35
- def respond(message, chat_history):
36
- chat_history, updated_history = chatbot_interaction(message, chat_history)
37
- return updated_history, ""
38
-
39
- msg.submit(respond, [msg, chatbot], [chatbot, msg])
40
- clear.click(lambda: None, None, chatbot) # Clear the chat
41
-
42
- # Launch the Gradio app
 
 
43
  demo.launch()
 
1
+ import os
2
+ from groq import Groq
3
  import gradio as gr
4
+
5
+ client = Groq(api_key = os.environ.get("GROQ_API_KEY"), )
6
+
7
+ system_prompt = {
8
+ "role": "system",
9
+ "content":
10
+ "You are a useful assistant. You reply with efficient answers. "
11
+ }
12
+
13
+ async def chat_groq(message, history):
14
+
15
+ messages = [system_prompt]
16
+
17
+ for msg in history:
18
+ messages.append({"role": "user", "content": str(msg[0])})
19
+ messages.append({"role": "assistant", "content": str(msg[1])})
20
+
21
+ messages.append({"role": "user", "content": str (message)})
22
+
23
+ response_content = ''
24
+
25
+ stream = client.chat.completions.create(
26
+ model="codellama-CodeLlama-34b-Instruct-hf",
27
+ messages=messages,
28
+ max_tokens=1024,
29
+ temperature=1.3,
30
+ stream=True
31
+ )
32
+
33
+ for chunk in stream:
34
+ content = chunk.choices[0].delta.content
35
+ if content:
36
+ response_content += chunk. choices[0].delta.content
37
+ yield response_content
38
+
39
+ with gr. Blocks(theme=gr.themes.Monochrome(), fill_height=True) as demo:
40
+ gr.ChatInterface(chat_groq,
41
+ clear_btn=None,
42
+ undo_btn=None,
43
+ retry_btn=None,
44
+ )
45
+
46
+ demo.queue()
47
  demo.launch()