ans123 commited on
Commit
a157698
·
verified ·
1 Parent(s): c036871

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -22
app.py CHANGED
@@ -1,15 +1,19 @@
1
  import gradio as gr
2
  import pandas as pd
3
- from groq import Groq
 
4
 
5
- # Initialize the Groq client with your API key
6
- client = Groq(api_key="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")
 
 
7
 
8
  # Define the system message for the model
9
- system_message = {
10
- "role": "system",
11
- "content": "You are an experienced Fashion designer who starts conversation with proper greeting, giving valuable and catchy fashion advice and suggestions, stays to the point and precise, asks questions only if the user has any concerns over your provided suggestions."
12
- }
 
13
 
14
  # Function to reset the chat
15
  def reset_chat():
@@ -45,22 +49,16 @@ def chat(user_input, messages):
45
  # Append user message to the conversation history
46
  messages.append({"role": "user", "content": user_input})
47
 
 
 
 
 
 
 
48
  try:
49
- # Generate a response from the Groq API
50
- completion = client.chat.completions.create(
51
- model="llama3-8b-8192",
52
- messages=messages,
53
- temperature=1,
54
- max_tokens=1024,
55
- top_p=1,
56
- stream=False,
57
- )
58
-
59
- # Ensure response is valid
60
- if completion.choices and len(completion.choices) > 0:
61
- response_content = completion.choices[0].message.content
62
- else:
63
- response_content = "Sorry, I couldn't generate a response."
64
 
65
  except Exception as e:
66
  response_content = f"Error: {str(e)}"
 
1
  import gradio as gr
2
  import pandas as pd
3
+ import torch
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
 
6
+ # Load the model and tokenizer
7
+ model_name = "nvidia/Llama-3.1-Nemotron-70B-Instruct-HF"
8
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
 
11
  # Define the system message for the model
12
+ system_message = (
13
+ "You are an experienced Fashion designer who starts conversation with proper greeting, "
14
+ "giving valuable and catchy fashion advice and suggestions, stays to the point and precise, "
15
+ "asks questions only if the user has any concerns over your provided suggestions."
16
+ )
17
 
18
  # Function to reset the chat
19
  def reset_chat():
 
49
  # Append user message to the conversation history
50
  messages.append({"role": "user", "content": user_input})
51
 
52
+ # Prepare the input for the model
53
+ input_text = system_message + "\n" + "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])
54
+
55
+ # Tokenize and encode the input text
56
+ inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
57
+
58
  try:
59
+ # Generate a response from the model
60
+ outputs = model.generate(**inputs, max_length=150, num_return_sequences=1, temperature=0.7)
61
+ response_content = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  except Exception as e:
64
  response_content = f"Error: {str(e)}"