Nishant51 commited on
Commit
5c4dbe9
·
verified ·
1 Parent(s): d7e02e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -25
app.py CHANGED
@@ -1,30 +1,33 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
 
4
- # 1. Load the AI Brain (GPT-2)
5
- # We do this OUTSIDE the function so it only loads once when the app starts
6
- print("Loading AI...")
7
- generator = pipeline('text-generation', model='gpt2')
8
 
9
- # 2. Define the function
10
- def ask_brain(question):
11
- # Ask the AI to generate text
12
- result = generator(question, max_length=100, num_return_sequences=1)
13
-
14
- # Extract the text properly
15
- answer = result[0]['generated_text']
16
-
17
- # Return the answer to the user
18
- return answer
19
-
20
- # 3. Create the App Interface
21
- iface = gr.Interface(
22
- fn=ask_brain,
23
- inputs="text",
24
- outputs="text",
25
- title="My Free AI Model",
26
- description="Ask me anything!"
27
  )
28
 
29
- # 4. Launch the App
30
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from llama_cpp import Llama
3
 
4
+ # 1. Load the Model
5
+ # This automatically downloads the "DeepSeek-R1-Distill-Llama-8B" (GGUF version)
6
+ # We use the Q4_K_M version because it fits in the FREE 16GB RAM tier.
7
+ print("⏳ Downloading & Loading Model... (This takes 1-2 mins on first run)")
8
 
9
+ llm = Llama.from_pretrained(
10
+ repo_id="bartowski/DeepSeek-R1-Distill-Llama-8B-GGUF",
11
+ filename="DeepSeek-R1-Distill-Llama-8B-Q4_K_M.gguf",
12
+ verbose=True,
13
+ n_ctx=4096 # Context window (memory of the conversation)
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  )
15
 
16
+ # 2. Define the Chat Function
17
+ def chat_with_deepseek(message, history):
18
+ # Format the prompt for DeepSeek
19
+ # It expects: User: <msg> \n Assistant:
20
+ prompt = f"User: {message}\nAssistant:"
21
+
22
+ # Generate response
23
+ output = llm(
24
+ prompt,
25
+ max_tokens=512, # How long the answer can be
26
+ stop=["User:", "\n\n"], # Stop it from talking to itself
27
+ echo=False
28
+ )
29
+ return output['choices'][0]['text']
30
+
31
+ # 3. Launch the Chat Interface
32
+ # We use ChatInterface because it handles the UI automatically
33
+ gr.ChatInterface(chat_with_deepseek).launch()