Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,47 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
if __name__ == "__main__":
|
| 43 |
-
|
|
|
|
|
|
|
| 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()
|