Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,92 +1,72 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 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
|
| 17 |
)
|
| 18 |
|
| 19 |
-
genai.
|
| 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 |
-
|
| 36 |
-
|
| 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 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
User: {user_message}
|
| 49 |
-
Chatbot:
|
| 50 |
"""
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
|
|
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
llm=llm,
|
| 72 |
-
prompt=prompt,
|
| 73 |
-
memory=memory_state,
|
| 74 |
-
verbose=False,
|
| 75 |
-
)
|
| 76 |
|
| 77 |
-
try:
|
| 78 |
-
response = chain.predict(user_message=user_message)
|
| 79 |
except Exception as e:
|
| 80 |
-
|
| 81 |
|
| 82 |
-
history.append((user_message,
|
| 83 |
|
| 84 |
-
return "", history
|
| 85 |
|
| 86 |
|
|
|
|
|
|
|
|
|
|
| 87 |
with gr.Blocks() as demo:
|
| 88 |
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
msg = gr.Textbox(
|
| 92 |
placeholder="Type your message...",
|
|
@@ -95,20 +75,17 @@ with gr.Blocks() as demo:
|
|
| 95 |
|
| 96 |
send = gr.Button("Send")
|
| 97 |
|
| 98 |
-
memory_state = gr.State()
|
| 99 |
-
|
| 100 |
msg.submit(
|
| 101 |
respond,
|
| 102 |
-
[msg, chatbot
|
| 103 |
-
[msg, chatbot
|
| 104 |
)
|
| 105 |
|
| 106 |
send.click(
|
| 107 |
respond,
|
| 108 |
-
[msg, chatbot
|
| 109 |
-
[msg, chatbot
|
| 110 |
)
|
| 111 |
|
| 112 |
-
|
| 113 |
if __name__ == "__main__":
|
| 114 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from google import genai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# -------------------------------
|
| 6 |
# Load Gemini API Key
|
| 7 |
+
# -------------------------------
|
| 8 |
api_key = os.getenv("GOOGLE_API_KEY")
|
| 9 |
|
| 10 |
if not api_key:
|
| 11 |
raise ValueError(
|
| 12 |
+
"GOOGLE_API_KEY not found. Add it in Hugging Face Settings → Variables and Secrets."
|
| 13 |
)
|
| 14 |
|
| 15 |
+
client = genai.Client(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
SYSTEM_PROMPT = """
|
| 18 |
+
Meet Arun, your youthful and witty personal assistant!
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
At 21 years old, he is full of energy and always eager to help.
|
| 21 |
|
| 22 |
+
Arun's goal is to assist users with any questions or problems they have.
|
| 23 |
|
| 24 |
+
His enthusiasm shines through in every response, making conversations enjoyable and engaging.
|
|
|
|
|
|
|
|
|
|
| 25 |
"""
|
| 26 |
|
| 27 |
+
# -------------------------------
|
| 28 |
+
# Chat Function
|
| 29 |
+
# -------------------------------
|
| 30 |
+
def respond(user_message, history):
|
|
|
|
| 31 |
|
| 32 |
+
if history is None:
|
| 33 |
+
history = []
|
| 34 |
|
| 35 |
+
conversation = SYSTEM_PROMPT + "\n\n"
|
| 36 |
|
| 37 |
+
for user, bot in history:
|
| 38 |
+
conversation += f"User: {user}\n"
|
| 39 |
+
conversation += f"Assistant: {bot}\n"
|
| 40 |
|
| 41 |
+
conversation += f"User: {user_message}\nAssistant:"
|
| 42 |
|
| 43 |
+
try:
|
| 44 |
+
response = client.models.generate_content(
|
| 45 |
+
model="gemini-2.5-flash",
|
| 46 |
+
contents=conversation
|
| 47 |
+
)
|
| 48 |
|
| 49 |
+
answer = response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
|
|
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
+
answer = f"❌ Error: {e}"
|
| 53 |
|
| 54 |
+
history.append((user_message, answer))
|
| 55 |
|
| 56 |
+
return "", history
|
| 57 |
|
| 58 |
|
| 59 |
+
# -------------------------------
|
| 60 |
+
# UI
|
| 61 |
+
# -------------------------------
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
|
| 64 |
+
gr.Markdown("# 🤖 Arun AI Assistant")
|
| 65 |
+
|
| 66 |
+
chatbot = gr.Chatbot(
|
| 67 |
+
label="Chat",
|
| 68 |
+
height=500,
|
| 69 |
+
)
|
| 70 |
|
| 71 |
msg = gr.Textbox(
|
| 72 |
placeholder="Type your message...",
|
|
|
|
| 75 |
|
| 76 |
send = gr.Button("Send")
|
| 77 |
|
|
|
|
|
|
|
| 78 |
msg.submit(
|
| 79 |
respond,
|
| 80 |
+
inputs=[msg, chatbot],
|
| 81 |
+
outputs=[msg, chatbot],
|
| 82 |
)
|
| 83 |
|
| 84 |
send.click(
|
| 85 |
respond,
|
| 86 |
+
inputs=[msg, chatbot],
|
| 87 |
+
outputs=[msg, chatbot],
|
| 88 |
)
|
| 89 |
|
|
|
|
| 90 |
if __name__ == "__main__":
|
| 91 |
demo.launch()
|