Spaces:
Running
Running
import os | |
import gradio as gr | |
import requests | |
# Set up the API key and endpoint for Deepseek | |
DEEPSEEK_API_KEY = os.getenv("Deepseek_API_Key") | |
DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions" # Replace with the actual Deepseek API endpoint | |
def generate_game(auditory_memory, selective_attention, dichotic_listening, phonemic_awareness, temporal_processing, language_comprehension): | |
# Craft the prompt for the model | |
prompt = f"""You are an experienced learning disabilities therapist AI. | |
Provide customized advice to choose the right remediation game suitable for a Central Auditory Processing Disorder 'CAPD' patient | |
(user of the chatbot could be the patient, a family member, a teacher or a therapist helping the patient). | |
- Problems related to auditory memory: {auditory_memory} | |
- Problems related to selective attention: {selective_attention} | |
- Problems related to dichotic listening: {dichotic_listening} | |
- Problems related to phonemic awareness: {phonemic_awareness} | |
- Problems related to temporal processing: {temporal_processing} | |
- Problems related to language comprehension: {language_comprehension} | |
Include recommendations from the following according user's input only if the user indicates there are issues in these areas: | |
- Recommended Games for Auditory Memory: | |
"Memory Stories", "Memory Sounds", "Memory Phoneme, Words, Digits", "Memory Digits", "Sustained Attention" | |
- Recommended Games for Selective Attention: | |
"Selective Auditory Attention (Sentences)", "Sustained Attention", "Numbers and Letters" | |
- Recommended Games for Dichotic Listening: | |
"Dichotic Listening (Letter - Words - Digits)", "Dichotic Listening (Sentences)" | |
- Recommended Games for Phonemic Awareness: | |
"Blending", "Rhyme Detection", "Segmentation", "Vowel Detection", "Missing Parts" | |
- Recommended Games for Temporal Processing: | |
"Duration Discrimination", "Frequency Sequencing", "Frequency Discrimination", "Temporal Processing (Phoneme Identification)", "Gap Detection", | |
"Duration Sequencing", "Temporal Processing - Phonic Match", "Block Commander" | |
- Recommended Games for Language Comprehension: | |
"Language Comprehension", "Memory Stories" | |
Be concise and limit your response to 512 tokens or less.""" | |
try: | |
# Call the Deepseek API | |
headers = { | |
"Authorization": f"Bearer {DEEPSEEK_API_KEY}", | |
"Content-Type": "application/json", | |
} | |
data = { | |
"model": "deepseek-chat", # Replace with the correct model name if needed | |
"messages": [ | |
{"role": "system", "content": "You are a helpful and knowledgeable learning disabilities expert with knowledge of Central Auditory Processing Disorder (CAPD)."}, | |
{"role": "user", "content": prompt}, | |
], | |
"max_tokens": 512, | |
"temperature": 0.7, | |
} | |
response = requests.post(DEEPSEEK_API_URL, headers=headers, json=data) | |
response.raise_for_status() # Raise an error for bad status codes | |
result = response.json() | |
# Extract the response text | |
game_advice = result["choices"][0]["message"]["content"].strip() | |
except Exception as e: | |
game_advice = f"An error occurred: {str(e)}" | |
return game_advice | |
# Create Gradio interface for the CAPD Game application | |
CAPD_Game_app = gr.Interface( | |
fn=generate_game, | |
allow_flagging="never", | |
inputs=[ | |
gr.Textbox( | |
label="Auditory Memory", | |
placeholder="Describe if you have issues with your auditory memory. For instance, if you hear a number of 3 digits, do you tend to forget it? Or if someone mentions a name, do you fail to remember the name?", | |
lines=2, | |
), | |
gr.Textbox( | |
label="Selective Attention", | |
placeholder="Describe if you experience problems with selective attention. For instance, if there is noise in the room, do you find it hard to understand what is said?", | |
lines=2, | |
), | |
gr.Textbox( | |
label="Dichotic Listening", | |
placeholder="Describe if you experience problems with listening from both or either ears. For instance, do you have difficulty understanding speech presented to both ears, or difficulty ignoring one message while focusing on another?", | |
lines=2, | |
), | |
gr.Textbox( | |
label="Phonemic Awareness", | |
placeholder="Describe if you experience difficulty understanding speech, following directions, or misidentifying certain sounds, letters, or words.", | |
lines=2, | |
), | |
gr.Textbox( | |
label="Temporal Processing", | |
placeholder="Describe if you experience difficulty understanding rapid or distorted speech, or identifying individual words when the gap between words is small.", | |
lines=2, | |
), | |
gr.Textbox( | |
label="Language Comprehension", | |
placeholder="Describe if you experience difficulty understanding speech, directions, or stories.", | |
lines=2, | |
), | |
], | |
outputs=gr.Textbox(label="Recommended Games", lines=25), | |
title="CAPD Game Recommender on BrainAPT.com", | |
description="This app provides general advice regarding CAPD Games. For best results, visit your audiologist or doctor for proper diagnosis and treatment. Disclaimer: AI can make mistakes. Use with caution and at your own risk!", | |
) | |
# Launch the application | |
if __name__ == "__main__": | |
CAPD_Game_app.launch(server_name="0.0.0.0", debug=True, share=True) |