rahul7star commited on
Commit
60a6925
·
verified ·
1 Parent(s): aac0f46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -49
app.py CHANGED
@@ -1,57 +1,27 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
 
5
- # Model ve tokenizer yükleme
6
- model_name = "rahul7star/gemma-3bit" # veya "google/gemma-3-1b-it"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForCausalLM.from_pretrained(
9
- model_name,
10
- torch_dtype=torch.float16,
11
- device_map="auto"
12
  )
13
 
14
- # Sistem promptu
15
- system_prompt = """"Enhance and expand the following prompt with more details and context:."""
 
 
 
 
16
 
17
- def predict(message, history):
18
- # Sohbet geçmişini formatlama
19
- messages = [{"role": "system", "content": system_prompt}]
20
-
21
- # Önceki mesajları ekleme
22
- for user_msg, bot_msg in history:
23
- messages.append({"role": "user", "content": user_msg})
24
- messages.append({"role": "assistant", "content": bot_msg})
25
-
26
- # Yeni mesajı ekleme
27
- messages.append({"role": "user", "content": message})
28
-
29
- # Tokenize etme
30
- inputs = tokenizer.apply_chat_template(
31
- messages,
32
- return_tensors="pt"
33
- ).to(model.device)
34
-
35
- # Yanıt üretme
36
- outputs = model.generate(
37
- inputs,
38
- max_new_tokens=512,
39
- temperature=0.7,
40
- top_p=0.95,
41
- do_sample=True
42
- )
43
-
44
- # Yanıtı decode etme
45
- response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
46
-
47
- return response
48
-
49
- # Gradio arayüzü
50
- demo = gr.ChatInterface(
51
- fn=predict,
52
- title="Sağlık Danışmanı Chatbot (Gemma 3)",
53
- description="Bu chatbot, sağlık, beslenme ve egzersiz konularında bilgi vermek için tasarlanmıştır. Tıbbi teşhis koymaz veya reçete önermez.",
54
- theme="soft"
55
  )
56
 
57
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
  import torch
4
 
5
+ pipe = pipeline(
6
+ "image-text-to-text",
7
+ model="rahul7star/gemma-3bit",
8
+ device="cpu",
9
+ torch_dtype=torch.bfloat16
 
 
10
  )
11
 
12
+ def chat(image, prompt):
13
+ messages = [
14
+ {"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": prompt}]}
15
+ ]
16
+ out = pipe(text=messages, max_new_tokens=200)
17
+ return out[0]["generated_text"][-1]["content"]
18
 
19
+ demo = gr.Interface(
20
+ fn=chat,
21
+ inputs=[gr.Image(type="pil"), gr.Textbox(label="Prompt")],
22
+ outputs=gr.Textbox(label="Response"),
23
+ title="Gemma 3 4B‑IT Vision‑Text Chat",
24
+ description="Upload an image and ask questions using Google’s gems!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  )
26
 
27
+ demo.launch()