Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,118 +1,110 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
-
|
| 5 |
-
# HuatuoGPT model
|
| 6 |
-
model_name = "FreedomIntelligence/HuatuoGPT-7B"
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
self.
|
| 12 |
-
self.
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
self.
|
| 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 |
-
return
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
with gr.
|
| 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 |
-
# Connect actions
|
| 113 |
-
input_box.submit(show_reasoning, inputs=input_box, outputs=output_box)
|
| 114 |
-
ask_btn.click(show_reasoning, inputs=input_box, outputs=output_box)
|
| 115 |
-
clear_btn.click(lambda: ("", ""), outputs=[input_box, output_box])
|
| 116 |
-
|
| 117 |
-
if __name__ == "__main__":
|
| 118 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
# HuatuoGPT model
|
| 6 |
+
model_name = "FreedomIntelligence/HuatuoGPT-7B"
|
| 7 |
+
|
| 8 |
+
class MedicalAI:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.model = None
|
| 11 |
+
self.tokenizer = None
|
| 12 |
+
self.loaded = False
|
| 13 |
+
|
| 14 |
+
def load_model(self):
|
| 15 |
+
if self.loaded:
|
| 16 |
+
return True
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
print("π Loading HuatuoGPT-7B...")
|
| 20 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 21 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 22 |
+
model_name,
|
| 23 |
+
torch_dtype=torch.float16,
|
| 24 |
+
device_map="auto",
|
| 25 |
+
trust_remote_code=True
|
| 26 |
+
)
|
| 27 |
+
self.loaded = True
|
| 28 |
+
print("β
HuatuoGPT Ready!")
|
| 29 |
+
return True
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"β Error: {e}")
|
| 32 |
+
return False
|
| 33 |
+
|
| 34 |
+
medical_ai = MedicalAI()
|
| 35 |
+
|
| 36 |
+
def get_ai_thinking(message):
|
| 37 |
+
"""Get HuatuoGPT's complete reasoning process"""
|
| 38 |
+
if not medical_ai.loaded:
|
| 39 |
+
success = medical_ai.load_model()
|
| 40 |
+
if not success:
|
| 41 |
+
return "π Loading AI... Please wait."
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
# Simple one-line prompt to see natural reasoning
|
| 45 |
+
prompt = f"Question: {message}\nAnswer:"
|
| 46 |
+
|
| 47 |
+
inputs = medical_ai.tokenizer(prompt, return_tensors="pt")
|
| 48 |
+
|
| 49 |
+
# Generate longer response to see full reasoning
|
| 50 |
+
with torch.no_grad():
|
| 51 |
+
outputs = medical_ai.model.generate(
|
| 52 |
+
inputs.input_ids,
|
| 53 |
+
max_new_tokens=400, # More tokens to see reasoning
|
| 54 |
+
temperature=0.7,
|
| 55 |
+
do_sample=True,
|
| 56 |
+
top_p=0.9,
|
| 57 |
+
repetition_penalty=1.1,
|
| 58 |
+
pad_token_id=medical_ai.tokenizer.eos_token_id
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Get full response including reasoning
|
| 62 |
+
full_response = medical_ai.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 63 |
+
|
| 64 |
+
# Extract everything after "Answer:"
|
| 65 |
+
if "Answer:" in full_response:
|
| 66 |
+
reasoning = full_response.split("Answer:")[1].strip()
|
| 67 |
+
else:
|
| 68 |
+
reasoning = full_response.replace(prompt, "").strip()
|
| 69 |
+
|
| 70 |
+
return reasoning
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return f"β Error: {str(e)}"
|
| 74 |
+
|
| 75 |
+
# FIXED: Remove theme parameter
|
| 76 |
+
with gr.Blocks() as demo: # Removed: theme=gr.themes.Soft()
|
| 77 |
+
gr.Markdown("# π§ HuatuoGPT Medical Reasoning")
|
| 78 |
+
gr.Markdown("**See how the AI doctor thinks and reasons**")
|
| 79 |
+
|
| 80 |
+
with gr.Row():
|
| 81 |
+
with gr.Column(scale=1):
|
| 82 |
+
input_box = gr.Textbox(
|
| 83 |
+
label="Your Medical Question",
|
| 84 |
+
placeholder="headache? fever? sleep problems?",
|
| 85 |
+
max_lines=1, # Single line only
|
| 86 |
+
)
|
| 87 |
+
ask_btn = gr.Button("π€ Ask HuatuoGPT", variant="primary")
|
| 88 |
+
|
| 89 |
+
with gr.Column(scale=2):
|
| 90 |
+
output_box = gr.Textbox(
|
| 91 |
+
label="HuatuoGPT's Reasoning",
|
| 92 |
+
placeholder="AI thinking will appear here...",
|
| 93 |
+
lines=8,
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
clear_btn = gr.Button("ποΈ Clear")
|
| 97 |
+
|
| 98 |
+
# Function to get AI reasoning
|
| 99 |
+
def show_reasoning(message):
|
| 100 |
+
if not message.strip():
|
| 101 |
+
return "Please enter a medical question."
|
| 102 |
+
return get_ai_thinking(message)
|
| 103 |
+
|
| 104 |
+
# Connect actions
|
| 105 |
+
input_box.submit(show_reasoning, inputs=input_box, outputs=output_box)
|
| 106 |
+
ask_btn.click(show_reasoning, inputs=input_box, outputs=output_box)
|
| 107 |
+
clear_btn.click(lambda: ("", ""), outputs=[input_box, output_box])
|
| 108 |
+
|
| 109 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
demo.launch()
|