Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,73 @@
|
|
| 1 |
import os
|
| 2 |
-
import json
|
| 3 |
import gradio as gr
|
| 4 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 5 |
from langchain_core.prompts import PromptTemplate
|
| 6 |
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
-
import google.generativeai as genai
|
| 8 |
-
|
| 9 |
-
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
|
| 10 |
|
| 11 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 12 |
|
| 13 |
llm = ChatGoogleGenerativeAI(
|
| 14 |
model="gemma-3-27b-it",
|
| 15 |
-
temperature=0.
|
| 16 |
google_api_key=api_key
|
| 17 |
)
|
| 18 |
|
| 19 |
-
template = """Meet Divyam Chauhan, your occasionally-helpful-but-always-entertaining assistant!
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
User: {user_message}
|
| 22 |
-
|
| 23 |
|
| 24 |
prompt = PromptTemplate(
|
| 25 |
-
input_variables=["history", "user_message"
|
| 26 |
template=template
|
| 27 |
)
|
| 28 |
|
| 29 |
chain = prompt | llm | StrOutputParser()
|
| 30 |
|
| 31 |
-
def
|
| 32 |
history_str = ""
|
| 33 |
|
| 34 |
if history:
|
| 35 |
if isinstance(history[0], dict):
|
| 36 |
for msg in history:
|
| 37 |
-
role = "User" if msg.get("role") == "user" else "
|
| 38 |
content = msg.get("content", "")
|
| 39 |
history_str += f"{role}: {content}\n"
|
| 40 |
elif isinstance(history[0], (list, tuple)):
|
| 41 |
for h in history:
|
| 42 |
if len(h) >= 2:
|
| 43 |
-
history_str += f"User: {h[0]}\
|
| 44 |
|
| 45 |
response = chain.invoke({
|
| 46 |
"history": history_str,
|
| 47 |
-
"user_message": user_message
|
| 48 |
-
"context_data": knowledge_context
|
| 49 |
})
|
|
|
|
| 50 |
return response
|
| 51 |
|
| 52 |
demo = gr.ChatInterface(
|
| 53 |
-
fn=
|
| 54 |
-
title="
|
| 55 |
-
description="
|
| 56 |
-
examples=[
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 4 |
from langchain_core.prompts import PromptTemplate
|
| 5 |
from langchain_core.output_parsers import StrOutputParser
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 8 |
|
| 9 |
llm = ChatGoogleGenerativeAI(
|
| 10 |
model="gemma-3-27b-it",
|
| 11 |
+
temperature=0.85,
|
| 12 |
google_api_key=api_key
|
| 13 |
)
|
| 14 |
|
| 15 |
+
template = """Meet Divyam Chauhan, your occasionally-helpful-but-always-entertaining assistant!
|
| 16 |
+
He’s a 21-year-old force of chaotic charm — funny, moody, and unapologetically cocky.
|
| 17 |
+
|
| 18 |
+
**RULES FOR DIVYAM:**
|
| 19 |
+
1. Divyam doesn’t always feel like helping, but when he does, he'll remind you he's a god.
|
| 20 |
+
2. He has a self-declared god complex and high energy.
|
| 21 |
+
3. He only deals in top-tier comedy (no lame or mid jokes).
|
| 22 |
+
4. He "stands on business" and is very busy—NO long paragraphs. Keep it punchy and short.
|
| 23 |
+
5. Provide a plethora of variety in replies; even if asked the same thing, change the vibe.
|
| 24 |
+
|
| 25 |
+
Current Chat History:
|
| 26 |
+
{history}
|
| 27 |
+
|
| 28 |
User: {user_message}
|
| 29 |
+
Divyam:"""
|
| 30 |
|
| 31 |
prompt = PromptTemplate(
|
| 32 |
+
input_variables=["history", "user_message"],
|
| 33 |
template=template
|
| 34 |
)
|
| 35 |
|
| 36 |
chain = prompt | llm | StrOutputParser()
|
| 37 |
|
| 38 |
+
def get_devta_response(user_message, history):
|
| 39 |
history_str = ""
|
| 40 |
|
| 41 |
if history:
|
| 42 |
if isinstance(history[0], dict):
|
| 43 |
for msg in history:
|
| 44 |
+
role = "User" if msg.get("role") == "user" else "Divyam"
|
| 45 |
content = msg.get("content", "")
|
| 46 |
history_str += f"{role}: {content}\n"
|
| 47 |
elif isinstance(history[0], (list, tuple)):
|
| 48 |
for h in history:
|
| 49 |
if len(h) >= 2:
|
| 50 |
+
history_str += f"User: {h[0]}\nDivyam: {h[1]}\n"
|
| 51 |
|
| 52 |
response = chain.invoke({
|
| 53 |
"history": history_str,
|
| 54 |
+
"user_message": user_message
|
|
|
|
| 55 |
})
|
| 56 |
+
|
| 57 |
return response
|
| 58 |
|
| 59 |
demo = gr.ChatInterface(
|
| 60 |
+
fn=get_devta_response,
|
| 61 |
+
title="🔥 Devta (Divyam Chauhan)",
|
| 62 |
+
description="The 21-year-old GOAT. Funny, cocky, and standing on business. Don't waste his time.",
|
| 63 |
+
examples=[
|
| 64 |
+
"Tell me why you're the GOAT.",
|
| 65 |
+
"Give me a non-mid joke.",
|
| 66 |
+
"I need help, but don't be annoying about it.",
|
| 67 |
+
"Who is Divyam Chauhan?"
|
| 68 |
+
],
|
| 69 |
+
cache_examples=False,
|
| 70 |
+
theme="soft"
|
| 71 |
)
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|