Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import torch
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
import spaces
|
6 |
+
|
7 |
+
# Check if we're running in a Hugging Face Space with zero GPU constraints
|
8 |
+
IS_SPACES_ZERO = os.environ.get("SPACES_ZERO_GPU", "0") == "1"
|
9 |
+
IS_SPACE = os.environ.get("SPACE_ID", None) is not None
|
10 |
+
|
11 |
+
# Determine device (set to CPU for zero-GPU)
|
12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
print(f"Using device: {device}")
|
14 |
+
|
15 |
+
# Load model and tokenizer
|
16 |
+
model_name = "linjc16/Panacea-7B-Chat"
|
17 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
|
18 |
+
model.to(device)
|
19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
20 |
+
|
21 |
+
# Define prompt structure
|
22 |
+
@spaces.GPU # This will handle spaces for either GPU or CPU as available
|
23 |
+
def generate_response(system_instruction, user_input):
|
24 |
+
# Format the prompt with the system instruction and user input
|
25 |
+
prompt = f"{system_instruction}\n\nUser: {user_input}\nBot:"
|
26 |
+
|
27 |
+
# Tokenize and prepare inputs
|
28 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
29 |
+
|
30 |
+
# Generate model response
|
31 |
+
with torch.no_grad():
|
32 |
+
outputs = model.generate(**inputs, max_new_tokens=100, do_sample=True)
|
33 |
+
|
34 |
+
# Decode response
|
35 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("Bot:")[-1].strip()
|
36 |
+
|
37 |
+
return response # Return only bot's response for display in the right column
|
38 |
+
|
39 |
+
# Set up Gradio interface
|
40 |
+
with gr.Blocks() as demo:
|
41 |
+
gr.Markdown("# Clinical Trial Chatbot")
|
42 |
+
|
43 |
+
with gr.Row():
|
44 |
+
# Left column for inputs
|
45 |
+
with gr.Column():
|
46 |
+
system_instruction = gr.Textbox(
|
47 |
+
placeholder="Enter system instruction here...", label="System Instruction")
|
48 |
+
user_input = gr.Textbox(
|
49 |
+
placeholder="Type your message here...", label="Your Message")
|
50 |
+
submit_btn = gr.Button("Submit")
|
51 |
+
|
52 |
+
# Right column for displaying bot's response
|
53 |
+
with gr.Column():
|
54 |
+
response_display = gr.Textbox(
|
55 |
+
label="Bot Response", interactive=False, placeholder="Response will appear here.")
|
56 |
+
|
57 |
+
# Link the submit button to the generate_response function
|
58 |
+
submit_btn.click(generate_response, [system_instruction, user_input], response_display)
|
59 |
+
|
60 |
+
# Launch the app
|
61 |
+
demo.launch(share=True)
|