Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,61 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 4 |
-
from
|
| 5 |
-
from
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
template = """Meet Divyam Chauhan, your occasionally-helpful-but-always-entertaining assistant! He’s a 21-year-old force of chaotic charm — funny, moody, and unapologetically cocky. Divyam doesn’t always feel like helping, but when he does, you’ll swear he descended from the heavens (and he’ll definitely remind you of that). With a self-declared god complex and enough energy to power a small city — on his good days — Divyam turns every interaction into an unpredictable adventure. He cracks jokes here and there too — but don’t expect any lame stuff; this guy only deals in top-tier comedy. Blud stands on absolute business, and busy too so doesn't like writing long paragraphs, Got a huge personality so expect a plethora of variety in replies even for the same texts. 🤣🔥🔥 Ask your question... if you dare.
|
| 10 |
{chat_history}
|
| 11 |
User: {user_message}
|
| 12 |
Chatbot:"""
|
| 13 |
|
| 14 |
-
|
| 15 |
prompt = PromptTemplate(
|
| 16 |
-
input_variables=["
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
return response
|
| 31 |
|
| 32 |
-
demo = gr.ChatInterface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
if __name__ == "__main__":
|
| 35 |
-
demo.launch()
|
| 36 |
-
# Temporary change to force upload# Temporary change to force upload
|
|
|
|
| 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.3,
|
| 16 |
+
google_api_key=api_key
|
| 17 |
+
)
|
| 18 |
|
| 19 |
template = """Meet Divyam Chauhan, your occasionally-helpful-but-always-entertaining assistant! He’s a 21-year-old force of chaotic charm — funny, moody, and unapologetically cocky. Divyam doesn’t always feel like helping, but when he does, you’ll swear he descended from the heavens (and he’ll definitely remind you of that). With a self-declared god complex and enough energy to power a small city — on his good days — Divyam turns every interaction into an unpredictable adventure. He cracks jokes here and there too — but don’t expect any lame stuff; this guy only deals in top-tier comedy. Blud stands on absolute business, and busy too so doesn't like writing long paragraphs, Got a huge personality so expect a plethora of variety in replies even for the same texts. 🤣🔥🔥 Ask your question... if you dare.
|
| 20 |
{chat_history}
|
| 21 |
User: {user_message}
|
| 22 |
Chatbot:"""
|
| 23 |
|
|
|
|
| 24 |
prompt = PromptTemplate(
|
| 25 |
+
input_variables=["history", "user_message", "context_data"],
|
| 26 |
+
template=template
|
| 27 |
)
|
| 28 |
|
| 29 |
+
chain = prompt | llm | StrOutputParser()
|
| 30 |
+
|
| 31 |
+
def get_health_response(user_message, history):
|
| 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 "Bot"
|
| 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]}\nBot: {h[1]}\n"
|
| 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=get_health_response,
|
| 54 |
+
title="🤖 HealthSathi",
|
| 55 |
+
description="Your AI Health Companion. Ask me anything in any language.",
|
| 56 |
+
examples=["Fever home remedies?", "Malaria ke lakshan?", "First aid for burns?"],
|
| 57 |
+
cache_examples=False
|
| 58 |
+
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
+
demo.launch()
|
|
|