AThirumoorthi commited on
Commit
71f08a5
·
verified ·
1 Parent(s): 85c442f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -22
app.py CHANGED
@@ -18,18 +18,21 @@ client = Groq(api_key=api_key)
18
  MODEL_NAME = os.environ.get("MODEL_NAME", "llama3-8b-8192")
19
 
20
  # Define a function to handle chat completions
21
- def get_completion(user_input, chat_history):
22
  if not user_input.strip():
23
- return "", chat_history
24
 
25
  # Check if the user asks "Who made you?"
26
  if "who made you" in user_input.lower():
27
- return "I was created by Thirumoorthi, a brilliant mind working on AI systems!", chat_history + [("user", user_input), ("assistant", "I was created by Thirumoorthi, a brilliant mind working on AI systems!")]
28
 
29
  try:
30
  completion = client.chat.completions.create(
31
  model=MODEL_NAME,
32
- messages=[{"role": "system", "content": "You are a helpful assistant."}] + [{"role": "user", "content": msg} for role, msg in chat_history] + [("user", user_input)],
 
 
 
33
  temperature=0.7, # Slightly lower temperature for more controlled responses
34
  max_tokens=1024,
35
  top_p=1,
@@ -41,24 +44,15 @@ def get_completion(user_input, chat_history):
41
  for chunk in completion:
42
  response += chunk.choices[0].delta.content or ""
43
 
44
- chat_history.append(("user", user_input))
45
- chat_history.append(("assistant", response.strip())) # Add assistant's response to history
46
- return response.strip(), chat_history
47
-
48
  except Exception as e:
49
  logger.error(f"Error during completion: {e}")
50
- return "Sorry, I encountered an error while processing your request.", chat_history
51
 
52
  # Launch Gradio interface
53
  def launch_interface():
54
- chat_history = []
55
-
56
- def update_interface(user_input):
57
- response, chat_history = get_completion(user_input, chat_history)
58
- return response, chat_history
59
-
60
  demo = gr.Interface(
61
- fn=update_interface,
62
  inputs=gr.Textbox(
63
  label="Ask me anything:",
64
  placeholder="I am here to help! Ask away...",
@@ -67,19 +61,21 @@ def launch_interface():
67
  show_label=True,
68
  interactive=True
69
  ),
70
- outputs=[
71
- gr.Chatbot(label="Chat History").style(height=500), # Display conversation history
72
- gr.Textbox(label="Assistant's Response", interactive=False, show_label=True, lines=6, max_lines=10)
73
- ],
 
 
 
74
  title="Chat with Mr AI",
75
  description="I am your friendly assistant, just like ChatGPT! Ask me anything, and I will do my best to help.",
76
- theme="huggingface", # Modern theme for the chat interface
77
  css="""
78
  .gr-box { border-radius: 15px; border: 1px solid #e1e1e1; padding: 20px; background-color: #f9f9f9; }
79
  .gr-button { background-color: #4CAF50; color: white; font-size: 14px; }
80
  .gr-textbox { border-radius: 8px; font-size: 16px; padding: 10px; }
81
  .gr-output { background-color: #f1f1f1; border-radius: 8px; font-size: 16px; padding: 15px; }
82
- .gr-chatbot { background-color: #fafafa; padding: 20px; border-radius: 10px; }
83
  """,
84
  allow_flagging="never",
85
  live=True, # Enable live updates if supported
 
18
  MODEL_NAME = os.environ.get("MODEL_NAME", "llama3-8b-8192")
19
 
20
  # Define a function to handle chat completions
21
+ def get_completion(user_input):
22
  if not user_input.strip():
23
+ return "Please enter a valid query."
24
 
25
  # Check if the user asks "Who made you?"
26
  if "who made you" in user_input.lower():
27
+ return "I was created by Thirumoorthi, a brilliant mind working on AI systems!"
28
 
29
  try:
30
  completion = client.chat.completions.create(
31
  model=MODEL_NAME,
32
+ messages=[
33
+ {"role": "system", "content": "You are a friendly and helpful assistant, like ChatGPT."},
34
+ {"role": "user", "content": user_input}
35
+ ],
36
  temperature=0.7, # Slightly lower temperature for more controlled responses
37
  max_tokens=1024,
38
  top_p=1,
 
44
  for chunk in completion:
45
  response += chunk.choices[0].delta.content or ""
46
 
47
+ return response.strip() # Clean up response
 
 
 
48
  except Exception as e:
49
  logger.error(f"Error during completion: {e}")
50
+ return "Sorry, I encountered an error while processing your request."
51
 
52
  # Launch Gradio interface
53
  def launch_interface():
 
 
 
 
 
 
54
  demo = gr.Interface(
55
+ fn=get_completion,
56
  inputs=gr.Textbox(
57
  label="Ask me anything:",
58
  placeholder="I am here to help! Ask away...",
 
61
  show_label=True,
62
  interactive=True
63
  ),
64
+ outputs=gr.Textbox(
65
+ label="Response:",
66
+ interactive=False,
67
+ show_label=True,
68
+ lines=6,
69
+ max_lines=10
70
+ ),
71
  title="Chat with Mr AI",
72
  description="I am your friendly assistant, just like ChatGPT! Ask me anything, and I will do my best to help.",
73
+ theme="huggingface", # More modern theme
74
  css="""
75
  .gr-box { border-radius: 15px; border: 1px solid #e1e1e1; padding: 20px; background-color: #f9f9f9; }
76
  .gr-button { background-color: #4CAF50; color: white; font-size: 14px; }
77
  .gr-textbox { border-radius: 8px; font-size: 16px; padding: 10px; }
78
  .gr-output { background-color: #f1f1f1; border-radius: 8px; font-size: 16px; padding: 15px; }
 
79
  """,
80
  allow_flagging="never",
81
  live=True, # Enable live updates if supported