iamkdp commited on
Commit
3b29212
·
verified ·
1 Parent(s): b92e7a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -41
app.py CHANGED
@@ -1,43 +1,47 @@
1
- import os
2
  import gradio as gr
3
- from langchain_groq import ChatGroq
4
- from langchain_core.prompts import ChatPromptTemplate
5
-
6
- # Get API key from environment variable
7
- groq_api_key = os.environ.get("GROQ_API_KEY")
8
-
9
- # Check if API key is set
10
- if not groq_api_key:
11
- raise ValueError("❌ GROQ_API_KEY is not set. Please add it in Hugging Face Secrets.")
12
-
13
- # Initialize Groq LLM
14
- llm = ChatGroq(
15
- groq_api_key=groq_api_key,
16
- model_name="llama3-8b-8192" # or "mixtral-8x7b-32768"
17
- )
18
-
19
- # Prompt template
20
- prompt = ChatPromptTemplate.from_template(
21
- "You are Krish, a wise and witty assistant. Answer the following question:\n\n{question}"
22
- )
23
-
24
- # Response function
25
- def chat_with_krish(message):
26
- try:
27
- chain = prompt | llm
28
- response = chain.invoke({"question": message})
29
- return response.content
30
- except Exception as e:
31
- return f"❌ Error: {e}"
32
-
33
- # Gradio chat interface
34
- ui = gr.ChatInterface(
35
- fn=chat_with_krish,
36
- title="🦚 Meet Krish",
37
- description="A wise, witty, and compassionate friend",
38
- theme="soft"
39
- )
40
-
41
- # Launch Gradio app
 
 
 
 
 
42
  if __name__ == "__main__":
43
- ui.launch()
 
 
1
  import gradio as gr
2
+ from langchain.chat_models import HuggingFaceChat
3
+ from langchain.chains import ConversationChain
4
+ from langchain.memory import ConversationBufferMemory
5
+
6
+ MODEL_ID = "google/gemma-2b" # Adjust if you have a local path or HF repo
7
+
8
+ def load_model():
9
+ # Load Gemma 2B chat model on CPU
10
+ return HuggingFaceChat(
11
+ model=MODEL_ID,
12
+ model_kwargs={"device_map": "cpu"},
13
+ temperature=0.7,
14
+ )
15
+
16
+ def main():
17
+ llm = load_model()
18
+ memory = ConversationBufferMemory(return_messages=True)
19
+ conversation = ConversationChain(llm=llm, memory=memory)
20
+
21
+ def respond(user_input):
22
+ response = conversation.run(user_input)
23
+ return response
24
+
25
+ title = "🦚 Meet Krish"
26
+ description = "A wise, witty, and compassionate friend — Chatbot named KrishWay"
27
+
28
+ with gr.Blocks() as demo:
29
+ gr.Markdown(f"# {title}")
30
+ gr.Markdown(description)
31
+
32
+ chatbot = gr.Chatbot()
33
+ msg = gr.Textbox(placeholder="Say something to Krish...")
34
+ clear = gr.Button("Clear")
35
+
36
+ def user_message(text, chat_history):
37
+ bot_response = respond(text)
38
+ chat_history = chat_history + [(text, bot_response)]
39
+ return "", chat_history
40
+
41
+ msg.submit(user_message, [msg, chatbot], [msg, chatbot])
42
+ clear.click(lambda: [], None, chatbot)
43
+
44
+ demo.launch()
45
+
46
  if __name__ == "__main__":
47
+ main()