Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,37 @@
|
|
|
|
1 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
2 |
import torch
|
3 |
-
from flask import Flask, request, jsonify
|
4 |
|
5 |
-
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
prompt = input_data.get("prompt", "")
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
1 |
+
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
3 |
import torch
|
|
|
4 |
|
5 |
+
# Load free model from Hugging Face (like Mistral or Mixtral)
|
6 |
+
model_name = "mistralai/Mistral-7B-Instruct-v0.1"
|
7 |
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
10 |
+
model_name,
|
11 |
+
torch_dtype=torch.float16,
|
12 |
+
device_map="auto"
|
13 |
+
)
|
14 |
|
15 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
|
|
16 |
|
17 |
+
def generate_answer(question):
|
18 |
+
prompt = f"[INST] {question} [/INST]"
|
19 |
+
output = pipe(prompt, max_new_tokens=500, do_sample=True, temperature=0.7)[0]['generated_text']
|
|
|
20 |
|
21 |
+
# Cut only the answer part (after the [/INST] token)
|
22 |
+
if "[/INST]" in output:
|
23 |
+
answer = output.split("[/INST]")[-1].strip()
|
24 |
+
else:
|
25 |
+
answer = output
|
26 |
+
|
27 |
+
return answer
|
28 |
+
|
29 |
+
iface = gr.Interface(
|
30 |
+
fn=generate_answer,
|
31 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask any question..."),
|
32 |
+
outputs="text",
|
33 |
+
title="MentorMind AI Q&A",
|
34 |
+
description="Ask anything and get a detailed human-like answer!"
|
35 |
+
)
|
36 |
|
37 |
+
iface.launch()
|
|