Spaces:
Sleeping
Sleeping
File size: 1,670 Bytes
e6b6060 262542b e6b6060 262542b e30d0e0 e6b6060 262542b ee76456 262542b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
# Load the tokenizer and model from your Hugging Face repository
model_path = "sakthi54321/Power_chat_ai" # Update with your model's repo
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path)
# Function to generate a response based on input
def generate_response(prompt):
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(
inputs["input_ids"],
max_length=150,
num_return_sequences=1,
do_sample=True,
temperature=0.7
)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
# Function to determine if the prompt is code-related
def is_code_prompt(prompt):
code_keywords = ['def', 'import', 'print', 'class', 'function', 'variable', 'while', 'for']
return any(keyword in prompt for keyword in code_keywords)
# Combined response function (handles both general and code)
def handle_prompt(prompt):
if is_code_prompt(prompt):
return generate_response(f"### Instruction: Write a Python code to solve the following problem: {prompt}\n### Response:")
else:
return generate_response(f"### Instruction: {prompt}\n### Response:")
# Define Gradio interface with input and output fields
iface = gr.Interface(
fn=handle_prompt,
inputs="text",
outputs="text",
live=True,
title="TinyLlama Assistant",
description="Interact with the TinyLlama-1B model for general and coding tasks. Enter a prompt to get a response, either in general text or Python code."
)
# Launch the app
iface.launch()
|