Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,48 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
-
from langchain_openai import ChatOpenAI
|
| 5 |
from langchain.chains import LLMChain
|
| 6 |
from langchain.prompts import PromptTemplate
|
| 7 |
from langchain.memory import ConversationBufferMemory
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# Load
|
| 10 |
-
|
| 11 |
|
| 12 |
-
if not
|
| 13 |
raise ValueError(
|
| 14 |
-
"
|
| 15 |
)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
template = """
|
| 19 |
-
Meet Arun, your youthful and witty personal assistant!
|
| 20 |
|
| 21 |
-
At 21 years old, he is full of energy and always eager to help.
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
Current Conversation:
|
| 28 |
{chat_history}
|
| 29 |
|
| 30 |
User: {user_message}
|
|
@@ -37,24 +55,13 @@ prompt = PromptTemplate(
|
|
| 37 |
)
|
| 38 |
|
| 39 |
|
| 40 |
-
# Create a new memory for each user session
|
| 41 |
def new_memory():
|
| 42 |
-
return ConversationBufferMemory(
|
| 43 |
-
memory_key="chat_history",
|
| 44 |
-
input_key="user_message",
|
| 45 |
-
return_messages=False,
|
| 46 |
-
)
|
| 47 |
|
| 48 |
|
| 49 |
-
|
| 50 |
-
llm = ChatOpenAI(
|
| 51 |
-
model="gpt-4o-mini",
|
| 52 |
-
temperature=0.5,
|
| 53 |
-
api_key=openai_api_key,
|
| 54 |
-
)
|
| 55 |
|
| 56 |
|
| 57 |
-
# Chat Function
|
| 58 |
def respond(user_message, history, memory_state):
|
| 59 |
|
| 60 |
if memory_state is None:
|
|
@@ -69,7 +76,6 @@ def respond(user_message, history, memory_state):
|
|
| 69 |
|
| 70 |
try:
|
| 71 |
response = chain.predict(user_message=user_message)
|
| 72 |
-
|
| 73 |
except Exception as e:
|
| 74 |
response = f"❌ Error: {str(e)}"
|
| 75 |
|
|
@@ -78,15 +84,9 @@ def respond(user_message, history, memory_state):
|
|
| 78 |
return "", history, memory_state
|
| 79 |
|
| 80 |
|
| 81 |
-
|
| 82 |
-
with gr.Blocks(title="Arun AI Assistant") as demo:
|
| 83 |
-
|
| 84 |
-
gr.Markdown("# 🤖 Arun AI Assistant")
|
| 85 |
|
| 86 |
-
chatbot = gr.Chatbot(
|
| 87 |
-
label="Chat",
|
| 88 |
-
height=500,
|
| 89 |
-
)
|
| 90 |
|
| 91 |
msg = gr.Textbox(
|
| 92 |
placeholder="Type your message...",
|
|
@@ -99,14 +99,14 @@ with gr.Blocks(title="Arun AI Assistant") as demo:
|
|
| 99 |
|
| 100 |
msg.submit(
|
| 101 |
respond,
|
| 102 |
-
|
| 103 |
-
|
| 104 |
)
|
| 105 |
|
| 106 |
send.click(
|
| 107 |
respond,
|
| 108 |
-
|
| 109 |
-
|
| 110 |
)
|
| 111 |
|
| 112 |
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import google.generativeai as genai
|
|
|
|
| 4 |
from langchain.chains import LLMChain
|
| 5 |
from langchain.prompts import PromptTemplate
|
| 6 |
from langchain.memory import ConversationBufferMemory
|
| 7 |
+
from langchain.llms.base import LLM
|
| 8 |
+
from typing import Optional, List
|
| 9 |
+
|
| 10 |
|
| 11 |
+
# Load Gemini API Key
|
| 12 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
| 13 |
|
| 14 |
+
if not api_key:
|
| 15 |
raise ValueError(
|
| 16 |
+
"GOOGLE_API_KEY not found. Add it in Hugging Face Settings -> Variables and Secrets."
|
| 17 |
)
|
| 18 |
|
| 19 |
+
genai.configure(api_key=api_key)
|
|
|
|
|
|
|
| 20 |
|
|
|
|
| 21 |
|
| 22 |
+
# Gemini Wrapper for LangChain
|
| 23 |
+
class GeminiLLM(LLM):
|
| 24 |
+
model_name: str = "gemini-2.5-flash"
|
| 25 |
+
|
| 26 |
+
@property
|
| 27 |
+
def _llm_type(self):
|
| 28 |
+
return "gemini"
|
| 29 |
|
| 30 |
+
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
|
| 31 |
+
model = genai.GenerativeModel(self.model_name)
|
| 32 |
+
response = model.generate_content(prompt)
|
| 33 |
+
return response.text
|
| 34 |
+
|
| 35 |
+
@property
|
| 36 |
+
def _identifying_params(self):
|
| 37 |
+
return {"model_name": self.model_name}
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
template = """Meet Arun, your youthful and witty personal assistant!
|
| 41 |
+
|
| 42 |
+
At 21 years old, he is full of energy and always eager to help.
|
| 43 |
+
|
| 44 |
+
His goal is to assist users with any questions.
|
| 45 |
|
|
|
|
| 46 |
{chat_history}
|
| 47 |
|
| 48 |
User: {user_message}
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
|
|
|
|
| 58 |
def new_memory():
|
| 59 |
+
return ConversationBufferMemory(memory_key="chat_history")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
+
llm = GeminiLLM()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
|
|
|
|
| 65 |
def respond(user_message, history, memory_state):
|
| 66 |
|
| 67 |
if memory_state is None:
|
|
|
|
| 76 |
|
| 77 |
try:
|
| 78 |
response = chain.predict(user_message=user_message)
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
response = f"❌ Error: {str(e)}"
|
| 81 |
|
|
|
|
| 84 |
return "", history, memory_state
|
| 85 |
|
| 86 |
|
| 87 |
+
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
chatbot = gr.Chatbot(label="Arun AI Assistant")
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
msg = gr.Textbox(
|
| 92 |
placeholder="Type your message...",
|
|
|
|
| 99 |
|
| 100 |
msg.submit(
|
| 101 |
respond,
|
| 102 |
+
[msg, chatbot, memory_state],
|
| 103 |
+
[msg, chatbot, memory_state],
|
| 104 |
)
|
| 105 |
|
| 106 |
send.click(
|
| 107 |
respond,
|
| 108 |
+
[msg, chatbot, memory_state],
|
| 109 |
+
[msg, chatbot, memory_state],
|
| 110 |
)
|
| 111 |
|
| 112 |
|