Raiff1982 commited on
Commit
9f821aa
·
verified ·
1 Parent(s): d2ef3e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -27
app.py CHANGED
@@ -1,35 +1,62 @@
1
  import gradio as gr
2
- import asyncio
3
- from AICoreAGIX_with_TB import AICoreAGIX
4
 
 
5
  ai_core = AICoreAGIX()
6
 
7
- async def diagnose_tb(image_file, audio_file):
8
- user_id = 1 # Example user
9
- result = await ai_core.run_tb_diagnostics(image_file.name, audio_file.name, user_id)
10
- return (
11
- f"**TB Risk Level:** {result['tb_risk']}\n\n"
12
- f"**Image Result:** {result['image_analysis']['result']} "
13
- f"(Confidence: {result['image_analysis']['confidence']:.2f})\n\n"
14
- f"**Audio Result:** {result['audio_analysis']['result']} "
15
- f"(Confidence: {result['audio_analysis']['confidence']:.2f})\n\n"
16
- f"**Ethical Analysis:** {result['ethical_analysis']}\n\n"
17
- f"**Explanation:** {result['explanation']}"
18
- )
19
-
20
- # Async wrapper for Gradio
21
- def sync_diagnose_tb(image_file, audio_file):
22
- return asyncio.run(diagnose_tb(image_file, audio_file))
23
-
24
- demo = gr.Interface(
25
- fn=sync_diagnose_tb,
26
- inputs=[
27
- gr.File(label="Upload TB Saliva Image"),
28
- gr.File(label="Upload Cough Audio File (.wav)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  ],
30
- outputs=gr.Markdown(label="Codriao's Response"),
31
- title="Codriao TB Risk Analyzer",
32
- description="Upload a microscopy image and cough audio to analyze TB risk with compassionate AI support."
33
  )
34
 
35
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ from Codriaoagix import AICoreAGIX # Ensure this imports your AICoreAGIX class
4
 
5
+ # Initialize the AI core
6
  ai_core = AICoreAGIX()
7
 
8
+ def respond(message, history, system_message, max_tokens, temperature, top_p, image, audio):
9
+ # Process the uploaded files
10
+ if image and audio:
11
+ # Save the uploaded files to disk or process them as needed
12
+ image_path = "uploaded_image.png"
13
+ audio_path = "uploaded_audio.wav"
14
+ image.save(image_path)
15
+ audio.save(audio_path)
16
+
17
+ # Run TB diagnostics
18
+ tb_result = ai_core.run_tb_diagnostics(image_path, audio_path, user_id=1) # Replace with actual user_id handling
19
+
20
+ # Incorporate TB diagnostic results into the response
21
+ tb_message = f"TB Diagnostic Result: {tb_result['tb_risk']}\n"
22
+ tb_message += f"Image Analysis: {tb_result['image_analysis']}\n"
23
+ tb_message += f"Audio Analysis: {tb_result['audio_analysis']}\n"
24
+ tb_message += f"Shareable Link: {tb_result['shareable_link']}\n\n"
25
+ else:
26
+ tb_message = "No TB diagnostic data provided.\n\n"
27
+
28
+ # Existing chat functionality
29
+ messages = [{"role": "system", "content": system_message}]
30
+ for user_msg, bot_msg in history:
31
+ if user_msg:
32
+ messages.append({"role": "user", "content": user_msg})
33
+ if bot_msg:
34
+ messages.append({"role": "assistant", "content": bot_msg})
35
+ messages.append({"role": "user", "content": message})
36
+
37
+ response = ""
38
+ for message in client.chat_completion(
39
+ messages,
40
+ max_tokens=max_tokens,
41
+ stream=True,
42
+ temperature=temperature,
43
+ top_p=top_p,
44
+ ):
45
+ token = message.choices[0].delta.content
46
+ response += token
47
+ yield tb_message + response
48
+
49
+ # Define the Gradio interface
50
+ demo = gr.ChatInterface(
51
+ respond,
52
+ additional_inputs=[
53
+ gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
54
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
55
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
56
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
57
+ gr.inputs.Image(type="pil", label="Upload Saliva Microscopy Image"),
58
+ gr.inputs.Audio(type="file", label="Upload Cough Audio Recording"),
59
  ],
 
 
 
60
  )
61
 
62
  if __name__ == "__main__":