junaidbaber commited on
Commit
7077c22
·
1 Parent(s): 0c6a823

Uploading the demo

Browse files
Files changed (4) hide show
  1. README.md +4 -4
  2. app.py +64 -0
  3. requirements.txt +4 -0
  4. run_model.py +36 -0
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: Demo Lowcode Llm
3
- emoji: 🦀
4
- colorFrom: red
5
- colorTo: blue
6
  sdk: streamlit
7
  sdk_version: 1.41.1
8
  app_file: app.py
 
1
  ---
2
+ title: Llm Medical
3
+ emoji: 🏆
4
+ colorFrom: gray
5
+ colorTo: green
6
  sdk: streamlit
7
  sdk_version: 1.41.1
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ # Hugging Face repository details
6
+ MODEL_ID = "meta-llama/CodeLlama-7b-Instruct-hf"
7
+
8
+ def load_model():
9
+ """Load the Hugging Face model and tokenizer."""
10
+ try:
11
+ st.write("Loading model and tokenizer...")
12
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
13
+ model = AutoModelForCausalLM.from_pretrained(
14
+ MODEL_ID, device_map="auto", torch_dtype=torch.float16
15
+ )
16
+ st.write("Model and tokenizer successfully loaded.")
17
+ return tokenizer, model
18
+ except Exception as e:
19
+ st.error(f"Error loading model: {e}")
20
+ return None, None
21
+
22
+ # Load the model and tokenizer
23
+ @st.cache_resource
24
+ def get_model():
25
+ return load_model()
26
+
27
+ tokenizer, model = get_model()
28
+
29
+ # Streamlit UI
30
+ st.title("Medical Chatbot")
31
+ st.write("This chatbot provides medical assistance. Type your question below!")
32
+
33
+ if model is None or tokenizer is None:
34
+ st.error("Model failed to load. Please check the Hugging Face model path or environment configuration.")
35
+ else:
36
+ user_input = st.text_input("You:", placeholder="Enter your medical question here...", key="input_box")
37
+
38
+ if st.button("Send"):
39
+ if user_input.strip():
40
+ # Construct the prompt
41
+ SYSTEM_PROMPT = "You are a helpful medical assistant. Provide accurate and concise answers."
42
+ full_prompt = f"{SYSTEM_PROMPT}\nUser: {user_input}\nAssistant:"
43
+
44
+ # Tokenize the input
45
+ inputs = tokenizer(full_prompt, return_tensors="pt", truncation=True).to("cuda")
46
+
47
+ try:
48
+ # Generate the response
49
+ outputs = model.generate(
50
+ inputs["input_ids"],
51
+ max_length=200, # Limit response length
52
+ temperature=0.7, # Control randomness
53
+ top_p=0.9, # Top-p sampling
54
+ pad_token_id=tokenizer.eos_token_id
55
+ )
56
+
57
+ # Decode and display the response
58
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("Assistant:")[-1].strip()
59
+ st.write(f"**Model:** {response}")
60
+
61
+ except Exception as e:
62
+ st.error(f"Error generating response: {e}")
63
+ else:
64
+ st.warning("Please enter a valid question.")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ llama-cpp-python
3
+ huggingface-hub
4
+
run_model.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from llama_cpp import Llama
2
+
3
+ # Path to the GGUF model file
4
+ MODEL_PATH = "llama-3.1-8B.gguf"
5
+
6
+ # Load the model
7
+ print("Loading the model...")
8
+ try:
9
+ llama = Llama(model_path=MODEL_PATH, n_ctx=1024, n_threads=4)
10
+ print("Model loaded successfully!")
11
+ except Exception as e:
12
+ print(f"Failed to load the model: {e}")
13
+ exit(1)
14
+
15
+ # Chat loop
16
+ print("Chat with the model! Type 'exit' to end the conversation.")
17
+ while True:
18
+ user_input = input("You: ").strip()
19
+ if user_input.lower() == "exit":
20
+ print("Exiting chat. Goodbye!")
21
+ break
22
+
23
+ # Query the model
24
+ print("Thinking...")
25
+ response = llama(
26
+ user_input,
27
+ max_tokens=50, # Limit response length
28
+ temperature=0.7, # Control randomness
29
+ top_p=0.9, # Top-p sampling
30
+ stop=["You:"] # Stop at the next user prompt
31
+ )
32
+
33
+ # Extract and clean response text
34
+ response_text = response['choices'][0]['text'].strip()
35
+ print(f"Model: {response_text}")
36
+