Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
+
|
| 4 |
+
# Your Hugging Face model repo ID
|
| 5 |
+
model_id = "chanystrange/mistral-agri-merged_143"
|
| 6 |
+
|
| 7 |
+
# Load tokenizer and model
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
model_id,
|
| 11 |
+
device_map="auto",
|
| 12 |
+
load_in_4bit=True
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 16 |
+
|
| 17 |
+
# Function for API
|
| 18 |
+
def chat(prompt):
|
| 19 |
+
output = generator(prompt, max_new_tokens=200, do_sample=True)
|
| 20 |
+
return output[0]["generated_text"]
|
| 21 |
+
|
| 22 |
+
# Gradio Interface (this works as REST API too)
|
| 23 |
+
demo = gr.Interface(fn=chat, inputs="text", outputs="text")
|
| 24 |
+
|
| 25 |
+
# Launch Space with share=True to get a public URL
|
| 26 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|