Aaresh5308a commited on
Commit
1809a85
·
1 Parent(s): 93efb9b
Files changed (2) hide show
  1. app.py +63 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
4
+ from fastapi import FastAPI, Request
5
+ from fastapi.responses import JSONResponse
6
+ import uvicorn
7
+ import time
8
+
9
+ # 1. Load the model
10
+ model_id = "Aaresh5308a/Gemma4_e2b_NepalaiEdition"
11
+ pipe = pipeline(
12
+ "text-generation",
13
+ model=model_id,
14
+ model_kwargs={"torch_dtype": torch.bfloat16},
15
+ device_map="auto",
16
+ )
17
+
18
+ # 2. Define the Inference Function
19
+ def chat_response(message, history):
20
+ # Format the prompt for Gemma 4
21
+ prompt = pipe.tokenizer.apply_chat_template(
22
+ history + [{"role": "user", "content": message}],
23
+ tokenize=False,
24
+ add_generation_prompt=True
25
+ )
26
+ outputs = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)
27
+ return outputs[0]["generated_text"][len(prompt):]
28
+
29
+ # 3. Create Gradio UI
30
+ demo = gr.ChatInterface(fn=chat_response, title="Gemma 4 Nepali Edition")
31
+
32
+ # 4. Create OpenAI-Compatible API using FastAPI
33
+ app = FastAPI()
34
+
35
+ @app.post("/v1/chat/completions")
36
+ async def chat_completions(request: Request):
37
+ data = await request.json()
38
+ messages = data.get("messages", [])
39
+
40
+ # Simple extraction of the last user message for this example
41
+ user_msg = messages[-1]["content"]
42
+ history = messages[:-1]
43
+
44
+ response_text = chat_response(user_msg, history)
45
+
46
+ # Return OpenAI formatted JSON
47
+ return {
48
+ "id": f"chatcmpl-{int(time.time())}",
49
+ "object": "chat.completion",
50
+ "created": int(time.time()),
51
+ "model": model_id,
52
+ "choices": [{
53
+ "index": 0,
54
+ "message": {"role": "assistant", "content": response_text},
55
+ "finish_reason": "stop"
56
+ }]
57
+ }
58
+
59
+ # Mount Gradio to FastAPI
60
+ app = gr.mount_gradio_app(app, demo, path="/")
61
+
62
+ if __name__ == "__main__":
63
+ uvicorn.run(app, host="0.0.0.0", port=7860)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ gradio
4
+ fastapi
5
+ uvicorn