Yoxas commited on
Commit
0c0b519
·
verified ·
1 Parent(s): 8f93945

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -68
app.py CHANGED
@@ -1,74 +1,20 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
- import spaces
4
- import sentencepiece
5
 
6
- """
7
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
8
- """
9
- try:
10
- tokenizer = AutoTokenizer.from_pretrained("Yoxas/autotrain-phi3-statistical", trust_remote_code=True)
11
- model = AutoModelForCausalLM.from_pretrained("Yoxas/autotrain-phi3-statistical", trust_remote_code=True)
12
- except Exception as e:
13
- print(f"Error loading model: {e}")
14
- tokenizer = None
15
- model = None
16
 
17
- @spaces.GPU(duration=120)
18
- def respond(
19
- message,
20
- history: list[tuple[str, str]],
21
- system_message,
22
- max_tokens,
23
- temperature,
24
- top_p,
25
- ):
26
- if tokenizer is None or model is None:
27
- yield "Error: Model not loaded properly."
28
- return
29
 
30
- messages = [{"role": "system", "content": system_message}]
 
 
 
31
 
32
- for val in history:
33
- if val[0]:
34
- messages.append({"role": "user", "content": val[0]})
35
- if val[1]:
36
- messages.append({"role": "assistant", "content": val[1]})
37
 
38
- messages.append({"role": "user", "content": message})
39
-
40
- inputs = tokenizer(messages, return_tensors="pt", padding=True, truncation=True)
41
-
42
- response = ""
43
-
44
- for i in range(max_tokens):
45
- try:
46
- outputs = model.generate(inputs.input_ids, attention_mask=inputs.attention_mask, max_length=inputs.input_ids.shape[-1] + 1, temperature=temperature, top_p=top_p)
47
- token = tokenizer.decode(outputs[0, -1:], skip_special_tokens=True)
48
-
49
- response += token
50
- yield response
51
- except StopIteration:
52
- break
53
-
54
- """
55
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
56
- """
57
- demo = gr.ChatInterface(
58
- respond,
59
- additional_inputs=[
60
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
61
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
62
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
63
- gr.Slider(
64
- minimum=0.1,
65
- maximum=1.0,
66
- value=0.95,
67
- step=0.05,
68
- label="Top-p (nucleus sampling)",
69
- ),
70
- ],
71
- )
72
-
73
- if __name__ == "__main__":
74
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
 
 
3
 
4
+ # Load the tokenizer and model
5
+ tokenizer = AutoTokenizer.from_pretrained("Yoxas/autotrain-phi3-statistical", trust_remote_code=True)
6
+ model = AutoModelForCausalLM.from_pretrained("Yoxas/autotrain-phi3-statistical", trust_remote_code=True)
 
 
 
 
 
 
 
7
 
8
+ # Use a pipeline as a high-level helper
9
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, trust_remote_code=True)
 
 
 
 
 
 
 
 
 
 
10
 
11
+ # Define the chatbot function
12
+ def chatbot(input_text):
13
+ response = pipe(input_text, max_length=150, num_return_sequences=1)
14
+ return response[0]['generated_text']
15
 
16
+ # Create the Gradio interface
17
+ interface = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Research Paper Abstract Chatbot")
 
 
 
18
 
19
+ # Launch the Gradio app
20
+ interface.launch()