Dharini3 commited on
Commit
90da26b
·
verified ·
1 Parent(s): 4a8cff1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -2
app.py CHANGED
@@ -1,16 +1,145 @@
1
  import os
2
  import gradio as gr
3
- from langchain.chat_models import ChatOpenAI
4
  from langchain import LLMChain, PromptTemplate
5
  from langchain.memory import ConversationBufferMemory
6
 
7
- OPENAI_API_KEY=os.getenv('OPENAI_API_KEY')
8
 
9
  template = """You are a helpful assistant to answer all user queries.
10
  {chat_history}
11
  User: {user_message}
12
  Chatbot:"""
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  prompt = PromptTemplate(
15
  input_variables=["chat_history", "user_message"], template=template
16
  )
 
1
  import os
2
  import gradio as gr
3
+ from langchain_groq import ChatGroq
4
  from langchain import LLMChain, PromptTemplate
5
  from langchain.memory import ConversationBufferMemory
6
 
7
+ GROQ_API_KEY=os.getenv('GROQ_API_KEY')
8
 
9
  template = """You are a helpful assistant to answer all user queries.
10
  {chat_history}
11
  User: {user_message}
12
  Chatbot:"""
13
 
14
+ import os
15
+ from groq import Groq
16
+ import gradio as gr
17
+ import logging
18
+
19
+ # Set up logging configuration
20
+ logging.basicConfig(level=logging.DEBUG)
21
+ logger = logging.getLogger(__name__)
22
+
23
+ # Initialize the Groq client with API key from environment variables
24
+ api_key = os.environ.get("GROQ_API_KEY")
25
+ if not api_key:
26
+ logger.error("GROQ_API_KEY environment variable is not set.")
27
+ raise ValueError("GROQ_API_KEY environment variable is required.")
28
+ client = Groq(api_key=api_key)
29
+
30
+ # Default model name, can be overridden by environment variable
31
+ MODEL_NAME = os.environ.get("MODEL_NAME", "llama3-8b-8192")
32
+
33
+ def get_completion(user_input):
34
+ """
35
+ Generate a chat completion response using the Groq client.
36
+
37
+ Args:
38
+ user_input (str): The user's input query.
39
+
40
+ Returns:
41
+ str: The generated response or an error message.
42
+ """
43
+ try:
44
+ completion = client.chat.completions.create(
45
+ model=MODEL_NAME,
46
+ messages=[
47
+ {"role": "system", "content": "You are a helpful assistant."},
48
+ {"role": "user", "content": user_input}
49
+ ],
50
+ temperature=1,
51
+ max_tokens=1024,
52
+ top_p=1,
53
+ stream=False, # Gradio does not support stream=True
54
+ )
55
+
56
+ # Extract the response
57
+ response = completion.choices[0].message.content.strip()
58
+ return response
59
+ except Exception as e:
60
+ logger.error(f"Error during completion: {e}")
61
+ return "Sorry, I encountered an error while processing your request."
62
+
63
+ def launch_interface():
64
+ """
65
+ Launch the Gradio interface for the chatbot.
66
+ """
67
+ demo = gr.Interface(
68
+ fn=get_completion,
69
+ inputs=gr.Textbox(
70
+ label="Enter your query:",
71
+ placeholder="Ask me anything...",
72
+ lines=2,
73
+ max_lines=5
74
+ ),
75
+ outputs=gr.Textbox(
76
+ label="Response:",
77
+ lines=6,
78
+ max_lines=10
79
+ ),
80
+ title="Mr AI",
81
+ description="""
82
+ <style>
83
+ .gr-box {
84
+ border-radius: 10px;
85
+ border: 2px solid #007BFF;
86
+ padding: 15px;
87
+ background-color: #F8F9FA;
88
+ }
89
+ .gr-input {
90
+ font-size: 1.2em;
91
+ padding: 10px;
92
+ border-radius: 5px;
93
+ border: 2px solid #007BFF;
94
+ margin-bottom: 10px;
95
+ }
96
+ .gr-output {
97
+ font-size: 1.2em;
98
+ padding: 10px;
99
+ border-radius: 5px;
100
+ border: 2px solid #28A745;
101
+ background-color: #E9F7EF;
102
+ }
103
+ .gr-interface-title {
104
+ font-size: 2em;
105
+ font-weight: bold;
106
+ color: #007BFF;
107
+ text-align: center;
108
+ margin-bottom: 20px;
109
+ }
110
+ .gr-interface-description {
111
+ font-size: 1.2em;
112
+ color: #6C757D;
113
+ text-align: center;
114
+ margin-bottom: 20px;
115
+ }
116
+ .gr-button {
117
+ background-color: #007BFF;
118
+ color: white;
119
+ border-radius: 5px;
120
+ padding: 10px 20px;
121
+ font-size: 1em;
122
+ border: none;
123
+ cursor: pointer;
124
+ margin-top: 10px;
125
+ }
126
+ .gr-button:hover {
127
+ background-color: #0056b3;
128
+ }
129
+ </style>
130
+ <div class="gr-interface-title">Welcome to Mr AI</div>
131
+ <div class="gr-interface-description">Ask anything and get a helpful response.</div>
132
+ """,
133
+ allow_flagging="never",
134
+ live=True
135
+ )
136
+
137
+ logger.info("Starting Gradio interface")
138
+ demo.launch(share=True)
139
+
140
+ if __name__ == "__main__":
141
+ launch_interface()
142
+
143
  prompt = PromptTemplate(
144
  input_variables=["chat_history", "user_message"], template=template
145
  )