segestic commited on
Commit
30d62b2
·
verified ·
1 Parent(s): 196976e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from llama_cpp import Llama
3
+ from huggingface_hub import hf_hub_download
4
+
5
+ # Download model from Hugging Face (if not already present)
6
+ model_file_path = hf_hub_download(
7
+ repo_id="TheBloke/Llama-2-7B-GGUF",
8
+ filename="llama-2-7b.Q4_0.gguf"
9
+ )
10
+
11
+ # Try to load the model from the downloaded file
12
+ try:
13
+ llm_llama_cpp = Llama(
14
+ model_path=model_file_path, # Path where the model is downloaded
15
+ verbose=False # Suppress llama.cpp's own informational prints
16
+ )
17
+
18
+ # Function that generates a response using the Llama model
19
+ def talk(prompt, history):
20
+ try:
21
+ # Generate text with streaming
22
+ response_stream = llm_llama_cpp.create_completion(
23
+ prompt,
24
+ max_tokens=200, # You can adjust the max tokens as needed
25
+ stream=True
26
+ )
27
+
28
+ response = ""
29
+ for chunk in response_stream:
30
+ # Extract the text from the current chunk
31
+ if 'choices' in chunk and len(chunk['choices']) > 0 and 'text' in chunk['choices'][0]:
32
+ response += chunk['choices'][0]['text']
33
+
34
+ # Return the full response after streaming is done
35
+ return response
36
+
37
+ except Exception as e:
38
+ return f"Error with llama-cpp-python: {e}"
39
+
40
+ except FileNotFoundError:
41
+ print(f"Error: Model file not found at {model_file_path}")
42
+ except Exception as e:
43
+ print(f"Error with llama-cpp-python: {e}")
44
+
45
+ # Gradio interface setup
46
+ TITLE = "AI Copilot for Diabetes Patients"
47
+ DESCRIPTION = "I provide answers to concerns related to Diabetes"
48
+
49
+ # Design chatbot interface
50
+ demo = gr.ChatInterface(
51
+ fn=talk, # The function that processes user input and returns the response
52
+ chatbot=gr.Chatbot(
53
+ show_label=True,
54
+ show_share_button=True,
55
+ show_copy_button=True,
56
+ likeable=True,
57
+ layout="bubble", # Display messages in bubble format
58
+ bubble_full_width=False,
59
+ ),
60
+ theme="Soft", # Soft theme for the UI
61
+ examples=[["what is Diabetes?"]], # Example query to get started
62
+ title=TITLE, # Title of the interface
63
+ description=DESCRIPTION, # Description for context
64
+ )
65
+
66
+ # Launch the chatbot interface
67
+ demo.launch()