Spaces:
Sleeping
Create app.py
Browse filesimport gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# Use a smaller instruct-tuned model that runs on Hugging Face Spaces
model_name = "tiiuae/falcon-7b-instruct" # Falcon-7B is lighter than Mistral
# Load model and tokenizer with optimizations
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16,
device_map="auto" # Uses available GPU/CPU
)
# AI Response Function
def nithin_ai(question):
inputs = tokenizer(question, return_tensors="pt").input_ids.to(model.device)
outputs = model.generate(inputs, max_length=200)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return response
# Gradio Chat Interface
iface = gr.Interface(
fn=nithin_ai,
inputs="text",
outputs="text",
title="Nithin AI - Student Doubt Solver",
description="Ask any question related to robotics, science, or math!"
)
iface.launch()
|
@@ -2,18 +2,20 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
model_name = "tiiuae/falcon-7b-instruct" #
|
| 7 |
|
|
|
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
model_name,
|
| 11 |
torch_dtype=torch.float16,
|
| 12 |
-
device_map="
|
| 13 |
)
|
| 14 |
|
|
|
|
| 15 |
def nithin_ai(question):
|
| 16 |
-
inputs = tokenizer(question, return_tensors="pt").input_ids
|
| 17 |
outputs = model.generate(inputs, max_length=200)
|
| 18 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 19 |
return response
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Use a smaller instruct-tuned model that runs on Hugging Face Spaces
|
| 6 |
+
model_name = "tiiuae/falcon-7b-instruct" # Falcon-7B is lighter than Mistral
|
| 7 |
|
| 8 |
+
# Load model and tokenizer with optimizations
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
model_name,
|
| 12 |
torch_dtype=torch.float16,
|
| 13 |
+
device_map="auto" # Uses available GPU/CPU
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# AI Response Function
|
| 17 |
def nithin_ai(question):
|
| 18 |
+
inputs = tokenizer(question, return_tensors="pt").input_ids.to(model.device)
|
| 19 |
outputs = model.generate(inputs, max_length=200)
|
| 20 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 21 |
return response
|