File size: 3,932 Bytes
8724a9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cd7937
e50b3db
3cd7937
 
 
8724a9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b94628c
8724a9c
 
 
b94628c
8724a9c
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from flask import Flask, request, render_template, jsonify
import google.generativeai as genai
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
from langchain.prompts import PromptTemplate
from dotenv import load_dotenv
from langchain_pinecone import PineconeVectorStore
import os

load_dotenv()
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/chatbot')
def chatbot():
    return render_template('med_chatbot.html')

@app.route('/identification')
def identification():
    return render_template('skin_disease_identification.html')


@app.route('/get', methods=['POST'])
def get_response():
    user_question = request.form['msg']  # Get the user's message from the form data
    response = user_input(user_question)  # Get the response from the bot
    return jsonify({'response': response})  # Send the response back to the client

def user_input(user_question):
    embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
    index_name = "medicalchatbot2"
    vectorstore = PineconeVectorStore(index_name=index_name, embedding=embeddings, namespace="example-namespace")
    docs = vectorstore.similarity_search(user_question, namespace="example-namespace")

    generation_config = {
      "temperature": 1,
      "top_p": 0.95,
      "top_k": 0,
      "max_output_tokens": 8192,
    }

    safety_settings = [
      {
        "category": "HARM_CATEGORY_HARASSMENT",
        "threshold": "BLOCK_MEDIUM_AND_ABOVE"
      },
      {
        "category": "HARM_CATEGORY_HATE_SPEECH",
        "threshold": "BLOCK_MEDIUM_AND_ABOVE"
      },
      {
        "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
        "threshold": "BLOCK_MEDIUM_AND_ABOVE"
      },
      {
        "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
        "threshold": "BLOCK_MEDIUM_AND_ABOVE"
      },
    ]

    model = genai.GenerativeModel(model_name="gemini-1.5-pro-latest",
                                  generation_config=generation_config,
                                  safety_settings=safety_settings)

    prompt_template = """
    you are a Medical Assistant. If user writes name of the disease, then explain the symptoms and treatment for that disease. Answer the question as detailed as possible, make sure to provide all the details, if the answer is not in provided context then create an answer by yourself that makes sense. give the answer point wise.\n\n
    Context:\n {context}?\n
    Question: \n{question}\n
    Answer:
    """

    prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])

    convo = model.start_chat(history=[
        {
            "role": "user",
            "parts": ["I am writing an initial prompt. read it carefully. I am deploying you in medical chatbot. you have to act as a medical assistant. your next question will be asked by a user related to medical information. help the user identify the disease based on his symptoms. give him treatment suggestions. and other relevant information. now when you give the response, do not use any title or header or bold letters. just use line spaces. all the best."]
        },
        {
            "role": "model",
            "parts": ["sounds good! i'm ready to assist users with their medical concerns. just provide me with the user's symptoms and i'll do my best to help diagnose the issue, suggest treatments, and offer relevant information. remember, i'm only an AI assistant, so it's crucial for users to consult with a qualified healthcare professional for a proper diagnosis and treatment plan."]
        },
    ])

    response = convo.send_message(user_question)
    response_text = convo.last.text  # Extract the text from the ChatSession object
    return response_text

def main():
    app.run(debug=True)

if __name__ == "__main__":
    main()