File size: 3,420 Bytes
1ec0b16
 
 
 
 
b1e86a8
 
b1186d2
1821bae
1ec0b16
b1186d2
24e1a24
b1e86a8
1ec0b16
b1e86a8
 
 
1ec0b16
b1186d2
 
1821bae
b1e86a8
 
1821bae
b1e86a8
1821bae
b1e86a8
1821bae
b1e86a8
1821bae
 
 
 
b1e86a8
 
1ec0b16
1821bae
 
b1e86a8
1821bae
b1e86a8
 
 
 
 
 
 
 
 
b1186d2
1821bae
1ec0b16
 
 
 
 
b1e86a8
 
 
 
b1186d2
 
1821bae
1ec0b16
 
 
b1e86a8
 
 
 
 
 
 
 
 
 
 
 
 
b36b54e
 
1ec0b16
b36b54e
 
1ec0b16
b1e86a8
b36b54e
 
 
 
 
 
 
 
 
 
 
 
 
 
1ec0b16
 
b36b54e
1ec0b16
 
b1186d2
b1e86a8
1ec0b16
b1186d2
1ec0b16
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from groq import Groq
import gradio as gr
from gtts import gTTS
import uuid
import os
from PIL import Image
import pytesseract

# Initialize the Groq client
client = Groq(api_key=os.getenv("GROQ_API_KEY_2"))


# Function to initialize conversation messages
def initialize_messages():
    return [{
        "role": "system",
        "content": '''You are Dr. HealthBuddy, a highly experienced and professional virtual doctor chatbot...'''
    }]


# Function to handle user input and history
def customLLMBot(user_input, image_path, history):
    messages_prmt = history or initialize_messages()
    image_analysis = ""

    # Process the uploaded image
    if image_path:
        try:
            img = Image.open(image_path)
            extracted_text = pytesseract.image_to_string(img)
            image_analysis = f"Analyzed Image: {extracted_text.strip()}"
        except Exception as e:
            image_analysis = f"Error processing the image: {str(e)}"

    # Add user input and image analysis to the conversation
    messages_prmt.append({"role": "user", "content": user_input})
    if image_analysis:
        messages_prmt.append({"role": "user", "content": image_analysis})

    # Generate response using Groq
    try:
        response = client.chat.completions.create(
            messages=messages_prmt,
            model="llama3-8b-8192",
        )
        LLM_reply = response.choices[0].message.content
        messages_prmt.append({"role": "assistant", "content": LLM_reply})
    except Exception as e:
        LLM_reply = f"Error generating response: {str(e)}"

    # Generate audio response
    audio_file = f"response_{uuid.uuid4().hex}.mp3"
    try:
        tts = gTTS(LLM_reply, lang='en')
        tts.save(audio_file)
    except Exception as e:
        audio_file = None
        LLM_reply += f"\n\nError generating audio: {str(e)}"

    return [(user_input, LLM_reply)], audio_file, messages_prmt


# Chatbot UI
def chatbot_ui():
    with gr.Blocks() as demo:
        gr.Markdown("# Healthcare Chatbot Doctor")

        chatbot = gr.Chatbot(label="English Responses")
        user_input = gr.Textbox(
            label="Ask anything related to your health condition",
            placeholder="Enter your symptoms here...",
            lines=1
        )
        image_input = gr.Image(label="Upload an image", type="filepath")
        audio_output = gr.Audio(label="Audio Response")

        submit_btn = gr.Button("Submit")
        clear_btn = gr.Button("Clear")

        # Combine submit button and Enter key functionality
        submit_action = submit_btn.click(
            customLLMBot,
            inputs=[user_input],
            outputs=[chatbot, audio_output],
        )

        user_input_action = user_input.submit(
            customLLMBot,
            inputs=[user_input],
            outputs=[chatbot, audio_output],
        )

        # Reset the textbox after submission
        for action in [submit_action, user_input_action]:
            action.then(
                lambda: "",  # Clear input box
                inputs=[],
                outputs=user_input,
            )

        # Clear button functionality
        clear_btn.click(
            lambda: ([], "", None, [], None),
            inputs=[],
            outputs=[chatbot, user_input, audio_output],
        )

    return demo

# Launch the chatbot UI
chatbot_ui().launch(server_name="0.0.0.0", server_port=7860)