amrfayadd commited on
Commit
6b5d4b4
·
verified ·
1 Parent(s): e299696

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -10
app.py CHANGED
@@ -1,15 +1,28 @@
1
- from transformers import AutoTokenizer, AutoModelForCausalLM
2
  import gradio as gr
3
- import torch
 
4
 
5
- model_name = "MBZUAI/BiMediX2-8B-hf"
6
 
7
- tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
8
- model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto", trust_remote_code=True)
 
 
 
 
 
 
9
 
10
- def chat(text):
11
- inputs = tokenizer(text, return_tensors="pt").to(model.device)
12
- outputs = model.generate(**inputs, max_new_tokens=300)
13
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
14
 
15
- gr.Interface(chat, inputs="text", outputs="text").launch()
 
 
 
 
 
 
1
  import gradio as gr
2
+ import requests
3
+ import json
4
 
5
+ API_URL = "http://localhost:8000/v1/chat/completions"
6
 
7
+ def chat_fn(message, history):
8
+ headers = {"Content-Type": "application/json"}
9
+ payload = {
10
+ "model": "MBZUAI/BiMediX2-8B-hf",
11
+ "messages": [{"role": "user", "content": message}],
12
+ "max_tokens": 512,
13
+ "temperature": 0.2
14
+ }
15
 
16
+ try:
17
+ response = requests.post(API_URL, headers=headers, json=payload)
18
+ data = response.json()
19
+ reply = data["choices"][0]["message"]["content"]
20
+ return reply
21
+ except Exception as e:
22
+ return f"Error: {str(e)}"
23
 
24
+ gr.ChatInterface(
25
+ fn=chat_fn,
26
+ title="BiMediX2 Medical Chatbot",
27
+ description="Ask any medical question."
28
+ ).launch()